Search in sources :

Example 41 with XPath

use of javax.xml.xpath.XPath in project azure-tools-for-java by Microsoft.

the class AILibraryHandler method setFilterDef.

private void setFilterDef() {
    if (webXMLDoc == null) {
        return;
    }
    try {
        String exprFilter = message("aiExprConst");
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element eleFilter = (Element) xpath.evaluate(exprFilter, webXMLDoc, XPathConstants.NODE);
        if (eleFilter == null) {
            Element filter = webXMLDoc.createElement(message("filterTag"));
            Element filterName = webXMLDoc.createElement(message("filterEle"));
            filterName.setTextContent(message("aiWebfilter"));
            filter.appendChild(filterName);
            Element fClass = webXMLDoc.createElement("filter-class");
            fClass.setTextContent(message("aiWebFilterClassName"));
            filter.appendChild(fClass);
            NodeList existingFilterNodeList = webXMLDoc.getElementsByTagName(message("filterTag"));
            Node existingFilterNode = existingFilterNodeList != null & existingFilterNodeList.getLength() > 0 ? existingFilterNodeList.item(0) : null;
            webXMLDoc.getDocumentElement().insertBefore(filter, existingFilterNode);
        }
    } catch (Exception ex) {
        AzurePlugin.log(ex.getMessage(), ex);
    }
}
Also used : XPath(javax.xml.xpath.XPath) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException)

Example 42 with XPath

use of javax.xml.xpath.XPath in project azure-tools-for-java by Microsoft.

the class AILibraryHandler method removeAIFilterDef.

public void removeAIFilterDef() throws Exception {
    if (webXMLDoc == null) {
        return;
    }
    try {
        String exprFilter = message("aiExprConst");
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element eleFilter = (Element) xpath.evaluate(exprFilter, webXMLDoc, XPathConstants.NODE);
        if (eleFilter != null) {
            eleFilter.getParentNode().removeChild(eleFilter);
        }
        String exprFltMapping = message("exprFltMapping");
        Element eleFilMapping = (Element) xpath.evaluate(exprFltMapping, webXMLDoc, XPathConstants.NODE);
        if (eleFilMapping != null) {
            eleFilMapping.getParentNode().removeChild(eleFilMapping);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        AzurePlugin.log(ex.getMessage(), ex);
        throw new Exception(String.format("%s%s", message("aiRemoveErr"), ex.getMessage()));
    }
}
Also used : XPath(javax.xml.xpath.XPath) Element(org.w3c.dom.Element) IOException(java.io.IOException)

Example 43 with XPath

use of javax.xml.xpath.XPath in project azure-tools-for-java by Microsoft.

the class ParserXMLUtility method createElement.

/** Generic API to update or create DOM elements */
public static Element createElement(Document doc, String expr, Element parentElement, String elementName, boolean firstChild, Map<String, String> attributes) throws Exception {
    if (doc == null) {
        throw new IllegalArgumentException(INVALID_ARG);
    } else {
        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = null;
        if (expr != null)
            element = (Element) xPath.evaluate(expr, doc, XPathConstants.NODE);
        // If element doesn't exist create one
        if (element == null) {
            element = doc.createElement(elementName);
            if (firstChild) {
                parentElement.insertBefore(element, parentElement != null ? parentElement.getFirstChild() : null);
            } else {
                parentElement.appendChild(element);
            }
        }
        if (attributes != null && !attributes.isEmpty()) {
            for (Map.Entry<String, String> attribute : attributes.entrySet()) {
                element.setAttribute(attribute.getKey(), attribute.getValue());
            }
        }
        return element;
    }
}
Also used : XPath(javax.xml.xpath.XPath) Element(org.w3c.dom.Element) Map(java.util.Map)

Example 44 with XPath

use of javax.xml.xpath.XPath in project azure-tools-for-java by Microsoft.

the class ParserXMLUtility method updateOrCreateElement.

/** Generic API to update or create DOM elements */
public static Element updateOrCreateElement(Document doc, String expr, String parentNodeExpr, String elementName, boolean firstChild, Map<String, String> attributes) throws Exception {
    if (doc == null) {
        throw new IllegalArgumentException(INVALID_ARG);
    } else {
        XPath xPath = XPathFactory.newInstance().newXPath();
        Element element = null;
        if (expr != null)
            element = (Element) xPath.evaluate(expr, doc, XPathConstants.NODE);
        // If element doesn't exist create one
        if (element == null) {
            element = doc.createElement(elementName);
            Element parentElement = (Element) xPath.evaluate(parentNodeExpr, doc, XPathConstants.NODE);
            if (firstChild) {
                parentElement.insertBefore(element, parentElement != null ? parentElement.getFirstChild() : null);
            } else {
                parentElement.appendChild(element);
            }
        }
        if (attributes != null && !attributes.isEmpty()) {
            for (Map.Entry<String, String> attribute : attributes.entrySet()) {
                element.setAttribute(attribute.getKey(), attribute.getValue());
            }
        }
        return element;
    }
}
Also used : XPath(javax.xml.xpath.XPath) Element(org.w3c.dom.Element) Map(java.util.Map)

Example 45 with XPath

use of javax.xml.xpath.XPath in project azure-tools-for-java by Microsoft.

the class XMLHelper method main.

public static void main(String[] argv) {
    if (argv.length < 4) {
        System.out.println("[Failed] Please specify the arguments $XML_FILE $XPATH $NEW_VALUE $JOIN_OR_REPLACE.");
        System.exit(1);
    }
    String FILEPATH = argv[0];
    String XPATH = argv[1];
    String NEW_VALUE = argv[2];
    String CHANGETYPE = argv[3];
    boolean DISPLAY_LOG = false;
    if (argv.length > 4 && argv[4].toLowerCase().equals("true")) {
        DISPLAY_LOG = true;
    }
    try {
        System.out.println("Starting to modify XML " + FILEPATH);
        // Read the content from xml file
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(FILEPATH);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expression = xpath.compile(XPATH);
        Node targetNode = (Node) expression.evaluate(doc, XPathConstants.NODE);
        String oldValue = targetNode.getNodeValue();
        String newValue = "";
        if (CHANGETYPE.equals("JOIN")) {
            newValue = oldValue + NEW_VALUE;
        }
        if (DISPLAY_LOG) {
            System.out.println("The old value is " + oldValue);
            System.out.println("The new value is " + newValue);
        }
        targetNode.setNodeValue(newValue);
        // Write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(FILEPATH));
        transformer.transform(source, result);
        System.out.println("Modify XML Finished.");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Aggregations

XPath (javax.xml.xpath.XPath)197 Document (org.w3c.dom.Document)107 NodeList (org.w3c.dom.NodeList)99 XPathExpressionException (javax.xml.xpath.XPathExpressionException)70 Node (org.w3c.dom.Node)70 XPathExpression (javax.xml.xpath.XPathExpression)69 XPathFactory (javax.xml.xpath.XPathFactory)59 DocumentBuilder (javax.xml.parsers.DocumentBuilder)45 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 IOException (java.io.IOException)32 Element (org.w3c.dom.Element)32 Test (org.junit.Test)25 InputSource (org.xml.sax.InputSource)23 SAXException (org.xml.sax.SAXException)21 File (java.io.File)19 ArrayList (java.util.ArrayList)19 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 HashMap (java.util.HashMap)16 InputStream (java.io.InputStream)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11