Example usage for org.apache.commons.jxpath JXPathContext getNamespaceURI

List of usage examples for org.apache.commons.jxpath JXPathContext getNamespaceURI

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext getNamespaceURI.

Prototype

public String getNamespaceURI(String prefix) 

Source Link

Document

Given a prefix, returns a registered namespace URI.

Usage

From source file:org.xchain.javassist.SimpleCommand.java

public boolean execute(JXPathContext context) throws Exception {
    String testNamespace = context.getNamespaceURI("test");
    context.getVariables().declareVariable("namespace", testNamespace);
    return true;//  w w w. java2 s .  c o  m
}

From source file:org.xchain.namespaces.jsl.AbstractTemplateCommand.java

/**
 * Uses the specified nameXPath and namespaceXPath to create a dynamic qName.
 *///from w ww.  j  a v a2s .  com
protected QName dynamicQName(JXPathContext context, String nameXPath, String namespaceXPath,
        boolean includeDefaultPrefix) throws SAXException {
    String name = (String) context.getValue(nameXPath, String.class);
    if (name == null) {
        throw new SAXException("QNames cannot have null names.");
    }
    String namespace = null;
    if (namespaceXPath != null) {
        namespace = (String) context.getValue(namespaceXPath, String.class);
        if (namespace == null) {
            throw new SAXException("Namespace uris cannot be null.");
        }
    }

    String[] parts = name.split(":", 2);
    String prefixPart = parts.length == 2 ? parts[0] : "";
    String localPart = parts.length == 2 ? parts[1] : parts[0];

    // if the prefix is not "", then it must be defined in the context.
    String prefixPartNamespace = (includeDefaultPrefix || !"".equals(prefixPart))
            ? context.getNamespaceURI(prefixPart)
            : "";
    if (!"".equals(prefixPart) && prefixPartNamespace == null) {
        throw new SAXException("The prefix '" + prefixPart + "' is not defined in the context.");
    }

    // ASSERT: if the prefix is not "", then it is defined in the context.

    // if the namespace is null, then the prefix defines the namespace.
    if (namespace == null) {
        return new QName(prefixPartNamespace != null ? prefixPartNamespace : "", localPart, prefixPart);
    }

    // ASSERT: the namespace was specified.

    // if the prefix is "", then we can lookup the proper namespace.
    if ("".equals(prefixPart)) {
        if (!namespace.equals(prefixPartNamespace)) {
            String namespacePrefix = ((ScopedJXPathContextImpl) context).getPrefix(namespace);
            if (namespacePrefix == null) {
                throw new SAXException("The namespace '" + namespace + "' is not bound to a prefix.");
            }
            if (!includeDefaultPrefix && "".equals(namespacePrefix)) {
                throw new SAXException("The namespace '" + namespace + "' cannot be used for the attribute '"
                        + name + "', because it maps to the default namespace.");
            }
            return new QName(namespace, localPart, namespacePrefix);
        } else {
            return new QName(namespace, localPart, prefixPart);
        }
    }

    // ASSERT: the prefix is defined and the namespace is defined, if they do not match, we must fail.
    if (!namespace.equals(prefixPartNamespace)) {
        throw new SAXException("The prefix '" + prefixPart + "' is bound to '" + prefixPartNamespace
                + "', but the namespace '" + namespace + "' is required.");
    }

    // ASSERT: the namespace and prefix will work together.
    return new QName(namespace, localPart, prefixPart);
}

From source file:org.xchain.test.namespace.CopyNamespaceCommand.java

public boolean execute(JXPathContext context) throws Exception {
    ((ScopedQNameVariables) context.getVariables()).declareVariable(getVariable(context),
            context.getNamespaceURI(getPrefix(context)), Scope.request);
    return false;
}