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

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

Introduction

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

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

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

@SuppressWarnings("unchecked")
public void testAddRemoveOperationLogic() {
    Iterator it;//from w w  w .java 2 s .c  o  m
    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:nl.clockwork.mule.common.filter.JXPathFilter.java

@Override
public boolean accept(MuleMessage message) {
    try {/*from  ww  w. j av a  2s. c  om*/
        JXPathContext context = JXPathContext.newContext(message.getPayload());
        String s = context.getValue(xPathQuery).toString();
        return s.matches(regEx);
    } catch (Exception e) {
        return false;
    }
}

From source file:nl.clockwork.mule.common.filter.ObjectExistsFilter.java

@Override
public boolean accept(MuleMessage message) {
    JXPathContext context = JXPathContext.newContext(message.getPayload());
    return context.getValue(xPathQuery) != null;
}

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  ww w .  j a v a  2 s. c  o  m*/
    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;
    }//from  ww w . j av  a2s.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.source.impl.ModuleSource.java

/**
 * Return an <code>InputStream</code> object to read from the source.
 *
 * @throws IOException if I/O error occured.
 *//*from ww w.j  a v a 2 s .  co  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.forms.binding.RepeaterJXPathBinding.java

/**
 * Get the identity of the given row context. That's infact a list of all
 * the values of the fields in the bean or XML that constitute the identity. 
 * @param rowContext//w  w w.j  a v  a  2  s.com
 * @return List the identity of the row context
 */
private List getIdentity(JXPathContext rowContext) {
    if (this.identityBinding == null) {
        return Collections.EMPTY_LIST;
    }

    List identity = new ArrayList();

    JXPathBindingBase[] childBindings = this.identityBinding.getChildBindings();
    if (childBindings != null) {
        int size = childBindings.length;
        for (int i = 0; i < size; i++) {
            ValueJXPathBinding vBinding = (ValueJXPathBinding) childBindings[i];
            Object value = rowContext.getValue(vBinding.getXPath());
            if (value != null && vBinding.getConvertor() != null) {
                if (value instanceof String) {
                    ConversionResult conversionResult = vBinding.getConvertor()
                            .convertFromString((String) value, vBinding.getConvertorLocale(), null);
                    if (conversionResult.isSuccessful())
                        value = conversionResult.getResult();
                    else
                        value = null;
                } else {
                    if (getLogger().isWarnEnabled()) {
                        getLogger().warn("Convertor ignored on backend-value " + "which isn't of type String.");
                    }
                }
            }
            identity.add(value);
        }
    }
    return identity;
}

From source file:org.apache.cocoon.forms.binding.RepeaterJXPathCollection.java

public void init(JXPathContext storageContext, String rowpath, RepeaterAdapter adapter) {
    this.storageContext = storageContext;
    collectionSize = 0;/*  w w  w . j ava2s. com*/
    Object value = storageContext.getValue(rowpath);
    if (value != null) {
        if (value instanceof Collection) {
            collectionSize = ((Collection) value).size();
        } else {
            collectionSize = ((Double) storageContext.getValue("count(" + rowpath + ")")).intValue();
        }
    }

    this.updatedRows = new HashMap();
    this.deletedRows = new HashSet();
    this.insertedRows = new ArrayList();
    this.adapter = adapter;
    this.sorter = adapter.sortBy(null);
}

From source file:org.apache.cocoon.forms.binding.ValueJXPathBinding.java

/**
 * Actively performs the binding from the ObjectModel wrapped in a jxpath
 * context to the CForms-form-widget specified in this object.
 *//*from  w ww . j a  v  a 2  s  . com*/
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = selectWidget(frmModel, this.fieldId);
    if (widget == null) {
        throw new BindingException("The widget with the ID [" + this.fieldId
                + "] referenced in the binding does not exist in the form definition.");
    }

    Object value = jxpc.getValue(this.xpath);
    if (value != null && convertor != null) {
        if (value instanceof String) {
            ConversionResult conversionResult = convertor.convertFromString((String) value, convertorLocale,
                    null);
            if (conversionResult.isSuccessful())
                value = conversionResult.getResult();
            else
                value = null;
        } else {
            getLogger().warn("Convertor ignored on backend-value which isn't of type String.");
        }
    }

    widget.setValue(value);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Done loading " + toString() + " -- value= " + value);
    }
}

From source file:org.apache.cocoon.forms.binding.ValueJXPathBinding.java

/**
 * Actively performs the binding from the CForms-form to the ObjectModel
 * wrapped in a jxpath context/*from  w  w  w  .jav a2s .co m*/
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = selectWidget(frmModel, this.fieldId);
    Object value = widget.getValue();
    if (value != null && convertor != null) {
        value = convertor.convertToString(value, convertorLocale, null);
    }

    Object oldValue = jxpc.getValue(this.xpath);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
    }

    boolean update = false;

    if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
        // first update the value itself
        if (value != null) {
            jxpc.createPathAndSetValue(this.xpath, value);
        } else {
            jxpc.removePath(this.xpath);
        }

        // now perform any other bindings that need to be performed when the value is updated
        JXPathContext subContext = null;
        try {
            subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
        } catch (JXPathException e) {
            // if the value has been set to null and the underlying model is a bean, then
            // JXPath will not be able to create a relative context
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("(Ignorable) problem binding field " + widget.getRequestParameterName(), e);
            }
        }
        if (subContext != null) {
            this.updateBinding.saveFormToModel(frmModel, subContext);
        }

        update = true;
    }

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update);
    }
}