org.apache.commons.jxpath.ri.model.NodePointer Java Examples

The following examples show how to use org.apache.commons.jxpath.ri.model.NodePointer. 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: SimplePathInterpreterTest.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
private void assertNullPointer(String path, String expectedPath,
        String expectedSignature)
{
    Pointer pointer = context.getPointer(path);
    assertNotNull("Null path exists: " + path,
                pointer);
    assertEquals("Null path as path: " + path,
                expectedPath, pointer.asPath());
    assertEquals("Checking Signature: " + path,
                expectedSignature, pointerSignature(pointer));
            
    Pointer vPointer = ((NodePointer) pointer).getValuePointer();
    assertTrue("Null path is null: " + path,
                !((NodePointer) vPointer).isActual());
    assertEquals("Checking value pointer signature: " + path,
                expectedSignature + "N", pointerSignature(vPointer));
}
 
Example #2
Source File: TestConfigurationNodeIteratorChildren.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests defining a start node for the iteration.
 */
@Test
public void testIterateStartsWith()
{
    final ConfigurationNodePointer<ImmutableNode> childPointer =
            new ConfigurationNodePointer<>(rootPointer, root
                    .getChildren().get(2), handler);
    final ConfigurationNodeIteratorChildren<ImmutableNode> it =
            new ConfigurationNodeIteratorChildren<>(
                    rootPointer, null, false, childPointer);
    assertEquals("Wrong start position", 0, it.getPosition());
    final List<NodePointer> nodes = iterationElements(it);
    assertEquals("Wrong size of iteration", CHILD_COUNT - 3, nodes.size());
    int index = 4;
    for (final NodePointer np : nodes)
    {
        final ImmutableNode node = (ImmutableNode) np.getImmediateNode();
        assertEquals("Wrong node value", String.valueOf(index),
                node.getValue());
        index++;
    }
}
 
Example #3
Source File: InfoSetUtil.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the supplied object to String.
 * @param object to convert
 * @return String value
 */
public static String stringValue(Object object) {
    if (object instanceof String) {
        return (String) object;
    }
    if (object instanceof Number) {
        double d = ((Number) object).doubleValue();
        long l = ((Number) object).longValue();
        return d == l ? String.valueOf(l) : String.valueOf(d);
    }
    if (object instanceof Boolean) {
        return ((Boolean) object).booleanValue() ? "true" : "false";
    }
    if (object == null) {
        return "";
    }
    if (object instanceof NodePointer) {
        return stringValue(((NodePointer) object).getValue());
    }
    if (object instanceof EvalContext) {
        EvalContext ctx = (EvalContext) object;
        Pointer ptr = ctx.getSingleNodePointer();
        return ptr == null ? "" : stringValue(ptr);
    }
    return String.valueOf(object);
}
 
Example #4
Source File: ConfigurationNodePointerFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a node pointer for the specified bean. If the bean is a
 * configuration node, a corresponding pointer is returned.
 *
 * @param parent the parent node
 * @param name the name
 * @param bean the bean
 * @return a pointer for a configuration node if the bean is such a node
 */
@Override
@SuppressWarnings("unchecked")
/* Type casts are safe here, see above. Also, the hierarchy of node
   pointers is consistent, so a parent is compatible to a child.
 */
public NodePointer createNodePointer(final NodePointer parent, final QName name,
        final Object bean)
{
    if (bean instanceof NodeWrapper)
    {
        final NodeWrapper<?> wrapper = (NodeWrapper<?>) bean;
        return new ConfigurationNodePointer((ConfigurationNodePointer) parent,
                wrapper.getNode(), wrapper.getNodeHandler());
    }
    return null;
}
 
Example #5
Source File: ConfigurationNodePointerFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a node pointer for the specified bean. If the bean is a
 * configuration node (indicated by a wrapper object), a corresponding
 * pointer is returned.
 *
 * @param name the name of the node
 * @param bean the bean
 * @param locale the locale
 * @return a pointer for a configuration node if the bean is such a node
 */
@Override
@SuppressWarnings("unchecked")
/* Type casts are safe here; because of the way the NodeWrapper was
   constructed the node handler must be compatible with the node.
 */
public NodePointer createNodePointer(final QName name, final Object bean, final Locale locale)
{
    if (bean instanceof NodeWrapper)
    {
        final NodeWrapper<?> wrapper = (NodeWrapper<?>) bean;
        return new ConfigurationNodePointer(wrapper.getNode(),
                locale, wrapper.getNodeHandler());
    }
    return null;
}
 
Example #6
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 #7
Source File: DOMNamespaceIterator.java    From commons-jxpath with Apache License 2.0 6 votes vote down vote up
public NodePointer getNodePointer() {
    if (position == 0) {
        if (!setPosition(1)) {
            return null;
        }
        position = 0;
    }
    int index = position - 1;
    if (index < 0) {
        index = 0;
    }
    String prefix = "";
    Attr attr = (Attr) attributes.get(index);
    String name = attr.getPrefix();
    if (name != null && name.equals("xmlns")) {
        prefix = DOMNodePointer.getLocalName(attr);
    }
    return new NamespacePointer(parent, prefix, attr.getValue());
}
 
Example #8
Source File: ConfigurationNodePointer.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Compares two child node pointers.
 *
 * @param pointer1 one pointer
 * @param pointer2 another pointer
 * @return a flag, which pointer should be sorted first
 */
@Override
public int compareChildNodePointers(final NodePointer pointer1,
        final NodePointer pointer2)
{
    final Object node1 = pointer1.getBaseValue();
    final Object node2 = pointer2.getBaseValue();

    // sort based on the occurrence in the sub node list
    for (final T child : getNodeHandler().getChildren(node))
    {
        if (child == node1)
        {
            return -1;
        }
        else if (child == node2)
        {
            return 1;
        }
    }
    return 0; // should not happen
}
 
Example #9
Source File: JDOMNodeIterator.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer getNodePointer() {
    if (child == null) {
        if (!setPosition(1)) {
            return null;
        }
        position = 0;
    }

    return new JDOMNodePointer(parent, child);
}
 
Example #10
Source File: DynamicPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new DynamicPointer.
 * @param parent parent pointer
 * @param name property name
 * @param bean owning bean
 * @param handler DynamicPropertyHandler
 */
public DynamicPointer(NodePointer parent, QName name,
        Object bean, DynamicPropertyHandler handler) {
    super(parent);
    this.name = name;
    this.bean = bean;
    this.handler = handler;
}
 
Example #11
Source File: SimplePathInterpreter.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a "null pointer" that
 * a) represents the requested path and
 * b) can be used for creation of missing nodes in the path.
 * @param context evaluation context
 * @param parent parent pointer
 * @param steps path steps
 * @param currentStep step number
 * @return NodePointer
 */
public static NodePointer createNullPointer(
        EvalContext context, NodePointer parent, Step[] steps,
        int currentStep) {
    if (currentStep == steps.length) {
        return parent;
    }

    parent = valuePointer(parent);

    Step step = steps[currentStep];

    int axis = step.getAxis();
    if (axis == Compiler.AXIS_CHILD || axis == Compiler.AXIS_ATTRIBUTE) {
        NullPropertyPointer pointer = new NullPropertyPointer(parent);
        QName name = ((NodeNameTest) step.getNodeTest()).getNodeName();
        pointer.setPropertyName(name.toString());
        pointer.setAttribute(axis == Compiler.AXIS_ATTRIBUTE);
        parent = pointer;
    }
    // else { it is self::node() }

    Expression[] predicates = step.getPredicates();
    return createNullPointerForPredicates(
        context,
        parent,
        steps,
        currentStep,
        predicates,
        0);
}
 
Example #12
Source File: Path.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * The idea here is to return a NullPointer rather than null if that's at
 * all possible. Take for example this path: "//map/key". Let's say, "map"
 * is an existing node, but "key" is not there. We will create a
 * NullPointer that can be used to set/create the "key" property.
 * <p>
 * However, a path like "//key" would still produce null, because we have
 * no way of knowing where "key" would be if it existed.
 * </p>
 * <p>
 * To accomplish this, we first try the path itself. If it does not find
 * anything, we chop off last step of the path, as long as it is a simple
 * one like child:: or attribute:: and try to evaluate the truncated path.
 * If it finds exactly one node - create a NullPointer and return. If it
 * fails, chop off another step and repeat. If it finds more than one
 * location - return null.
 * </p>
 * @param context evaluation context
 * @return Pointer
 */
protected Pointer searchForPath(EvalContext context) {
    EvalContext ctx = buildContextChain(context, steps.length, true);
    Pointer pointer = ctx.getSingleNodePointer();

    if (pointer != null) {
        return pointer;
    }

    for (int i = steps.length; --i > 0;) {
        if (!isSimpleStep(steps[i])) {
            return null;
        }
        ctx = buildContextChain(context, i, true);
        if (ctx.hasNext()) {
            Pointer partial = (Pointer) ctx.next();
            if (ctx.hasNext()) {
                // If we find another location - the search is
                // ambiguous, so we report failure
                return null;
            }
            if (partial instanceof NodePointer) {
                return SimplePathInterpreter.createNullPointer(
                        context,
                        (NodePointer) partial,
                        steps,
                        i);
            }
        }
    }
    return null;
}
 
Example #13
Source File: PropertyPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a NodePointer that can be used to access the currently
 * selected property value.
 * @return NodePointer
 */
public NodePointer getImmediateValuePointer() {
    return NodePointer.newChildNodePointer(
        (NodePointer) this.clone(),
        getName(),
        getImmediateNode());
}
 
Example #14
Source File: PropertyPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createChild(
    JXPathContext context,
    QName name,
    int index,
    Object value) {
    PropertyPointer prop = (PropertyPointer) clone();
    if (name != null) {
        prop.setPropertyName(name.toString());
    }
    prop.setIndex(index);
    return prop.createPath(context, value);
}
 
Example #15
Source File: ConfigurationNodeIteratorBase.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the current node pointer.
 *
 * @return the current pointer in this iteration
 */
@Override
public NodePointer getNodePointer()
{
    if (getPosition() < 1 && !setPosition(1))
    {
        return null;
    }

    return createNodePointer(positionToIndex(getPosition()));
}
 
Example #16
Source File: PropertyPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createChild(
    JXPathContext context,
    QName name,
    int index) {
    PropertyPointer prop = (PropertyPointer) clone();
    if (name != null) {
        prop.setPropertyName(name.toString());
    }
    prop.setIndex(index);
    return prop.createPath(context);
}
 
Example #17
Source File: JDOMPointerFactory.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createNodePointer(
        NodePointer parent, QName name, Object bean) {
    if (bean instanceof Document) {
        return new JDOMNodePointer(parent, bean);
    }
    if (bean instanceof Element) {
        return new JDOMNodePointer(parent, bean);
    }
    return null;
}
 
Example #18
Source File: RootContext.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new RootContext.
 * @param jxpathContext context
 * @param pointer pointer
 */
public RootContext(JXPathContextReferenceImpl jxpathContext,
        NodePointer pointer) {
    super(null);
    this.jxpathContext = jxpathContext;
    this.pointer = pointer;
    if (pointer != null) {
        pointer.setNamespaceResolver(jxpathContext.getNamespaceResolver());
    }
}
 
Example #19
Source File: JXPathContextReferenceImpl.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Get the value indicated.
 * @param xpath String
 * @param expr Expression
 * @return Object
 */
public Object getValue(String xpath, Expression expr) {
    Object result = expr.computeValue(getEvalContext());
    if (result == null) {
        if (expr instanceof Path && !isLenient()) {
            throw new JXPathNotFoundException("No value for xpath: "
                    + xpath);
        }
        return null;
    }
    if (result instanceof EvalContext) {
        EvalContext ctx = (EvalContext) result;
        result = ctx.getSingleNodePointer();
        if (!isLenient() && result == null) {
            throw new JXPathNotFoundException("No value for xpath: "
                    + xpath);
        }
    }
    if (result instanceof NodePointer) {
        result = ((NodePointer) result).getValuePointer();
        if (!isLenient()) {
            NodePointer.verify((NodePointer) result);
        }
        result = ((NodePointer) result).getValue();
    }
    return result;
}
 
Example #20
Source File: NullPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createChild(
    JXPathContext context,
    QName name,
    int index,
    Object value) {
    return createPath(context).createChild(context, name, index, value);
}
 
Example #21
Source File: SimplePathInterpreter.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the pointer has an attribute called "name" and
 * its value is equal to the supplied string.
 * @param pointer input pointer
 * @param name name to check
 * @return boolean
 */
private static boolean isNameAttributeEqual(
    NodePointer pointer,
    String name) {
    NodeIterator it = pointer.attributeIterator(QNAME_NAME);
    return it != null
        && it.setPosition(1)
        && name.equals(it.getNodePointer().getValue());
}
 
Example #22
Source File: JXPathContextReferenceImpl.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * Get a pointer to the specified path/expression.
 * @param xpath String
 * @param expr compiled Expression
 * @return Pointer
 */
public Pointer getPointer(String xpath, Expression expr) {
    Object result = expr.computeValue(getEvalContext());
    if (result instanceof EvalContext) {
        result = ((EvalContext) result).getSingleNodePointer();
    }
    if (result instanceof Pointer) {
        if (!isLenient() && !((NodePointer) result).isActual()) {
            throw new JXPathNotFoundException("No pointer for xpath: "
                    + xpath);
        }
        return (Pointer) result;
    }
    return NodePointer.newNodePointer(null, result, getLocale());
}
 
Example #23
Source File: NullPointer.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createPath(JXPathContext context) {
    if (parent != null) {
        return parent.createPath(context).getValuePointer();
    }
    throw new UnsupportedOperationException(
        "Cannot create the root object: " + asPath());
}
 
Example #24
Source File: CoreFunction.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * key() implementation.
 * @param context evaluation context
 * @return various Object
 */
protected Object functionKey(EvalContext context) {
    assertArgCount(2);
    String key = InfoSetUtil.stringValue(getArg1().computeValue(context));
    Object value = getArg2().compute(context);
    EvalContext ec = null;
    if (value instanceof EvalContext) {
        ec = (EvalContext) value;
        if (ec.hasNext()) {
            value = ((NodePointer) ec.next()).getValue();
        }
        else { // empty context -> empty results
            return new NodeSetContext(context, new BasicNodeSet());
        }
    }
    JXPathContext jxpathContext = context.getJXPathContext();
    NodeSet nodeSet = jxpathContext.getNodeSetByKey(key, value);
    if (ec != null && ec.hasNext()) {
        BasicNodeSet accum = new BasicNodeSet();
        accum.add(nodeSet);
        while (ec.hasNext()) {
            value = ((NodePointer) ec.next()).getValue();
            accum.add(jxpathContext.getNodeSetByKey(key, value));
        }
        nodeSet = accum;
    }
    return new NodeSetContext(context, nodeSet);
}
 
Example #25
Source File: CoreFunction.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * id() implementation.
 * @param context evaluation context
 * @return Pointer
 */
protected Object functionID(EvalContext context) {
    assertArgCount(1);
    String id = InfoSetUtil.stringValue(getArg1().computeValue(context));
    JXPathContext jxpathContext = context.getJXPathContext();
    NodePointer pointer = (NodePointer) jxpathContext.getContextPointer();
    return pointer.getPointerByID(jxpathContext, id);
}
 
Example #26
Source File: PropertyIterator.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer getNodePointer() {
    if (position == 0) {
        if (name != null) {
            if (!targetReady) {
                prepareForIndividualProperty(name);
            }
            // If there is no such property - return null
            if (empty) {
                return null;
            }
        }
        else {
            if (!setPosition(1)) {
                return null;
            }
            reset();
        }
    }
    try {
        return propertyNodePointer.getValuePointer();
    }
    catch (Throwable t) {
        propertyNodePointer.handle(t);
        NullPropertyPointer npp =
            new NullPropertyPointer(
                    propertyNodePointer.getImmediateParentPointer());
        npp.setPropertyName(propertyNodePointer.getPropertyName());
        npp.setIndex(propertyNodePointer.getIndex());
        return npp.getValuePointer();
    }
}
 
Example #27
Source File: DynamicPointerFactory.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createNodePointer(
    QName name,
    Object bean,
    Locale locale) {
    JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
    if (bi.isDynamic()) {
        DynamicPropertyHandler handler =
            ValueUtils.getDynamicPropertyHandler(
                bi.getDynamicPropertyHandlerClass());
        return new DynamicPointer(name, bean, handler, locale);
    }
    return null;
}
 
Example #28
Source File: JDOMNamespaceIterator.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer getNodePointer() {
    if (position == 0) {
        if (!setPosition(1)) {
            return null;
        }
        position = 0;
    }
    int index = position - 1;
    if (index < 0) {
        index = 0;
    }
    Namespace ns = (Namespace) namespaces.get(index);
    return new JDOMNamespacePointer(parent, ns.getPrefix(), ns.getURI());
}
 
Example #29
Source File: SimplePathInterpreter.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
/**
 * A path that starts with a property owner. The method evaluates
 * the first predicate in a special way and then forwards to
 * a general predicate processing method.
 * @param context evaluation context
 * @param parentPointer parent pointer
 * @param steps path steps
 * @param currentStep step number
 * @return NodePointer
 */
private static NodePointer doStepPredicatesPropertyOwner(
        EvalContext context, PropertyOwnerPointer parentPointer,
        Step[] steps, int currentStep) {
    Step step = steps[currentStep];
    Expression[] predicates = step.getPredicates();

    NodePointer childPointer =
        createChildPointerForStep(parentPointer, step);
    if (!childPointer.isActual()) {
        // Property does not exist - return a null pointer
        return createNullPointer(
            context,
            parentPointer,
            steps,
            currentStep);
    }

    // Evaluate predicates
    return doPredicate(
        context,
        childPointer,
        steps,
        currentStep,
        predicates,
        0);
}
 
Example #30
Source File: BeanPointerFactory.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
public NodePointer createNodePointer(NodePointer parent, QName name,
        Object bean) {
    if (bean == null) {
        return new NullPointer(parent, name);
    }

    JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
    return new BeanPointer(parent, name, bean, bi);
}