org.springframework.util.xml.SimpleNamespaceContext Java Examples

The following examples show how to use org.springframework.util.xml.SimpleNamespaceContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XliffUtils.java    From mojito with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #2
Source File: InboundXmlDataDictionary.java    From citrus-simulator with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: XpathExpectationsHelper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static XPathExpression compileXpathExpression(String expression,
		@Nullable Map<String, String> namespaces) throws XPathExpressionException {

	SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
	namespaceContext.setBindings(namespaces != null ? namespaces : Collections.emptyMap());
	XPath xpath = XPathFactory.newInstance().newXPath();
	xpath.setNamespaceContext(namespaceContext);
	return xpath.compile(expression);
}
 
Example #4
Source File: XpathExpectationsHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static XPathExpression compileXpathExpression(String expression,
		@Nullable Map<String, String> namespaces) throws XPathExpressionException {

	SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
	namespaceContext.setBindings(namespaces != null ? namespaces : Collections.emptyMap());
	XPath xpath = XPathFactory.newInstance().newXPath();
	xpath.setNamespaceContext(namespaceContext);
	return xpath.compile(expression);
}
 
Example #5
Source File: XpathExpectationsHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #6
Source File: ProjectService.java    From citrus-admin with Apache License 2.0 5 votes vote down vote up
/**
 * 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()), project.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()), project.getVersion(), false))
            .forEach(modules::add);

    return modules;
}
 
Example #7
Source File: ProjectService.java    From citrus-admin with Apache License 2.0 2 votes vote down vote up
/**
 * 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() : "";
}