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:org.chiba.xml.xpath.impl.JXPathTest.java

/**
 * Sets up the test./*w w  w .jav  a  2  s  .  co m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(getClass().getResourceAsStream("JXPathTest.xhtml"));

    this.context = JXPathContext.newContext(document);
    this.context.registerNamespace("html", "http://www.w3.org/1999/xhtml");
    this.context.registerNamespace("xf", "http://www.w3.org/2002/xforms");
    this.context.registerNamespace("ev", "http://www.w3.org/2001/xml-events");
    this.context.registerNamespace("", "");
    this.context.registerNamespace("my", "http://tempuri.org/my");
}

From source file:org.cloudml.connectors.util.CloudMLQueryUtil.java

public static Object cloudmlQuery(String jxpath, Object context) {
    JXPathContext jxpathcontext = JXPathContext.newContext(context);
    return jxpathcontext.getValue(jxpath);
}

From source file:org.cloudml.connectors.util.JXPath.java

public Object query(Object context) {
    JXPathContext jpathcontext = JXPathContext.newContext(context);
    return jpathcontext.getValue(literal);
}

From source file:org.cloudml.connectors.util.JXPath.java

public Iterator iterate(Object context) {
    JXPathContext jpathcontext = JXPathContext.newContext(context);
    return jpathcontext.iterate(literal);
}

From source file:org.cloudml.deployer.CloudMLElementComparator.java

public static Object query(Object obj, String path) {
    try {// w  ww .  j  a  v  a  2 s .  co  m
        JXPathContext jxpc = JXPathContext.newContext(obj);
        return jxpc.getValue(path);
    } catch (NullPointerException e) {
        return null;
    } catch (JXPathNotFoundException e) {
        return null;
    }
}

From source file:org.codehaus.mojo.javascript.assembler.JsBuilderAssemblerReader.java

/**
 * {@inheritDoc}/*from   w ww . ja va  2 s . c  o  m*/
 * 
 * @see org.codehaus.mojo.javascript.assembler.AssemblerReader#getAssembler(java.io.File)
 */
public Assembler getAssembler(File file) throws Exception {
    logger.info("Reading assembler descriptor " + file.getAbsolutePath());
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document dom = builder.parse(file);

    Assembler assembler = new Assembler();
    JXPathContext xpath = JXPathContext.newContext(dom);
    xpath.setLenient(true);
    String src = (String) xpath.getValue("//directory/@name");
    List nodes = xpath.selectNodes("//target");
    for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
        Node node = (Node) iterator.next();
        Script script = new Script();
        assembler.addScript(script);
        JXPathContext nodeContext = JXPathContext.newContext(node);
        String fileName = (String) nodeContext.getValue("@file");
        fileName = fileName.replace('\\', '/');
        if (fileName.startsWith(OUTPUT)) {
            fileName = fileName.substring(OUTPUT.length());
        }
        script.setFileName(fileName);

        for (Iterator iter = nodeContext.iterate("//include/@name"); iter.hasNext();) {
            String include = ((String) iter.next()).replace('\\', '/');
            if (src != null && src.length() > 0) {
                include = include.substring(src.length() + 1);
            }
            script.addInclude(include);
        }
    }
    return assembler;
}

From source file:org.commonjava.maven.galley.maven.model.view.JXPathContextAncestryTest.java

@Test
@Ignore//  w w  w  .j a  va2 s  .  c  om
public void basicJXPathTest() throws Exception {
    final InputStream is = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("jxpath/simple.pom.xml");

    final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

    final JXPathContext ctx = JXPathContext.newContext(document);

    document.getDocumentElement().removeAttribute("xmlns");

    final String projectGroupIdPath = "ancestor::project/groupId";

    // NOT what's failing...just populating the node set to traverse in order to feed the ancestor:: axis test.
    final List<?> nodes = ctx.selectNodes("/project/dependencies/dependency");
    for (final Object object : nodes) {
        final Node node = (Node) object;
        dump(node);

        final Stack<Node> revPath = new Stack<Node>();

        Node parent = node;
        while (parent != null) {
            revPath.push(parent);
            parent = parent.getParentNode();
        }

        JXPathContext nodeCtx = null;
        while (!revPath.isEmpty()) {
            final Node part = revPath.pop();
            if (nodeCtx == null) {
                nodeCtx = JXPathContext.newContext(part);
            } else {
                nodeCtx = JXPathContext.newContext(nodeCtx, part);
            }
        }

        System.out
                .println("Path derived from context: '" + nodeCtx.getNamespaceContextPointer().asPath() + "'");

        // brute-force approach...try to force population of the parent pointers by painstakingly constructing contexts for all intermediate nodes.
        System.out.println("Selecting groupId for declaring project using path-derived context...");
        System.out.println(nodeCtx.getValue(projectGroupIdPath));

        // Naive approach...this has all the context info it needs to get parent contexts up to and including the document!
        System.out.println("Selecting groupId for declaring project using non-derived context...");
        System.out.println(JXPathContext.newContext(node).getValue(projectGroupIdPath));
    }
}

From source file:org.commonjava.maven.galley.maven.parse.JXPathUtils.java

public static JXPathContext newContext(final Node node) {
    final JXPathContext ctx = JXPathContext.newContext(node);
    ctx.setLenient(true);/*ww w  . ja  v  a 2 s.c om*/
    ctx.setFunctions(new ClassFunctions(ResolveFunctions.class, "ext"));

    return ctx;
}

From source file:org.dcm4che3.conf.core.Nodes.java

public static Object getNode(Object rootConfigNode, String path) {
    try {/*from ww  w  .j a  v  a 2s . c om*/
        return JXPathContext.newContext(rootConfigNode).getValue(path);
    } catch (JXPathNotFoundException e) {
        return null;
    }
}

From source file:org.dcm4che3.conf.core.Nodes.java

public static void removeNodes(Map<String, Object> configurationRoot, String path) {
    JXPathContext.newContext(configurationRoot).removeAll(path);
}