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);
}
}
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()));
}
}
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;
}
}
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;
}
}
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);
}
Aggregations