List of usage examples for javax.xml.xpath XPathConstants NODE
QName NODE
To view the source code for javax.xml.xpath XPathConstants NODE.
Click Source Link
The XPath 1.0 NodeSet data type.
From source file:org.talend.dataquality.libraries.devops.ReleaseVersionBumper.java
private void bumpPomVersion() throws Exception { final String resourcePath = ReleaseVersionBumper.class.getResource(".").getFile(); final String projectRoot = new File(resourcePath).getParentFile().getParentFile().getParentFile() .getParentFile().getParentFile().getParentFile().getParentFile().getPath() + File.separator; String parentPomPath = "./pom.xml"; File inputFile = new File(projectRoot + parentPomPath); if (inputFile.exists()) { System.out.println("Updating: " + inputFile.getAbsolutePath()); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputFile); // replace version value of this project Node parentVersion = (Node) xPath.evaluate("/project/version", doc, XPathConstants.NODE); parentVersion.setTextContent(targetVersion); // replace property value of this project NodeList propertyNodes = ((Node) xPath.evaluate("/project/properties", doc, XPathConstants.NODE)) .getChildNodes();//from w w w.j a v a 2 s. co m for (int idx = 0; idx < propertyNodes.getLength(); idx++) { Node node = propertyNodes.item(idx); String propertyName = node.getNodeName(); String propertyValue = node.getTextContent(); if (propertyName.startsWith(DATAQUALITY_PREFIX)) { if (targetVersion.endsWith(SNAPSHOT_VERSION_SUFFIX) && propertyName.contains(SNAPSHOT_STRING)) { node.setTextContent(propertyValue.substring(0, 4) + microVersion); } else if (!targetVersion.endsWith(SNAPSHOT_VERSION_SUFFIX) && propertyName.contains(RELEASED_STRING)) { node.setTextContent(propertyValue.substring(0, 4) + microVersion); } } } // re-write pom.xml file xTransformer.transform(new DOMSource(doc), new StreamResult(inputFile)); // update manifest of this project Path manifestPath = Paths.get(inputFile.getParentFile().getAbsolutePath(), "META-INF", "MANIFEST.MF"); updateManifestVersion(manifestPath); // update modules NodeList moduleNodes = (NodeList) xPath.evaluate("/project/modules/module", doc, XPathConstants.NODESET); for (int idx = 0; idx < moduleNodes.getLength(); idx++) { String modulePath = moduleNodes.item(idx).getTextContent(); updateChildModules(new File(projectRoot + modulePath + "/pom.xml")); } } }
From source file:org.talend.dataquality.libraries.devops.ReleaseVersionBumper.java
private void updateChildModules(File inputFile) throws Exception { if (inputFile.exists()) { System.out.println("Updating: " + inputFile.getAbsolutePath()); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputFile); // replace parent version value Node parentVersion = (Node) xPath.evaluate("/project/parent/version", doc, XPathConstants.NODE); parentVersion.setTextContent(targetVersion); // replace project version value Node projectVersion = (Node) xPath.evaluate("/project/version", doc, XPathConstants.NODE); String projectVersionValue = projectVersion.getTextContent(); if (targetVersion.endsWith(SNAPSHOT_VERSION_SUFFIX)) { projectVersion.setTextContent(projectVersionValue.replace(RELEASED_STRING, SNAPSHOT_STRING)); } else {/*from w w w . j a va2 s.c om*/ projectVersion.setTextContent(projectVersionValue.replace(SNAPSHOT_STRING, RELEASED_STRING)); } // replace dependency version value NodeList nodes = (NodeList) xPath.evaluate("/project/dependencies/dependency/version", doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { Node node = nodes.item(idx); String depVersionvalue = node.getTextContent(); if (depVersionvalue.startsWith("${dataquality.")) { if (targetVersion.endsWith(SNAPSHOT_VERSION_SUFFIX)) { node.setTextContent(depVersionvalue.replace(RELEASED_STRING, SNAPSHOT_STRING)); } else { node.setTextContent(depVersionvalue.replace(SNAPSHOT_STRING, RELEASED_STRING)); } } } // re-write pom.xml file xTransformer.transform(new DOMSource(doc), new StreamResult(inputFile)); // update manifest file of child project Path manifestPath = Paths.get(inputFile.getParentFile().getAbsolutePath(), "META-INF", "MANIFEST.MF"); updateManifestVersion(manifestPath); } }
From source file:org.tmpotter.filters.TestFilterBase.java
/** * Remove version and toolname, then compare. * @param f1//from w ww .ja va2 s . c o m * @param f2 * @throws java.lang.Exception */ protected void compareTMX(File f1, File f2) throws Exception { XPathExpression exprVersion = XPathFactory.newInstance().newXPath() .compile("/tmx/header/@creationtoolversion"); XPathExpression exprTool = XPathFactory.newInstance().newXPath().compile("/tmx/header/@creationtool"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(TmxReader2.TMX_DTD_RESOLVER); Document doc1 = builder.parse(f1); Document doc2 = builder.parse(f2); Node n; n = (Node) exprVersion.evaluate(doc1, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprVersion.evaluate(doc2, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprTool.evaluate(doc1, XPathConstants.NODE); n.setNodeValue(""); n = (Node) exprTool.evaluate(doc2, XPathConstants.NODE); n.setNodeValue(""); Diff myDiff = DiffBuilder.compare(Input.from(doc1)).withTest(Input.from(doc2)).checkForSimilar() .ignoreWhitespace().build(); assertFalse(myDiff.hasDifferences()); }
From source file:org.unitedinternet.cosmo.calendar.hcalendar.HCalendarParser.java
/** * Finds nodes./* w w w .j a va 2 s. c o m*/ * @param expr The XPath expression. * @param context The context object. * @return The node. * @throws ParserException - if something is wrong this exception is thrown. */ private static Node findNode(XPathExpression expr, Object context) throws ParserException { try { return (Node) expr.evaluate(context, XPathConstants.NODE); } catch (XPathException e) { throw new ParserException("Unable to find node", -1, e); } }
From source file:org.wso2.carbon.andes.utils.DataSourceConfiguration.java
/** * This method will populate data source configurations by reading a XML file at the given * location. First it'll get the jndi configuration name from andes context instance and * compare that configuration name against given rdbms data sources. * Once relevant configuration node found it will call addToConfigurationMap method to * store configurations in hash maps.// www . ja v a 2s . c om * * @param filePath Path of the XML descriptor file * @throws org.wso2.carbon.andes.service.exception.ConfigurationException * throws exception if error occurs while reading the XML descriptor. */ public void loadDbConfiguration(String filePath) throws ConfigurationException { String[] dataSourceNameArray = new String[MessageBrokerDBUtil.NUMBER_OF_DATA_STORES]; dataSourceNameArray[MessageBrokerDBUtil.MESSAGE_STORE_DATA_SOURCE] = AndesContext.getInstance() .getStoreConfiguration().getMessageStoreProperties().getProperty(StoreConfiguration.DATA_SOURCE); dataSourceNameArray[MessageBrokerDBUtil.CONTEXT_STORE_DATA_SOURCE] = AndesContext.getInstance() .getStoreConfiguration().getContextStoreProperties().getProperty(StoreConfiguration.DATA_SOURCE); // if both data source configurations are equal only one data source configuration // will sourced to database if (dataSourceNameArray[MessageBrokerDBUtil.MESSAGE_STORE_DATA_SOURCE] .equals(dataSourceNameArray[MessageBrokerDBUtil.CONTEXT_STORE_DATA_SOURCE])) { dataSourceNameArray[MessageBrokerDBUtil.CONTEXT_STORE_DATA_SOURCE] = ""; } DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; if (log.isDebugEnabled()) { log.debug("load database configurations from data source xml file." + " file path : " + filePath); } try { builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(new FileInputStream(filePath)); if (!dataSourceNameArray[MessageBrokerDBUtil.MESSAGE_STORE_DATA_SOURCE].isEmpty()) { String xpathQuery = XPATH_DATASOURCE_CONFIGURATION + "'" + dataSourceNameArray[MessageBrokerDBUtil.MESSAGE_STORE_DATA_SOURCE] + XPATH_RDBMS_ATTRIBUTE; //read an xml node using xpath Node configurationNode = (Node) xPath.compile(xpathQuery).evaluate(xmlDocument, XPathConstants.NODE); addToConfigurationMap(configurationNode, messageStoreConfiguration); } if (!dataSourceNameArray[MessageBrokerDBUtil.CONTEXT_STORE_DATA_SOURCE].isEmpty()) { String xpathQuery = XPATH_DATASOURCE_CONFIGURATION + "'" + dataSourceNameArray[MessageBrokerDBUtil.CONTEXT_STORE_DATA_SOURCE] + XPATH_RDBMS_ATTRIBUTE; //read an xml node using xpath Node configurationNode = (Node) xPath.compile(xpathQuery).evaluate(xmlDocument, XPathConstants.NODE); addToConfigurationMap(configurationNode, contextStoreConfiguration); } } catch (ParserConfigurationException e) { log.error("Unexpected error occurred while parsing configuration.", e); throw new ConfigurationException("Unexpected error occurred while parsing" + " configuration: ", e); } catch (XPathException e) { log.error("Unexpected error occurred while parsing xml file content.", e); throw new ConfigurationException("Unexpected error occurred while parsing xml" + " file content: ", e); } catch (SAXException e) { log.error("Unexpected error occurred in XML parser.", e); throw new ConfigurationException("Unexpected error occurred in XML parser.", e); } catch (IOException e) { log.error("master data source xml file not found.", e); throw new ConfigurationException("master data source xml file not found.", e); } }
From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.RegistryXmlConfigurator.java
/** * Configure Cron tasks for file upload, file cleanup, throttling and API update * * @throws XPathExpressionException// www. j a v a 2s . c o m */ private void configureTasks() throws XPathExpressionException { //Check whether tasks element already existing and if so delete it Element tasks; if (doc.getElementsByTagName(TASKS).getLength() != 0) { Node taskNode = doc.getElementsByTagName(TASKS).item(0); taskNode.getParentNode().removeChild(taskNode); } tasks = doc.createElement(TASKS); //File Upload Task if (configProperties.containsKey(ConfigConstants.FILE_DATA_UPLOAD_TASK_ENABLED) && Boolean .parseBoolean(configProperties.getProperty(ConfigConstants.FILE_DATA_UPLOAD_TASK_ENABLED))) { Element fileUploadTask = doc.createElement(TASK); String className = configProperties.getProperty(ConfigConstants.FILE_DATA_UPLOAD_TASK_CLASS); className = (className != null && !className.isEmpty()) ? className : ConfigConstants.DEFAULT_FILE_DATA_UPLOAD_TASK_CLASS; fileUploadTask.setAttribute(NAME, className.substring(className.lastIndexOf(".") + 1, className.length())); fileUploadTask.setAttribute(CLASS, className); Element fileUploadTrigger = doc.createElement(TRIGGER); fileUploadTrigger.setAttribute(CRON, configProperties.getProperty(ConfigConstants.FILE_DATA_UPLOAD_TASK_CRON)); fileUploadTask.appendChild(fileUploadTrigger); tasks.appendChild(fileUploadTask); } //File Cleanup Task if (configProperties.containsKey(ConfigConstants.FILE_DATA_CLEANUP_TASK_ENABLED) && Boolean .parseBoolean(configProperties.getProperty(ConfigConstants.FILE_DATA_CLEANUP_TASK_ENABLED))) { Element fileCleanupTask = doc.createElement(TASK); String className = configProperties.getProperty(ConfigConstants.FILE_DATA_CLEANUP_TASK_CLASS); className = (className != null && !className.isEmpty()) ? className : ConfigConstants.DEFAULT_FILE_DATA_CLEANUP_TASK_CLASS; fileCleanupTask.setAttribute(NAME, className.substring(className.lastIndexOf(".") + 1, className.length())); fileCleanupTask.setAttribute(CLASS, className); Element fileCleanupTrigger = doc.createElement(TRIGGER); fileCleanupTrigger.setAttribute(CRON, configProperties.getProperty(ConfigConstants.FILE_DATA_CLEANUP_TASK_CRON)); Element fileCleanupProperty = doc.createElement(PROPERTY); fileCleanupProperty.setAttribute(KEY, FILE_RETENTION_DAYS); fileCleanupProperty.setAttribute(VALUE, configProperties.getProperty(ConfigConstants.FILE_DATA_RETENTION_DAYS)); fileCleanupTask.appendChild(fileCleanupTrigger); fileCleanupTask.appendChild(fileCleanupProperty); tasks.appendChild(fileCleanupTask); XPath xpath = XPathFactory.newInstance().newXPath(); Node registryRootNode = (Node) xpath.evaluate("//wso2registry", doc, XPathConstants.NODE); registryRootNode.appendChild(tasks); } //Throttling Synchronization Task if (configProperties.containsKey(ConfigConstants.THROTTLING_SYNC_TASK_ENABLED) && Boolean .parseBoolean(configProperties.getProperty(ConfigConstants.THROTTLING_SYNC_TASK_ENABLED))) { Element fileUploadTask = doc.createElement(TASK); String className = configProperties.getProperty(ConfigConstants.THROTTLING_SYNC_TASK_CLASS); className = (className != null && !className.isEmpty()) ? className : ConfigConstants.DEFAULT_THROTTLING_SYNC_TASK_CLASS; fileUploadTask.setAttribute(NAME, className.substring(className.lastIndexOf(".") + 1, className.length())); fileUploadTask.setAttribute(CLASS, className); Element fileUploadTrigger = doc.createElement(TRIGGER); fileUploadTrigger.setAttribute(CRON, configProperties.getProperty(ConfigConstants.THROTTLING_SYNC_TASK_CRON)); fileUploadTask.appendChild(fileUploadTrigger); tasks.appendChild(fileUploadTask); } //API Update Task if (configProperties.containsKey(ConfigConstants.API_UPDATE_TASK_ENABLED) && Boolean.parseBoolean(configProperties.getProperty(ConfigConstants.API_UPDATE_TASK_ENABLED))) { Element apiUpdateTask = doc.createElement(TASK); String className = configProperties.getProperty(ConfigConstants.API_UPDATE_TASK_CLASS); className = (className != null && !className.isEmpty()) ? className : ConfigConstants.DEFAULT_API_UPDATE_TASK_CLASS; apiUpdateTask.setAttribute(NAME, className.substring(className.lastIndexOf(".") + 1, className.length())); apiUpdateTask.setAttribute(CLASS, className); Element fileUploadTrigger = doc.createElement(TRIGGER); fileUploadTrigger.setAttribute(CRON, configProperties.getProperty(ConfigConstants.API_UPDATE_TASK_CRON)); apiUpdateTask.appendChild(fileUploadTrigger); tasks.appendChild(apiUpdateTask); } }
From source file:org.wso2.carbon.apimgt.hybrid.gateway.configurator.XmlConfigurator.java
/** * Replace values in given Document/*from w w w.java2 s . c o m*/ * * @param doc Document * @param xpathMap Map<String, String> * @param configProperties Properties * @throws XPathExpressionException */ private void replaceValues(Document doc, Map<String, String> xpathMap, Properties configProperties) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); for (Map.Entry entry : xpathMap.entrySet()) { String xpathKey = (String) entry.getKey(); String replaceKey = (String) entry.getValue(); if (configProperties.containsKey(replaceKey) && configProperties.getProperty(replaceKey) != null && !configProperties.getProperty(replaceKey).isEmpty()) { Node node = (Node) xpath.evaluate("//" + xpathKey.replace(".", "/"), doc, XPathConstants.NODE); node.setTextContent(configProperties.getProperty(replaceKey)); } } }
From source file:org.wso2.carbon.automation.engine.configurations.ConfigurationErrorChecker.java
/** * get Node from the configurations/* www .j a v a2s . c om*/ * * @param xmlDocument * @param expression * @return * @throws XPathExpressionException */ private static Node getConfigurationNode(Document xmlDocument, String expression) throws XPathExpressionException { XPath xPath = XPathFactory.newInstance().newXPath(); return (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE); }
From source file:org.wso2.carbon.bpmn.core.types.datatypes.xml.api.XMLDocument.java
/** * Function to evaluate xPath query, and return specified return type * * @param xpathStr xpath expression to evaluate * @param returnType The desired return type of xpath evaluation. Supported retrun types : "NODESET", "NODE", "STRING", "NUMBER", "BOOLEAN" * @return result of xpath evaluation in specified return type * @throws BPMNXmlException/*from w w w. j a va2 s. co m*/ * @throws XPathExpressionException */ public Object xPath(String xpathStr, String returnType) throws BPMNXmlException, XPathExpressionException { if (returnType.equals(XPathConstants.NODESET.getLocalPart())) { Utils.evaluateXPath(doc, xpathStr, XPathConstants.NODESET); } else if (returnType.equals(XPathConstants.NODE.getLocalPart())) { Utils.evaluateXPath(doc, xpathStr, XPathConstants.NODE); } else if (returnType.equals(XPathConstants.STRING.getLocalPart())) { Utils.evaluateXPath(doc, xpathStr, XPathConstants.STRING); } else if (returnType.equals(XPathConstants.NUMBER.getLocalPart())) { Utils.evaluateXPath(doc, xpathStr, XPathConstants.NUMBER); } else if (returnType.equals(XPathConstants.BOOLEAN.getLocalPart())) { Utils.evaluateXPath(doc, xpathStr, XPathConstants.BOOLEAN); } else { //Unknown return type throw new BPMNXmlException("Unknown return type : " + returnType); } return null; }
From source file:org.wso2.carbon.humantask.core.api.rendering.HTRenderingApiImpl.java
/** * Function to evaluate xpath and retrieve String of the result * * @param xPathExpression/*from ww w . j a va 2s .c o m*/ * @param xPathExpression * @param nsReferenceDoc * @return result of the XPath expression if evaluation success, Otherwise return null * @throws IOException * @throws SAXException * @throws IllegalAccessFault * @throws IllegalArgumentFault * @throws IllegalStateFault * @throws IllegalOperationFault * @throws XPathExpressionException */ private String evaluateXPath(String xPathExpression, Element targetXmlElement, Document nsReferenceDoc) throws IOException, SAXException, IllegalAccessFault, IllegalArgumentFault, IllegalStateFault, IllegalOperationFault, XPathExpressionException { if (xPathExpression.length() > 0) { //Evaluate XPath XPath xPath = XPathFactory.newInstance().newXPath(); NamespaceResolver nsResolver = new NamespaceResolver(nsReferenceDoc); xPath.setNamespaceContext(nsResolver); Node result = (Node) xPath.evaluate(xPathExpression, targetXmlElement, XPathConstants.NODE); if (result != null && !result.getFirstChild().hasChildNodes()) { return result.getTextContent(); } } return null; }