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.apache.camel.language.jxpath.JXPathExpression.java

public <T> T evaluate(Exchange exchange, Class<T> tClass) {
    try {//from www . j a va  2 s . com
        JXPathContext context = JXPathContext.newContext(exchange);
        context.setLenient(lenient);
        Object result = getJXPathExpression().getValue(context, type);
        assertResultType(exchange, result);
        return exchange.getContext().getTypeConverter().convertTo(tClass, result);
    } catch (JXPathException e) {
        throw new ExpressionEvaluationException(this, exchange, e);
    }
}

From source file:org.apache.cocoon.components.expression.jxpath.JXPathExpression.java

private JXPathContext getContext(ExpressionContext context) {
    // This could be made more efficient by caching the
    // JXPathContext within the Context object.
    JXPathContext jxcontext = JXPathContext.newContext(context.getContextBean());
    jxcontext.setVariables(new VariableAdapter(context));
    jxcontext.setLenient(this.lenient);
    jxcontext.setNamespaceContextPointer(new NamespacesTablePointer(context.getNamespaces()));
    return jxcontext;
}

From source file:org.apache.cocoon.components.flow.java.test.FlowTest.java

public void testSimple() throws Exception {

    /*        ClassLoader cl = getClass().getClassLoader();
            while (cl != null) {/*from   www.  ja  va 2s .c  om*/
    System.out.println(cl);
    cl = cl.getParent();
            }
            try {
    System.out.println(
            getClass().
            getProtectionDomain().
            getCodeSource().
            getLocation());
            }
            catch (Exception e) {
            }*/

    Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
    Continuable flow = (Continuable) clazz.newInstance();

    Method method = clazz.getMethod("run", new Class[0]);

    Continuation c = new Continuation(context);
    assertTrue(!c.isRestoring());
    assertTrue(!c.isCapturing());

    System.out.println("*** start flow");
    c.registerThread();
    method.invoke(flow, new Object[0]);
    if (c.isCapturing())
        c.getStack().popReference();
    c.deregisterThread();
    System.out.println("*** return from flow");

    assertTrue(!c.isRestoring());
    assertTrue(c.isCapturing());

    //System.out.println("request=" + request);
    request.addParameter("a", "2.3");
    redirector.reset();
    c = new Continuation(c, context);

    assertTrue(c.isRestoring());
    assertTrue(!c.isCapturing());

    System.out.println("*** resume flow");
    c.registerThread();
    method.invoke(flow, new Object[0]);
    if (c.isCapturing())
        c.getStack().popReference();
    c.deregisterThread();
    System.out.println("*** return from flow");

    assertTrue(!c.isRestoring());
    assertTrue(!c.isCapturing());

    VarMap map = (VarMap) FlowHelper.getContextObject(objectmodel);

    assertEquals(((Float) map.getMap().get("result")).floatValue(), 3.3f, 0.1f);

    JXPathContext jxcontext = JXPathContext.newContext(FlowHelper.getContextObject(objectmodel));
    Float result = (Float) jxcontext.getValue("result");

    assertEquals(result.floatValue(), 3.3f, 0.1f);
}

From source file:org.apache.cocoon.components.modules.input.JXPathHelper.java

public static Object getAttribute(String name, Configuration modeConf, JXPathHelperConfiguration setup,
        Object contextObj) throws ConfigurationException {

    if (contextObj == null) {
        return null;
    }//w  w w  .jav  a  2s.c  o m

    try {
        JXPathContext jxContext = JXPathContext.newContext(contextObj);
        setup(setup, jxContext, modeConf);

        Object obj = jxContext.getValue(name);
        return obj;
    } catch (Exception e) {
        throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e);
    }
}

From source file:org.apache.cocoon.components.modules.input.JXPathHelper.java

public static Object[] getAttributeValues(String name, Configuration modeConf, JXPathHelperConfiguration setup,
        Object contextObj) throws ConfigurationException {

    if (contextObj == null) {
        return null;
    }/*from  w w w  .  ja  va  2 s .c o m*/

    try {
        JXPathContext jxContext = JXPathContext.newContext(contextObj);
        setup(setup, jxContext, modeConf);

        List values = null;
        Iterator i = jxContext.iterate(name);
        if (i.hasNext()) {
            values = new LinkedList();
        }
        while (i.hasNext()) {
            values.add(i.next());
        }
        Object[] obj = null;
        if (values != null) {
            obj = values.toArray();
            if (obj.length == 0) {
                obj = null;
            }
        }
        return obj;
    } catch (Exception e) {
        throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e);
    }
}

From source file:org.apache.cocoon.components.source.impl.ModuleSource.java

/**
 * Return an <code>InputStream</code> object to read from the source.
 *
 * @throws IOException if I/O error occured.
 *//*from   w  w  w  .j a  v  a 2  s.  c o  m*/
public InputStream getInputStream() throws IOException, SourceException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Getting InputStream for " + getURI());
    }

    Object obj = getInputAttribute(this.attributeType, this.attributeName);
    if (obj == null)
        throw new SourceException(" The attribute: " + this.attributeName + " is empty");

    if (!(this.xPath.length() == 0 || this.xPath.equals("/"))) {
        JXPathContext context = JXPathContext.newContext(obj);
        obj = context.getValue(this.xPath);

        if (obj == null)
            throw new SourceException("the xpath: " + this.xPath + " applied on the attribute: "
                    + this.attributeName + " returns null");
    }

    if (obj instanceof InputStream) {
        return (InputStream) obj;
    } else if (obj instanceof String) {
        return new ByteArrayInputStream(((String) obj).getBytes());
    } else if (obj instanceof byte[]) {
        return new ByteArrayInputStream((byte[]) obj);
    } else {
        throw new SourceException(
                "The object type: " + obj.getClass() + " could not be serialized as a InputStream " + obj);
    }
}

From source file:org.apache.cocoon.components.source.impl.XModuleSource.java

/**
 * Implement this method to obtain SAX events.
 *
 */// w  w  w .j  av a 2  s  .co  m

public void toSAX(ContentHandler handler) throws SAXException {

    Object obj = getInputAttribute(this.attributeType, this.attributeName);
    if (obj == null)
        throw new SAXException(" The attribute: " + this.attributeName + " is empty");

    if (!(this.xPath.length() == 0 || this.xPath.equals("/"))) {
        JXPathContext context = JXPathContext.newContext(obj);

        obj = context.getPointer(this.xPath).getNode();

        if (obj == null)
            throw new SAXException("the xpath: " + this.xPath + " applied on the attribute: "
                    + this.attributeName + " returns null");
    }

    if (obj instanceof Document) {
        DOMStreamer domStreamer = new DOMStreamer(handler);
        domStreamer.stream((Document) obj);
    } else if (obj instanceof Node) {
        DOMStreamer domStreamer = new DOMStreamer(handler);
        handler.startDocument();
        domStreamer.stream((Node) obj);
        handler.endDocument();
    } else if (obj instanceof XMLizable) {
        ((XMLizable) obj).toSAX(handler);
    } else {
        throw new SAXException(
                "The object type: " + obj.getClass() + " could not be serialized to XML: " + obj);
    }
}

From source file:org.apache.cocoon.components.source.impl.XModuleSource.java

/**
 * Delete the source //from w  w  w. j a  v a2s  .c  o m
 */
public void delete() throws SourceException {
    if (!(this.xPath.length() == 0 || this.xPath.equals("/"))) {
        Object value;
        try {
            value = getInputAttribute(this.attributeType, this.attributeName);
        } catch (SAXException e) {
            throw new SourceException("delete: ", e);
        }
        if (value == null)
            throw new SourceException(" The attribute: " + this.attributeName + " is empty");

        JXPathContext context = JXPathContext.newContext(value);
        context.removeAll(this.xPath);
    } else {
        try {
            setOutputAttribute(this.attributeType, this.attributeName, null);
        } catch (SAXException e) {
            throw new SourceException("delete: ", e);
        }
    }
}

From source file:org.apache.cocoon.components.source.impl.XModuleSource.java

public void notify(Document insertDoc) throws SAXException {

    // handle xpaths, we are only handling inserts, i.e. if there is no
    // attribute of the given name and type the operation will fail
    if (!(this.xPath.length() == 0 || this.xPath.equals("/"))) {

        Object value = getInputAttribute(this.attributeType, this.attributeName);
        if (value == null)
            throw new SAXException(" The attribute: " + this.attributeName + " is empty");

        JXPathContext context = JXPathContext.newContext(value);

        if (value instanceof Document) {
            // If the attribute contains a dom document we
            // create the elements in the given xpath if
            // necesary, import the input document and put it
            // in the place described by the xpath.
            Document doc = (Document) value;

            Node importedNode = doc.importNode(insertDoc.getDocumentElement(), true);

            context.setLenient(true);// w w  w. j  a  v a2s  . co m
            context.setFactory(new DOMFactory());
            context.createPathAndSetValue(this.xPath, importedNode);
        } else {
            // Otherwise just try to put a the input document in
            // the place pointed to by the xpath
            context.setValue(this.xPath, insertDoc);
        }

    } else {
        setOutputAttribute(this.attributeType, this.attributeName, insertDoc);
    }
}

From source file:org.apache.cocoon.el.impl.jxpath.JXPathExpression.java

private JXPathContext getContext(ObjectModel objectModel) {
    // This could be made more efficient by caching the
    // JXPathContext within the Context object.

    JXPathContext jxobjectModel = JXPathContext.newContext(objectModel.get(ObjectModel.CONTEXTBEAN));
    jxobjectModel.setVariables(new VariableAdapter(objectModel));
    jxobjectModel.setLenient(this.lenient);
    jxobjectModel.setNamespaceContextPointer(
            new NamespacesTablePointer((NamespacesTable) objectModel.get(ObjectModel.NAMESPACE)));
    return jxobjectModel;
}