List of usage examples for javax.xml.xpath XPath reset
public void reset();
From source file:Main.java
public static boolean updatePomDBConfig(String userName, String password, String name, String host, String port, String version, String pomPath) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, TransformerException { Document document = loadXML(pomPath); XPath path = XPathFactory.newInstance().newXPath(); XPathExpression express = path.compile("//project/properties/mysql.server.version"); NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); Node node = nodes.item(0);/*from w w w .j ava 2 s.c o m*/ node.setTextContent(version); path.reset(); express = path.compile("//project/properties/mysql.server.host"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(host); path.reset(); express = path.compile("//project/properties/mysql.server.port"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(port); path.reset(); express = path.compile("//project/properties/mysql.server.database"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(name); path.reset(); express = path.compile("//project/properties/mysql.server.user"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(userName); path.reset(); express = path.compile("//project/properties/mysql.server.password"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); node = nodes.item(0); node.setTextContent(password); path.reset(); return updateXML(document, pomPath); }
From source file:Main.java
public static boolean updateDBConfig(String userName, String password, String name, String driver, String url, String dbConfigPath) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException { Document document = loadXML(dbConfigPath); XPath path = XPathFactory.newInstance().newXPath(); XPathExpression express = path.compile( "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Set[@name='className']"); NodeList nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); Node node = nodes.item(0);/* w w w .j a va2 s . c o m*/ node.setTextContent(driver); path.reset(); express = path.compile( "//Configure/New[@class='org.eclipse.jetty.plus.jndi.Resource']/Arg/New[@class='bitronix.tm.resource.jdbc.PoolingDataSource']/Get['@name=driverProperties']/Put"); nodes = (NodeList) express.evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if ("user".equals(node.getAttributes().item(0).getTextContent())) { node.setTextContent(userName); } else if ("password".equals(node.getAttributes().item(0).getTextContent())) { node.setTextContent(password); } else if ("URL".equals(node.getAttributes().item(0).getTextContent())) { // String url = node.getTextContent(); // System.out.println("basic url ---> " + url + "; name -->" + name); // url = replaceDBName("examples", name, url); // System.out.println("dist url ---> " + url); node.setTextContent(url); } } path.reset(); return updateXML(document, dbConfigPath); }
From source file:com.honnix.jaxo.core.internal.pool.XPathObjectFactory.java
@Override public void passivateObject(XPath xpath) throws Exception { xpath.reset(); }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
private Document replaceIfExistingByXPaths(final Document originalDom, final Document modifiedDom, Map<String, String> xPathPairs) throws XPathExpressionException { Document finalDom = originalDom; Element finalDomRoot = (Element) finalDom.getFirstChild(); //Element modifiedDomRoot = (Element) modifiedDom.getFirstChild(); Element lastChild = null;//from w w w . j a v a 2s .c o m for (Entry<String, String> xPathPair : xPathPairs.entrySet()) { /** * basically, this is to copy the element specified in srcXPath, and * replace/add it to the position pointed by destXPath... */ String srcXPath = xPathPair.getKey(); logger.debug("srcXPath: " + srcXPath); String destXPath = xPathPair.getValue(); logger.debug("destXPath: " + destXPath); XPath xPath = getXPathInstance(); // find all the nodes specified by destXPath in the originalDom, and // delete them all NodeList existingNodeList = (NodeList) (xPath.evaluate(destXPath, finalDom, XPathConstants.NODESET)); xPath.reset(); // find all the nodes specified by srcXPath in the modifiedDom NodeList nodeList = (NodeList) (xPath.evaluate(srcXPath, modifiedDom, XPathConstants.NODESET)); int el = existingNodeList.getLength(); logger.debug("find '" + el + "' in originalDom using xPath: " + destXPath); int l = nodeList.getLength(); logger.debug("find '" + l + "' in modifiedXml using xPath: " + srcXPath); for (int i = 0; i < el; i++) { Node c = existingNodeList.item(i); //xPathExpression = xPath.compile(srcXPath); //NodeList srcNodeLst = (NodeList) (xPathExpression.evaluate( //modifiedDom, XPathConstants.NODESET)); //NodeList srcNodeLst = modifiedDomRoot.getElementsByTagName(c.getNodeName()); if (l > 0) { // remove this node from its parent... c.getParentNode().removeChild(c); logger.debug("Node:" + c.getNodeName() + " is removed!"); } } // create the node structure first. and return the last child of the // path... the right most node... lastChild = createElementStructureByPath(finalDomRoot, destXPath); List<String> nodeNameList = getNodeList(destXPath); String lastNodeName = nodeNameList.get(nodeNameList.size() - 1); Node currentNode = null; for (int i = 0; i < l; i++) { currentNode = nodeList.item(i); // the name of the last node in srcXPath might not be the same // as the name of the last node in destXPath Element lastElement = finalDom.createElement(lastNodeName); // NodeList currentNodeChildNodes = currentNode.getChildNodes(); // int s = currentNodeChildNodes.getLength(); // for(int j = 0; j < s; j++){ // lastElement.appendChild(finalDom.importNode(currentNodeChildNodes.item(j), // true)); // } if (currentNode.hasAttributes()) { NamedNodeMap attributes = currentNode.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { String attribute_name = attributes.item(j).getNodeName(); String attribute_value = attributes.item(j).getNodeValue(); lastElement.setAttribute(attribute_name, attribute_value); } } while (currentNode.hasChildNodes()) { Node kid = currentNode.getFirstChild(); currentNode.removeChild(kid); lastElement.appendChild(finalDom.importNode(kid, true)); } lastChild.appendChild(lastElement); } } return finalDom; }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
/** * replace elements in originalDom with modifiedDom according to listed * xPaths, if the originalDom has elements not listed in the xPath, it will * be kept untouched. in the HashMap<String, String> xPathPairs, the key is * the path in the source xml, and the value is the xpath for the final * note*: the if the xpath has attributes, it's not going to work... need to * do a custom implementation when that use case happened... * /* ww w. ja v a2 s . co m*/ * @param originalDom * @param modifiedDom * @param xPaths * @return * @throws XPathExpressionException */ private Document replaceByXPaths(final Document originalDom, final Document modifiedDom, Map<String, String> xPathPairs) throws XPathExpressionException { Document finalDom = originalDom; Element finalDomRoot = (Element) finalDom.getFirstChild(); Element lastChild = null; for (Entry<String, String> xPathPair : xPathPairs.entrySet()) { /** * basically, this is to copy the element specified in srcXPath, and * replace/add it to the position pointed by destXPath... */ String srcXPath = xPathPair.getKey(); logger.debug("srcXPath: " + srcXPath); String destXPath = xPathPair.getValue(); logger.debug("destXPath: " + destXPath); XPath xPath = getXPathInstance(); // find all the nodes specified by destXPath in the originalDom, and // delete them all NodeList existingNodeList = (NodeList) (xPath.evaluate(destXPath, finalDom, XPathConstants.NODESET)); int el = existingNodeList.getLength(); logger.debug("find '" + el + "' in originalDom using xPath: " + destXPath); for (int i = 0; i < el; i++) { Node c = existingNodeList.item(i); // remove this node from its parent... c.getParentNode().removeChild(c); } // create the node structure first. and return the last child of the // path... the right most node... lastChild = createElementStructureByPath(finalDomRoot, destXPath); List<String> nodeNameList = getNodeList(destXPath); String lastNodeName = nodeNameList.get(nodeNameList.size() - 1); xPath.reset(); // find all the nodes specified by srcXPath in the modifiedDom NodeList nodeList = (NodeList) (xPath.evaluate(srcXPath, modifiedDom, XPathConstants.NODESET)); int l = nodeList.getLength(); logger.debug("find '" + l + "' in modifiedXml using xPath: " + srcXPath); Node currentNode = null; for (int i = 0; i < l; i++) { currentNode = nodeList.item(i); // the name of the last node in srcXPath might not be the same // as the name of the last node in destXPath Element lastElement = finalDom.createElement(lastNodeName); // NodeList currentNodeChildNodes = currentNode.getChildNodes(); // int s = currentNodeChildNodes.getLength(); // for(int j = 0; j < s; j++){ // lastElement.appendChild(finalDom.importNode(currentNodeChildNodes.item(j), // true)); // } if (currentNode.hasAttributes()) { NamedNodeMap attributes = currentNode.getAttributes(); for (int j = 0; j < attributes.getLength(); j++) { String attribute_name = attributes.item(j).getNodeName(); String attribute_value = attributes.item(j).getNodeValue(); lastElement.setAttribute(attribute_name, attribute_value); } } while (currentNode.hasChildNodes()) { Node kid = currentNode.getFirstChild(); currentNode.removeChild(kid); lastElement.appendChild(finalDom.importNode(kid, true)); } lastChild.appendChild(lastElement); } } return finalDom; }