Search in sources :

Example 1 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project galley by Commonjava.

the class JXPathContextAncestryTest method basicJXPathTest.

@Test
@Ignore
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));
    }
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Stack(java.util.Stack) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project opennms by OpenNMS.

the class JsonCollectorSolarisZonesIT method testXpath.

/**
 * Test to verify XPath content.
 *
 * @throws Exception the exception
 */
@Test
@SuppressWarnings("unchecked")
public void testXpath() throws Exception {
    JSONObject json = MockDocumentBuilder.getJSONDocument();
    JXPathContext context = JXPathContext.newContext(json);
    Iterator<Pointer> itr = context.iteratePointers("/zones/zone");
    while (itr.hasNext()) {
        Pointer resPtr = itr.next();
        JXPathContext relativeContext = context.getRelativeContext(resPtr);
        String resourceName = (String) relativeContext.getValue("@name");
        Assert.assertNotNull(resourceName);
        String value = (String) relativeContext.getValue("parameter[@key='nproc']/@value");
        Assert.assertNotNull(Integer.valueOf(value));
    }
}
Also used : JSONObject(net.sf.json.JSONObject) JXPathContext(org.apache.commons.jxpath.JXPathContext) Pointer(org.apache.commons.jxpath.Pointer) Test(org.junit.Test)

Example 3 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.

the class AbstractDataMapper method map.

public MappedData map(DataInput input, MappingContext mappingContext) {
    JXPathContext context = newContext(input.getValue());
    context.setFunctions(converterLibrary);
    InfomodelData normalized = new InfomodelData();
    final Infomodel deviceInfoModel = specification.getInfoModel();
    for (ModelProperty fbProperty : deviceInfoModel.getFunctionblocks()) {
        if (mappingContext.isIncluded(fbProperty.getName())) {
            FunctionblockData mappedFb = mapFunctionBlock(fbProperty, context);
            if (mappedFb != null) {
                normalized.withFunctionblock(mappedFb);
            }
        }
    }
    return this.doMap(normalized, mappingContext);
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext) Infomodel(org.eclipse.vorto.repository.api.content.Infomodel) ModelProperty(org.eclipse.vorto.repository.api.content.ModelProperty) FunctionblockData(org.eclipse.vorto.service.mapping.normalized.FunctionblockData) InfomodelData(org.eclipse.vorto.service.mapping.normalized.InfomodelData)

Example 4 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.

the class Jxpath method eval.

public static Object eval(String exp, Object value) {
    JXPathContext context = newContext(value);
    context.setFunctions(converterLibrary);
    return context.getValue(exp.replaceAll("\\.", "/"));
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext)

Example 5 with JXPathContext

use of org.apache.commons.jxpath.JXPathContext in project vorto by eclipse.

the class Jxpath method newContext.

private static JXPathContext newContext(Object ctxObject) {
    JXPathContext context = JXPathContext.newContext(ctxObject);
    TypeUtils.setTypeConverter(new MyTypeConverter());
    context.setLenient(false);
    return context;
}
Also used : JXPathContext(org.apache.commons.jxpath.JXPathContext)

Aggregations

JXPathContext (org.apache.commons.jxpath.JXPathContext)37 Element (org.jdom.Element)12 JXPathException (org.apache.commons.jxpath.JXPathException)7 List (java.util.List)6 ArrayList (java.util.ArrayList)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 JXPathInvalidSyntaxException (org.apache.commons.jxpath.JXPathInvalidSyntaxException)3 Pointer (org.apache.commons.jxpath.Pointer)3 HttpResponse (org.apache.http.HttpResponse)3 StatusLine (org.apache.http.StatusLine)3 HttpClient (org.apache.http.client.HttpClient)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)3 Test (org.junit.Test)3 ModelJXPathContext (org.openforis.idm.model.expression.internal.ModelJXPathContext)3