components/ciphertool/src/main/java/org/wso2/ciphertool/CipherTool.java
private static void tokenToConfigFile(String fileName, String xPath, String secretAlias, String encryptParamKey) {
        if (xPath != null && !xPath.equals("") && secretAlias != null && !secretAlias.equals("")) {
            String filePath = Utils.getConfigFilePath(fileName);
            try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(filePath);
                Node rootNode = doc.getDocumentElement();
                Node secretNamespaceNode = doc.createAttribute(Constants.SecureVault.NS_PREFIX);
                secretNamespaceNode.setTextContent(Constants.SecureVault.NS);
                rootNode.getAttributes().setNamedItem(secretNamespaceNode);
                Node secretProviderNode = doc.createAttribute(Constants.SecureVault.ATTRIBUTE);
                secretProviderNode.setTextContent(Constants.SecureVault.SECRET_PROVIDER_CLASS);

                XPathFactory xpf = XPathFactory.newInstance();
                XPath xp = xpf.newXPath();
                XPathExpression xpathEx = xp.compile(xPath);
                NodeList securedNodes = (NodeList) xpathEx.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
                if (securedNodes != null && securedNodes.getLength() > 0) {
                    for (int i = 0; i < securedNodes.getLength(); i++) {
                        Node secretAliasNode = doc.createAttribute(Constants.SecureVault.ALIAS);
                        secretAliasNode.setTextContent(secretAlias);
                        Node node = securedNodes.item(i);
                        if (node != null) {
                            if (!encryptParamKey.isEmpty()) {
                                node.getAttributes().getNamedItem(encryptParamKey)
                                    .setNodeValue(Constants.SecureVault.PASSWORD);
                            } else {
                                node.setTextContent(Constants.SecureVault.PASSWORD);
                            }
                            node.getAttributes().setNamedItem(secretAliasNode);
                        }
                    }
                } else {
                    throw new CipherToolException(
                            "Element for secret alias '" + secretAlias + "' can not be found in " +
                            fileName + " file or You have entered invalid Xpath value");
                }
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File(filePath));
                transformer.transform(source, result);
            } catch (ParserConfigurationException e) {
                throw new CipherToolException(
                        "Error writing protected token [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (XPathExpressionException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (TransformerException e) {
                throw new CipherToolException(
                        "Error writing protected token [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (SAXException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (IOException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            }

            System.out.println("Protected Token [" + secretAlias + "] is updated in " + fileName + " successfully\n");
        }
    }
/**
     * write the XML syntax to the configuration file,
     *
     * @param fileName        file name
     * @param xPath           Xpath value of the element that needs to be modified
     * @param secretAlias     alias name for the element value
     * @param encryptParamKey If this value is not Empty then its corresponding value to "password"
     */
write the XML syntax to the configuration file.

@param fileName        file name
@param xPath           Xpath value of the element that needs to be modified
@param secretAlias     alias name for the element value
@param encryptParamKey If this value is not Empty then its corresponding value to "password"
write the XML syntax to the configuration file . @param fileName file name @param xPath Xpath value of the element that needs to be modified @param secretAlias alias name for the element value @param encryptParamKey If this value is not Empty then its corresponding value to " password "
private static void tokenToConfigFile(String fileName, String xPath, String secretAlias, String encryptParamKey) {
        if (xPath != null && !xPath.equals("") && secretAlias != null && !secretAlias.equals("")) {
            String filePath = Utils.getConfigFilePath(fileName);
            try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(filePath);
                Node rootNode = doc.getDocumentElement();
                Node secretNamespaceNode = doc.createAttribute(Constants.SecureVault.NS_PREFIX);
                secretNamespaceNode.setTextContent(Constants.SecureVault.NS);
                rootNode.getAttributes().setNamedItem(secretNamespaceNode);
                Node secretProviderNode = doc.createAttribute(Constants.SecureVault.ATTRIBUTE);
                secretProviderNode.setTextContent(Constants.SecureVault.SECRET_PROVIDER_CLASS);
                XPathFactory xpf = XPathFactory.newInstance();
                XPath xp = xpf.newXPath();
                XPathExpression xpathEx = xp.compile(xPath);
                NodeList securedNodes = (NodeList) xpathEx.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
                if (securedNodes != null && securedNodes.getLength() > 0) {
                    for (int i = 0; i < securedNodes.getLength(); i++) {
                        Node secretAliasNode = doc.createAttribute(Constants.SecureVault.ALIAS);
                        secretAliasNode.setTextContent(secretAlias);
                        Node node = securedNodes.item(i);
                        if (node != null) {
                            if (!encryptParamKey.isEmpty()) {
                                node.getAttributes().getNamedItem(encryptParamKey)
                                    .setNodeValue(Constants.SecureVault.PASSWORD);
                            } else {
                                node.setTextContent(Constants.SecureVault.PASSWORD);
                            }
                            node.getAttributes().setNamedItem(secretAliasNode);
                        }
                    }
                } else {
                    throw new CipherToolException(
                            "Element for secret alias '" + secretAlias + "' can not be found in " +
                            fileName + " file or You have entered invalid Xpath value");
                }
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File(filePath));
                transformer.transform(source, result);
            } catch (ParserConfigurationException e) {
                throw new CipherToolException(
                        "Error writing protected token [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (XPathExpressionException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (TransformerException e) {
                throw new CipherToolException(
                        "Error writing protected token [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (SAXException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            } catch (IOException e) {
                throw new CipherToolException(
                        "Error writing protected token  [" + secretAlias + "] to " + fileName + " file ", e);
            }
            System.out.println("Protected Token [" + secretAlias + "] is updated in " + fileName + " successfully\n");
        }
    }
private static void tokenToConfigFile ( String fileName , String xPath , String secretAlias , String encryptParamKey ) { if ( xPath != null && ! xPath . equals ( "" ) && secretAlias != null && ! secretAlias . equals ( "" ) ) { String filePath = Utils . getConfigFilePath ( fileName ) ; try { DocumentBuilderFactory docFactory = DocumentBuilderFactory . newInstance ( ) ; DocumentBuilder docBuilder = docFactory . newDocumentBuilder ( ) ; Document doc = docBuilder . parse ( filePath ) ; Node rootNode = doc . getDocumentElement ( ) ; Node secretNamespaceNode = doc . createAttribute ( Constants . SecureVault . NS_PREFIX ) ; secretNamespaceNode . setTextContent ( Constants . SecureVault . NS ) ; rootNode . getAttributes ( ) . setNamedItem ( secretNamespaceNode ) ; Node secretProviderNode = doc . createAttribute ( Constants . SecureVault . ATTRIBUTE ) ; secretProviderNode . setTextContent ( Constants . SecureVault . SECRET_PROVIDER_CLASS ) ; XPathFactory xpf = XPathFactory . newInstance ( ) ; XPath xp = xpf . newXPath ( ) ; XPathExpression xpathEx = xp . compile ( xPath ) ; NodeList securedNodes = ( NodeList ) xpathEx . evaluate ( doc . getDocumentElement ( ) , XPathConstants . NODESET ) ; if ( securedNodes != null && securedNodes . getLength ( ) > 0 ) { for ( int i = 0 ; i < securedNodes . getLength ( ) ; i ++ ) { Node secretAliasNode = doc . createAttribute ( Constants . SecureVault . ALIAS ) ; secretAliasNode . setTextContent ( secretAlias ) ; Node node = securedNodes . item ( i ) ; if ( node != null ) { if ( ! encryptParamKey . isEmpty ( ) ) { node . getAttributes ( ) . getNamedItem ( encryptParamKey ) . setNodeValue ( Constants . SecureVault . PASSWORD ) ; } else { node . setTextContent ( Constants . SecureVault . PASSWORD ) ; } node . getAttributes ( ) . setNamedItem ( secretAliasNode ) ; } } } else { throw new CipherToolException ( "Element for secret alias '" + secretAlias + "' can not be found in " + fileName + " file or You have entered invalid Xpath value" ) ; } TransformerFactory transformerFactory = TransformerFactory . newInstance ( ) ; Transformer transformer = transformerFactory . newTransformer ( ) ; DOMSource source = new DOMSource ( doc ) ; StreamResult result = new StreamResult ( new File ( filePath ) ) ; transformer . transform ( source , result ) ; } catch ( ParserConfigurationException e ) { throw new CipherToolException ( "Error writing protected token [" + secretAlias + "] to " + fileName + " file " , e ) ; } catch ( XPathExpressionException e ) { throw new CipherToolException ( "Error writing protected token  [" + secretAlias + "] to " + fileName + " file " , e ) ; } catch ( TransformerException e ) { throw new CipherToolException ( "Error writing protected token [" + secretAlias + "] to " + fileName + " file " , e ) ; } catch ( SAXException e ) { throw new CipherToolException ( "Error writing protected token  [" + secretAlias + "] to " + fileName + " file " , e ) ; } catch ( IOException e ) { throw new CipherToolException ( "Error writing protected token  [" + secretAlias + "] to " + fileName + " file " , e ) ; } System . out . println ( "Protected Token [" + secretAlias + "] is updated in " + fileName + " successfully\n" ) ; } }
write the XML syntax to the configuration file,
write the XML syntax to the configuration file