Java Code Examples for org.apache.commons.jxpath.ri.QName#getPrefix()

The following examples show how to use org.apache.commons.jxpath.ri.QName#getPrefix() . 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: ConfigurationNodeIteratorChildren.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the list of selected sub nodes for a {@code NodeNameTest} with a
 * wildcard name.
 *
 * @param node the current node
 * @param name the name to be selected
 * @return the list with selected sub nodes
 */
private List<T> createSubNodeListForWildcardName(final T node, final QName name)
{
    final List<T> children = getNodeHandler().getChildren(node);
    if (name.getPrefix() == null)
    {
        return children;
    }
    final List<T> prefixChildren = new ArrayList<>(children.size());
    final String prefix = prefixName(name.getPrefix(), null);
    for (final T child : children)
    {
        if (StringUtils.startsWith(getNodeHandler().nodeName(child),
                prefix))
        {
            prefixChildren.add(child);
        }
    }
    return prefixChildren;
}
 
Example 2
Source File: BeanAttributeIterator.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new BeanAttributeIterator.
 * @param parent parent pointer
 * @param name name of this bean
 */
public BeanAttributeIterator(PropertyOwnerPointer parent, QName name) {
    super(
        parent,
        (name.getPrefix() == null
            && (name.getName() == null || name.getName().equals("*")))
            ? null
            : name.toString(),
        false,
        null);
    this.parent = parent;
    includeXmlLang =
        (name.getPrefix() != null && name.getPrefix().equals("xml"))
            && (name.getName().equals("lang")
            || name.getName().equals("*"));
}
 
Example 3
Source File: SimplePathInterpreter.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
/**
 * Get a NodeIterator.
 * @param context evaluation context
 * @param pointer owning pointer
 * @param step triggering step
 * @return NodeIterator
 */
private static NodeIterator getNodeIterator(
    EvalContext context,
    NodePointer pointer,
    Step step) {
    if (step.getAxis() == Compiler.AXIS_CHILD) {
        NodeTest nodeTest = step.getNodeTest();
        QName qname = ((NodeNameTest) nodeTest).getNodeName();
        String prefix = qname.getPrefix();
        if (prefix != null) {
            String namespaceURI = context.getJXPathContext()
                    .getNamespaceURI(prefix);
            nodeTest = new NodeNameTest(qname, namespaceURI);
        }
        return pointer.childIterator(nodeTest, false, null);
    }
    // else Compiler.AXIS_ATTRIBUTE
    if (!(step.getNodeTest() instanceof NodeNameTest)) {
        throw new UnsupportedOperationException(
            "Not supported node test for attributes: "
                + step.getNodeTest());
    }
    return pointer.attributeIterator(
        ((NodeNameTest) step.getNodeTest()).getNodeName());
}
 
Example 4
Source File: ConfigurationNodeIteratorAttribute.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Determines which attributes are selected based on the passed in node
 * name.
 *
 * @param parent the parent node pointer
 * @param name the name of the selected attribute
 * @return a list with the selected attributes
 */
private List<String> createAttributeDataList(
        final ConfigurationNodePointer<T> parent, final QName name)
{
    final List<String> result = new ArrayList<>();
    if (!WILDCARD.equals(name.getName()))
    {
        addAttributeData(parent, result, qualifiedName(name));
    }
    else
    {
        final Set<String> names =
                new LinkedHashSet<>(parent.getNodeHandler()
                        .getAttributes(parent.getConfigurationNode()));
        final String prefix =
                name.getPrefix() != null ? prefixName(name.getPrefix(),
                        null) : null;
        for (final String n : names)
        {
            if (prefix == null || StringUtils.startsWith(n, prefix))
            {
                addAttributeData(parent, result, n);
            }
        }
    }

    return result;
}
 
Example 5
Source File: DOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createChild(JXPathContext context, QName name, int index) {
    if (index == WHOLE_COLLECTION) {
        index = 0;
    }
    boolean success =
        getAbstractFactory(context).createObject(
            context,
            this,
            node,
            name.toString(),
            index);
    if (success) {
        NodeTest nodeTest;
        String prefix = name.getPrefix();
        String namespaceURI = prefix == null ? null : context
                .getNamespaceURI(prefix);
        nodeTest = new NodeNameTest(name, namespaceURI);

        NodeIterator it = childIterator(nodeTest, false, null);
        if (it != null && it.setPosition(index + 1)) {
            return it.getNodePointer();
        }
    }
    throw new JXPathAbstractFactoryException(
            "Factory could not create a child node for path: " + asPath()
                    + "/" + name + "[" + (index + 1) + "]");
}
 
Example 6
Source File: DOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createAttribute(JXPathContext context, QName name) {
    if (!(node instanceof Element)) {
        return super.createAttribute(context, name);
    }
    Element element = (Element) node;
    String prefix = name.getPrefix();
    if (prefix != null) {
        String ns = null;
        NamespaceResolver nsr = getNamespaceResolver();
        if (nsr != null) {
            ns = nsr.getNamespaceURI(prefix);
        }
        if (ns == null) {
            throw new JXPathException(
                "Unknown namespace prefix: " + prefix);
        }
        element.setAttributeNS(ns, name.toString(), "");
    }
    else {
        if (!element.hasAttribute(name.getName())) {
            element.setAttribute(name.getName(), "");
        }
    }
    NodeIterator it = attributeIterator(name);
    it.setPosition(1);
    return it.getNodePointer();
}
 
Example 7
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createChild(
    JXPathContext context,
    QName name,
    int index) {
    if (index == WHOLE_COLLECTION) {
        index = 0;
    }
    boolean success =
        getAbstractFactory(context).createObject(
            context,
            this,
            node,
            name.toString(),
            index);
    if (success) {
        NodeTest nodeTest;
        String prefix = name.getPrefix();
        String namespaceURI = prefix == null ? null : context
                .getNamespaceURI(prefix);
        nodeTest = new NodeNameTest(name, namespaceURI);

        NodeIterator it =
            childIterator(nodeTest, false, null);
        if (it != null && it.setPosition(index + 1)) {
            return it.getNodePointer();
        }
    }
    throw new JXPathAbstractFactoryException("Factory could not create "
            + "a child node for path: " + asPath() + "/" + name + "["
            + (index + 1) + "]");
}
 
Example 8
Source File: NodePointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if this Pointer matches the supplied NodeTest.
 * @param test the NodeTest to execute
 * @return true if a match
 */
public boolean testNode(NodeTest test) {
    if (test == null) {
        return true;
    }
    if (test instanceof NodeNameTest) {
        if (isContainer()) {
            return false;
        }
        NodeNameTest nodeNameTest = (NodeNameTest) test;
        QName testName = nodeNameTest.getNodeName();
        QName nodeName = getName();
        if (nodeName == null) {
            return false;
        }

        String testPrefix = testName.getPrefix();
        String nodePrefix = nodeName.getPrefix();
        if (!safeEquals(testPrefix, nodePrefix)) {
            String testNS = getNamespaceURI(testPrefix);
            String nodeNS = getNamespaceURI(nodePrefix);
            if (!safeEquals(testNS, nodeNS)) {
                return false;
            }
        }
        if (nodeNameTest.isWildcard()) {
            return true;
        }
        return testName.getName().equals(nodeName.getName());
    }
    return test instanceof NodeTypeTest
            && ((NodeTypeTest) test).getNodeType() == Compiler.NODE_TYPE_NODE && isNode();
}
 
Example 9
Source File: NamespaceContext.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public boolean nextNode() {
    super.setPosition(getCurrentPosition() + 1);
    if (!setStarted) {
        setStarted = true;
        if (!(nodeTest instanceof NodeNameTest)) {
            return false;
        }

        NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
        QName testName = nodeNameTest.getNodeName();
        if (testName.getPrefix() != null) {
            return false;
        }
        if (nodeNameTest.isWildcard()) {
            iterator =
                parentContext.getCurrentNodePointer().namespaceIterator();
        }
        else {
            currentNodePointer =
                parentContext.getCurrentNodePointer().namespacePointer(
                        testName.getName());
            return currentNodePointer != null;
        }
    }

    if (iterator == null) {
        return false;
    }
    if (!iterator.setPosition(iterator.getPosition() + 1)) {
        return false;
    }
    currentNodePointer = iterator.getNodePointer();
    return true;
}
 
Example 10
Source File: DOMNodePointer.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Test a Node.
 * @param node to test
 * @param test to execute
 * @return true if node passes test
 */
public static boolean testNode(Node node, NodeTest test) {
    if (test == null) {
        return true;
    }
    if (test instanceof NodeNameTest) {
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            return false;
        }

        NodeNameTest nodeNameTest = (NodeNameTest) test;
        QName testName = nodeNameTest.getNodeName();
        String namespaceURI = nodeNameTest.getNamespaceURI();
        boolean wildcard = nodeNameTest.isWildcard();
        String testPrefix = testName.getPrefix();
        if (wildcard && testPrefix == null) {
            return true;
        }
        if (wildcard
            || testName.getName()
                    .equals(DOMNodePointer.getLocalName(node))) {
            String nodeNS = DOMNodePointer.getNamespaceURI(node);
            return equalStrings(namespaceURI, nodeNS) || nodeNS == null
                    && equalStrings(testPrefix, getPrefix(node));
        }
        return false;
    }
    if (test instanceof NodeTypeTest) {
        int nodeType = node.getNodeType();
        switch (((NodeTypeTest) test).getNodeType()) {
            case Compiler.NODE_TYPE_NODE :
                return true;
            case Compiler.NODE_TYPE_TEXT :
                return nodeType == Node.CDATA_SECTION_NODE
                    || nodeType == Node.TEXT_NODE;
            case Compiler.NODE_TYPE_COMMENT :
                return nodeType == Node.COMMENT_NODE;
            case Compiler.NODE_TYPE_PI :
                return nodeType == Node.PROCESSING_INSTRUCTION_NODE;
            default:
                return false;
        }
    }
    if (test instanceof ProcessingInstructionTest
            && node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        String testPI = ((ProcessingInstructionTest) test).getTarget();
        String nodePI = ((ProcessingInstruction) node).getTarget();
        return testPI.equals(nodePI);
    }
    return false;
}
 
Example 11
Source File: JDOMNodePointer.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Execute test against node on behalf of pointer.
 * @param pointer Pointer
 * @param node to test
 * @param test to execute
 * @return true if node passes test
 */
public static boolean testNode(
    NodePointer pointer,
    Object node,
    NodeTest test) {
    if (test == null) {
        return true;
    }
    if (test instanceof NodeNameTest) {
        if (!(node instanceof Element)) {
            return false;
        }

        NodeNameTest nodeNameTest = (NodeNameTest) test;
        QName testName = nodeNameTest.getNodeName();
        String namespaceURI = nodeNameTest.getNamespaceURI();
        boolean wildcard = nodeNameTest.isWildcard();
        String testPrefix = testName.getPrefix();
        if (wildcard && testPrefix == null) {
            return true;
        }
        if (wildcard
            || testName.getName()
                    .equals(JDOMNodePointer.getLocalName(node))) {
            String nodeNS = JDOMNodePointer.getNamespaceURI(node);
            return equalStrings(namespaceURI, nodeNS) || nodeNS == null
                    && equalStrings(testPrefix, getPrefix(node));
        }
        return false;
    }
    if (test instanceof NodeTypeTest) {
        switch (((NodeTypeTest) test).getNodeType()) {
            case Compiler.NODE_TYPE_NODE :
                return true;
            case Compiler.NODE_TYPE_TEXT :
                return (node instanceof Text) || (node instanceof CDATA);
            case Compiler.NODE_TYPE_COMMENT :
                return node instanceof Comment;
            case Compiler.NODE_TYPE_PI :
                return node instanceof ProcessingInstruction;
            default:
                return false;
        }
    }
    if (test instanceof ProcessingInstructionTest && node instanceof ProcessingInstruction) {
        String testPI = ((ProcessingInstructionTest) test).getTarget();
        String nodePI = ((ProcessingInstruction) node).getTarget();
        return testPI.equals(nodePI);
    }
    return false;
}
 
Example 12
Source File: SimplePathInterpreter.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Learn whether <code>name</code> is a lang attribute.
 * @param name to compare
 * @return boolean
 */
private static boolean isLangAttribute(QName name) {
    return name.getPrefix() != null
        && name.getPrefix().equals("xml")
        && name.getName().equals("lang");
}
 
Example 13
Source File: Path.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * Different axes are serviced by different contexts. This method
 * allocates the right context for the supplied step.
 * @param context evaluation context
 * @param axis code
 * @param nodeTest node test
 * @return EvalContext
 */
protected EvalContext createContextForStep(
    EvalContext context,
    int axis,
    NodeTest nodeTest) {
    if (nodeTest instanceof NodeNameTest) {
        QName qname = ((NodeNameTest) nodeTest).getNodeName();
        String prefix = qname.getPrefix();
        if (prefix != null) {
            String namespaceURI = context.getJXPathContext()
                    .getNamespaceURI(prefix);
            nodeTest = new NodeNameTest(qname, namespaceURI);
        }
    }

    switch (axis) {
    case Compiler.AXIS_ANCESTOR :
        return new AncestorContext(context, false, nodeTest);
    case Compiler.AXIS_ANCESTOR_OR_SELF :
        return new AncestorContext(context, true, nodeTest);
    case Compiler.AXIS_ATTRIBUTE :
        return new AttributeContext(context, nodeTest);
    case Compiler.AXIS_CHILD :
        return new ChildContext(context, nodeTest, false, false);
    case Compiler.AXIS_DESCENDANT :
        return new DescendantContext(context, false, nodeTest);
    case Compiler.AXIS_DESCENDANT_OR_SELF :
        return new DescendantContext(context, true, nodeTest);
    case Compiler.AXIS_FOLLOWING :
        return new PrecedingOrFollowingContext(context, nodeTest, false);
    case Compiler.AXIS_FOLLOWING_SIBLING :
        return new ChildContext(context, nodeTest, true, false);
    case Compiler.AXIS_NAMESPACE :
        return new NamespaceContext(context, nodeTest);
    case Compiler.AXIS_PARENT :
        return new ParentContext(context, nodeTest);
    case Compiler.AXIS_PRECEDING :
        return new PrecedingOrFollowingContext(context, nodeTest, true);
    case Compiler.AXIS_PRECEDING_SIBLING :
        return new ChildContext(context, nodeTest, true, true);
    case Compiler.AXIS_SELF :
        return new SelfContext(context, nodeTest);
    default:
        return null; // Never happens
    }
}
 
Example 14
Source File: ConfigurationNodeIteratorBase.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the qualified name from the given {@code QName}. If the name has
 * no namespace, result is the simple name. Otherwise, the namespace prefix
 * is added.
 *
 * @param name the {@code QName}
 * @return the qualified name
 */
protected static String qualifiedName(final QName name)
{
    return name.getPrefix() == null ? name.getName() : prefixName(
            name.getPrefix(), name.getName());
}