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

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

Introduction

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

Prototype

public static JXPathContext newContext(Object contextBean) 

Source Link

Document

Creates a new JXPathContext with the specified object as the root node.

Usage

From source file:blueprint.sdk.util.config.Config.java

/**
 * @param uri path/uri of config xml/*from  w ww  .j  a v  a 2 s . co  m*/
 * @throws ConfigException load failure
 */
public void load(String uri) throws ConfigException {
    synchronized (this) {
        this.uri = uri;

        if (builder == null) {
            try {
                builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                throw new ConfigException("XML parser error", e);
            }
        }
    }

    try {
        root = builder.parse(uri);
        context = JXPathContext.newContext(root);
    } catch (Exception e) {
        throw new ConfigException("Can't parse config xml", e);
    }

    String configName = JXPathHelper.evaluate("config/@name", context);
    if (Validator.isEmpty(configName)) {
        configName = "no-name";
    }

    L.info("configuration '" + configName + "' is loaded");
}

From source file:com.tunguski.xmlbeans.JXPath.java

/**
 * Select all nodes that are selectable by this XPath
 * expression. If multiple nodes match, multiple nodes
 * will be returned./* w  ww . j  a  va  2s  .  co m*/
 * <p/>
 * <p/>
 * <b>NOTE:</b> In most cases, nodes will be returned
 * in document-order, as defined by the XML Canonicalization
 * specification.  The exception occurs when using XPath
 * expressions involving the <code>union</code> operator
 * (denoted with the pipe '|' character).
 * </p>
 * <p/>
 * <p/>
 * <b>NOTE:</b> Param node must be a Dom node which will be used during the xpath
 * execution and iteration through the results. A call of node.dispose() must be done
 * after reading all results.
 * </p>
 *
 * @param node The node, nodeset or Context object for evaluation.
 * This value can be null.
 * @return The <code>a list</code> of all items selected
 *         by this XPath expression.
 */
public List selectNodes(Object node) {
    JXPathContext context = JXPathContext.newContext(node);

    // registering namespace prefixes
    for (int i = 0; i < namespaceMap.length; i++) {
        Map.Entry entry = (Map.Entry) namespaceMap[i];
        context.registerNamespace((String) entry.getKey(), (String) entry.getValue());
    }

    List searchResults = context.selectNodes(_queryExpr);
    Iterator searchResultsIter = searchResults.iterator();
    List resultsList = new ArrayList(searchResults.size());
    // id XPath function is returning internal pointers instead of nodes, so
    // we need to filter them
    while (searchResultsIter.hasNext()) {
        Object value = searchResultsIter.next();
        if (value instanceof DOMNodePointer) {
            value = ((DOMNodePointer) value).getNode();
        } else if (value instanceof NullPointer) {
            value = null;
        }

        if (value != null) {
            resultsList.add(value);
        }
    }
    return resultsList;
}

From source file:com.ignorelist.kassandra.steam.scraper.SharedConfig.java

public synchronized VdfNode getAppsNode() {
    if (null != appsNode) {
        return appsNode;
    }//ww  w.  j a  v  a 2  s  .com
    try {
        JXPathContext pathContext = JXPathContext.newContext(getRootNode());
        appsNode = (VdfNode) pathContext.getValue("//children[name='apps']");
        return appsNode;
    } catch (IOException | RecognitionException ex) {
        System.err.println(ex);
    }
    throw new IllegalStateException();
}

From source file:com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI.java

public Node selectNode(Node contextNode, String xpath) {
    Node node;/*  w ww.  j  a  v a2 s.co m*/
    try {
        node = (Node) JXPathContext.newContext(contextNode).selectSingleNode(xpath);
    } catch (Exception e) {
        node = null;
    }
    return node;
}

From source file:com.opera.core.systems.scope.AbstractService.java

/**
 * Query a collection with JXPath and return value of node
 *
 * @param collection//from www. j  a  v a  2 s. com
 * @param query a valid XPath query
 * @return result
 */
public Object xpathQuery(Collection<?> collection, String query) {
    JXPathContext pathContext = JXPathContext.newContext(collection);
    Object result = null;
    try {
        result = pathContext.getValue(query);
    } catch (JXPathNotFoundException e) {
        logger.log(Level.WARNING, "JXPath exception: {0}", e.getMessage());
    }
    return result;
}

From source file:blueprint.sdk.util.JXPathHelper.java

/**
 * @param expr XPath to evaluate/*  www.j av a2s  . c  om*/
 * @param target target node
 * @return Iterator or null(not found)
 */
public static Iterator<String> evaluateIterator(CompiledExpression expr, Node target) {
    return evaluateIterator(expr, JXPathContext.newContext(target));
}

From source file:com.yahoo.xpathproto.ProtoBuilder.java

/**
 * Gives a message builder from the content provided.
 *
 * @param content - The content object that is converted to JXPathContext to build the message.
 * @return The corresponding message builder object for the input.
 *//* ww w  .  j  a  v a2 s .c o  m*/
public Message.Builder builder(final Object content) {
    this.jXPathContext = JXPathContext.newContext(content);
    this.jXPathContext.setLenient(true);

    if (context == null) {
        return transformUsing(new Context(), builderConfig, transform);
    } else {
        return transformUsing(context, builderConfig, transform);
    }
}

From source file:com.pavelvlasov.uml.xmi.ElementImpl.java

private JXPathContext getJXPathContext() {
    if (jxPathContext == null) {
        if (getOwner() == null) {
            jxPathContext = JXPathContext.newContext(this);
        } else {/*from  www .j  a va2  s  .c o m*/
            jxPathContext = JXPathContext.newContext(((ElementImpl) getOwner()).getJXPathContext(), this);
        }
        jxPathContext.setLenient(true);
    }
    return jxPathContext;
}

From source file:fr.isima.ponge.wsprotocol.impl.BusinessProtocolImplTest.java

@SuppressWarnings("unchecked")
public void testAddRemoveOperationLogic() {
    Iterator it;//from   w w w.  ja  v a  2 s.  c om
    String str;
    JXPathContext ctx = JXPathContext.newContext(bp2);

    TestCase.assertEquals("s0", bp2.getInitialState().getName()); //$NON-NLS-1$
    it = ctx.iterate("finalStates/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("s1", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s0']/successors/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("s1", (String) it.next()); //$NON-NLS-1$
    TestCase.assertEquals("s0", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s0']/predecessors/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("s0", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s1']/predecessors/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("s0", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s1']/successors/name"); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("messages/name"); //$NON-NLS-1$
    // Strange, sometimes b is before a and vice-versa ...
    TestCase.assertTrue(it.hasNext());
    str = (String) it.next();
    if ("a".equals(str)) //$NON-NLS-1$
    {
        TestCase.assertEquals("b", (String) it.next()); //$NON-NLS-1$
    } else if ("b".equals(str)) //$NON-NLS-1$
    {
        TestCase.assertEquals("a", (String) it.next()); //$NON-NLS-1$
    } else {
        TestCase.fail();
    }
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s0']/outgoingOperations/message/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("a", (String) it.next()); //$NON-NLS-1$
    TestCase.assertEquals("b", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s0']/incomingOperations/message/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("b", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s1']/incomingOperations/message/name"); //$NON-NLS-1$
    TestCase.assertTrue(it.hasNext());
    TestCase.assertEquals("a", (String) it.next()); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    it = ctx.iterate("states[name='s1']/outgoingOperations/message/name"); //$NON-NLS-1$
    TestCase.assertFalse(it.hasNext());

    Operation toRemove = (Operation) ctx.getValue("operations[message/name='a']"); //$NON-NLS-1$
    bp2.removeOperation(toRemove);

    List remainingOps = (List) ctx.getValue("states[name='s0']/incomingOperations"); //$NON-NLS-1$
    TestCase.assertTrue(remainingOps.size() == 1);

    List remainingSucc = (List) ctx.getValue("states[name='s0']/successors"); //$NON-NLS-1$
    TestCase.assertTrue(remainingSucc.size() == 1);

    State s0 = bp2.getInitialState();
    bp2.removeState(s0);
    TestCase.assertNull(bp2.getInitialState());
}

From source file:com.opera.core.systems.scope.AbstractService.java

/**
 * Query a collection JXPath and return a pointer FIXME: This does not belong
 * here!/*w ww.j  a va2  s . c o  m*/
 *
 * @param collection
 * @param query
 * @return Pointer to node
 */
public Pointer xpathPointer(Collection<?> collection, String query) {
    JXPathContext pathContext = JXPathContext.newContext(collection);
    Pointer result = null;
    try {
        result = pathContext.getPointer(query);
    } catch (JXPathNotFoundException e) {
        logger.log(Level.WARNING, "JXPath exception: {0}", e.getMessage());
    }
    return result;
}