/** * Zet xpath namespace ctx & initialiseer database met afnemervoorbeeld schema. */ @PostConstruct //heeft shutdown hook // public void naConstructie() { final SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.bindNamespaceUri("brp", "http://www.bzk.nl/brp/brp0200"); xPath.setNamespaceContext(nsContext); LOGGER.warn("Berichten opslaan in database: {}", isDatabasePersistent); if (isDatabasePersistent) { final ClassPathXmlApplicationContext cp = new ClassPathXmlApplicationContext("afnemervoorbeeld-databasepersist-context.xml"); cp.registerShutdownHook(); jdbcTemplate = (JdbcTemplate) cp.getBean("jdbcTemplate"); } }
public static XPathExpression createXPathExpression(String expression) { SimpleNamespaceContext namespaces = new SimpleNamespaceContext(); // NOTE: These are the internal prefixes used in XPath expression namespaces.bindNamespaceUri("wfs", NS_URL_WFS); namespaces.bindNamespaceUri("xlink", NS_URL_XLINK); namespaces.bindNamespaceUri("gml", NS_URL_GML); namespaces.bindNamespaceUri("ogc", NS_URL_OGC); namespaces.bindNamespaceUri("ktjkiiwfs", NS_URL_KTJ_KIINTEISTO); namespaces.bindDefaultNamespaceUri(NS_URL_KTJ_KIINTEISTO); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(namespaces); try { return xpath.compile(expression); } catch (XPathExpressionException e) { throw new RuntimeException(e); } }
/** * Gets the target language of the XLIFF by looking at the first "file" * element. * * @param xliffContent xliff content from which to extract the target language * @return the target language or {@code null} if not found */ public String getTargetLanguage(String xliffContent) { String targetLanguage = null; InputSource inputSource = new InputSource(new StringReader(xliffContent)); XPath xPath = XPathFactory.newInstance().newXPath(); SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.bindNamespaceUri("xlf", "urn:oasis:names:tc:xliff:document:1.2"); xPath.setNamespaceContext(simpleNamespaceContext); try { Node node = (Node) xPath.evaluate("/xlf:xliff/xlf:file[1]/@target-language", inputSource, XPathConstants.NODE); if(node != null) { targetLanguage = node.getTextContent(); } } catch (XPathExpressionException xpee) { logger.debug("Can't extract target language from xliff", xpee); } return targetLanguage; }
/** * Select all nodes that are selected by this XPath expression. If multiple nodes match, * multiple nodes will be returned. Nodes will be returned in document-order, * @param path * @param node * @param namespaces Namespaces that need to be available in the xpath, where the key is the * prefix and the value the namespace URI * @return */ public static NodeList selectNodes(String path, Object node, Map<String, String> namespaces) { try { XPathFactory factory = XPathFactory.newInstance(); javax.xml.xpath.XPath xpath = factory.newXPath(); if (namespaces != null) { SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); bindUnboundedNamespaces(nsContext, namespaces); xpath.setNamespaceContext(nsContext); } return (NodeList) xpath.evaluate(path, node, XPathConstants.NODESET); } catch (Exception e) { throw new RuntimeException(e); } }
public static Node selectNode(String path, Object node, Map<String, String> namespaces) { try { XPathFactory factory = XPathFactory.newInstance(); javax.xml.xpath.XPath xpath = factory.newXPath(); if (namespaces != null) { SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); bindUnboundedNamespaces(nsContext, namespaces); xpath.setNamespaceContext(nsContext); } return (Node) xpath.evaluate(path, node, XPathConstants.NODE); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Return the text of a node, or the value of an attribute * @param path the XPath to execute * @param node the node, node-set or Context object for evaluation. This value can be null. */ public static String selectText(String path, Object node, Map<String, String> namespaces) { try { XPathFactory factory = XPathFactory.newInstance(); javax.xml.xpath.XPath xpath = factory.newXPath(); if (namespaces != null) { SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); bindUnboundedNamespaces(nsContext, namespaces); xpath.setNamespaceContext(nsContext); } return (String) xpath.evaluate(path, node, XPathConstants.STRING); } catch (Exception e) { throw new RuntimeException(e); } }
protected List<String> parseToUrlList(String responseBody) throws ParseUrlException { try { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); SimpleNamespaceContext namespaces = new SimpleNamespaceContext(); Map<String, String> namespaceMap = new HashMap<>(); namespaceMap.put("x", "http://openrosa.org/xforms/xformsList"); namespaces.setBindings(namespaceMap); xPath.setNamespaceContext(namespaces); InputSource inputSource = new InputSource(new ByteArrayInputStream(responseBody.getBytes())); NodeList nodeList = (NodeList) xPath.compile("/x:xforms/x:xform/x:downloadUrl").evaluate(inputSource, XPathConstants.NODESET); List<String> urls = new ArrayList<>(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i).getFirstChild(); urls.add(node.getNodeValue()); } return urls; } catch (XPathExpressionException e) { throw new ParseUrlException(e); } }
@Override public <T> T translate(Node node, T value, TestContext context) { for (Map.Entry<String, String> expressionEntry : mappings.entrySet()) { String expression = expressionEntry.getKey(); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(context.getNamespaceContextBuilder().getNamespaceMappings()); NodeList findings = (NodeList) XPathUtils.evaluateExpression(node.getOwnerDocument(), expression, namespaceContext, XPathConstants.NODESET); if (findings != null && containsNode(findings, node)) { return convertIfNecessary(context.replaceDynamicContentInString(expressionEntry.getValue()), value); } } return value; }
private static XPathRoot transform(TransformationBuilder builder) { StreamResult result; SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); try { TransformerFactory transformerFactory = (TransformerFactory) Class.forName(XsltTransformer.PREFERRED_TRANSFORMER_FACTORY).newInstance(); Transformer transformer = transformerFactory.newTransformer( new StreamSource(XmlTransformationTestUtil.class.getResourceAsStream(builder.stylesheet))); for (Map.Entry<String, Object> mapEntry : builder.parameterMap.entrySet()) { transformer.setParameter(mapEntry.getKey(), mapEntry.getValue()); } result = new StreamResult(new StringWriter()); transformer.transform(new StreamSource(XmlTransformationTestUtil.class.getResourceAsStream(builder.source)), result); } catch (Exception e) { throw new GreenbirdTestException(e); } for (String key : builder.namespaceMap.keySet()) { namespaceContext.bindNamespaceUri(key, builder.namespaceMap.get(key)); } return XPathRoot.forString(result.getWriter().toString()).withNamespaceContext(namespaceContext); }
private static XPath initializeXPath() { final Map<String, String> mappings = new HashMap<>(); mappings.put(BRP_NAMESPACE_PREFIX.replace(":", ""), BRP_NAMESPACE_URI); final SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.setBindings(mappings); final XPathFactory xPathFactory = XPathFactory.newInstance(); final XPath xpath = xPathFactory.newXPath(); xpath.setNamespaceContext(simpleNamespaceContext); return xpath; }
private XPathExpression compileXpathExpression(String expression, Map<String, String> namespaces) throws XPathExpressionException { SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings((namespaces != null) ? namespaces : Collections.<String, String> emptyMap()); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(namespaceContext); return xpath.compile(expression); }
/** * Get the Citrus modules for this project based on the build dependencies. * @return */ public List<Module> getModules() { List<Module> modules = new ArrayList<>(); Collection<String> allModules = new SpringBeanNamespacePrefixMapper().getNamespaceMappings().values(); if (project.isMavenProject()) { try { String pomXml = FileUtils.readToString(new FileSystemResource(project.getMavenPomFile())); SimpleNamespaceContext nsContext = new SimpleNamespaceContext(); nsContext.bindNamespaceUri("mvn", "http://maven.apache.org/POM/4.0.0"); Document pomDoc = XMLUtils.parseMessagePayload(pomXml); NodeList dependencies = XPathUtils.evaluateAsNodeList(pomDoc, "/mvn:project/mvn:dependencies/mvn:dependency/mvn:artifactId[starts-with(., 'citrus-')]", nsContext); for (int i = 0; i < dependencies.getLength(); i++) { String moduleName = DomUtils.getTextValue((Element) dependencies.item(i)); if (moduleName.equals("citrus-core")) { allModules.remove("citrus"); } else { allModules.remove(moduleName); } modules.add(new Module(moduleName.substring("citrus-".length()), getActiveProject().getVersion(), true)); } } catch (IOException e) { throw new ApplicationRuntimeException("Unable to open Maven pom.xml file", e); } } allModules.stream() .filter(name -> !name.equals("citrus-test")) .map(name -> name.equals("citrus") ? "citrus-core" : name) .map(name -> new Module(name.substring("citrus-".length()), getActiveProject().getVersion(), false)) .forEach(modules::add); return modules; }
private static void bindUnboundedNamespaces(SimpleNamespaceContext nsContext, Map<String, String> namespaces) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { //making sure that namespace is not already bound. Otherwise UnsupportedException happens if(nsContext.getPrefix(entry.getValue()) == null) { nsContext.bindNamespaceUri(entry.getKey(), entry.getValue()); } } }
public XformParserODK() { XPathFactory xPathFactory = XPathFactory.newInstance(); this.xPath = xPathFactory.newXPath(); SimpleNamespaceContext namespaces = new SimpleNamespaceContext(); namespaces.setBindings(NAMESPACE_MAP); this.xPath.setNamespaceContext(namespaces); }
public XPathHelper() { xpath = XPathFactory .newInstance() .newXPath(); namespaceContext = new SimpleNamespaceContext(); xpath.setNamespaceContext(namespaceContext); }
/** * Evaluates Xpath expression on document and returns null safe result value as String representation. * @param document * @param expression * @param nsContext * @return */ private String evaluate(Document document, String expression, SimpleNamespaceContext nsContext) { Object result = XPathUtils.evaluateExpression(document, expression, nsContext, XPathConstants.STRING); return result != null ? result.toString() : ""; }