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.xforms.test.RepeatedModelItemsTest.java

public void testInitialState() throws Exception {
    this.chibaBean.setXMLContainer(getClass().getResourceAsStream("repeatedModelItems.xhtml"));
    this.chibaBean.init();
    Instance instance = this.chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    //        ModelItem item = instance.getModelItem("/data/items/item[2]/a[1]");
    //        assertNotNull(item);
    ////from   w w  w  .  j av  a  2  s.c  o  m
    //        System.out.println("item type: " + item.getDatatype());
    //        System.out.println("item type: " + item.getRelevant());
    //        System.out.println("item type: " + item.getReadonly());

    JXPathContext context = JXPathContext.newContext(instance.getInstanceDocument());
    NodeImpl node = (NodeImpl) context.getPointer("//x").getNode();
    Object object = node.getUserData();
    assertNotNull(object);

    ModelItem item = instance.getModelItem("//x");
    assertNotNull(item);

    item = instance.getModelItem("//items/item[2]/a");
    assertNotNull(item);
    assertEquals("string", item.getDatatype());
    assertEquals("false()", item.getReadonly());
    assertEquals("string-length(.) = 3", item.getRelevant());

    item = instance.getModelItem("//items/item[2]/b");
    assertEquals("integer", item.getDatatype());
    assertEquals("false()", item.getRequired());
    assertEquals("string-length(.) = 3", item.getRelevant());

    item = instance.getModelItem("//items/item[2]/c");
    assertEquals("count(//items/item)", item.getCalculate());

    chibaBean.getContainer().dispatch("insert", "DOMActivate");
    DOMUtil.prettyPrintDOM(
            chibaBean.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument());

    item = instance.getModelItem("/data[1]/items[1]/item[3]");
    assertNotNull(item);
    assertEquals("false()", item.getReadonly());

    item = instance.getModelItem("/data[1]/items[1]/item[3]/a[1]");
    assertNotNull(item);
    assertEquals("string", item.getDatatype());
    assertEquals("false()", item.getReadonly());
    assertEquals("string-length(.) = 3", item.getRelevant());

}

From source file:org.chiba.xml.xforms.test.RepeatedModelItemsTest.java

public void testDatatype() throws Exception {
    this.chibaBean.setXMLContainer(getClass().getResourceAsStream("jobAppMini-broken.xml"));
    this.chibaBean.init();

    chibaBean.getContainer().dispatch("insert-trigger", "DOMActivate");

    JXPathContext context = JXPathContext.newContext(chibaBean.getContainer().getDocument());
    String type = (String) context.getValue(
            "//*[@chiba:xpath='/jobApplication[1]/resume[1]/education-info[2]/school-name[1]/value[1]']/@chiba:type");
    assertEquals("token", type);

    type = (String) context.getValue(
            "//*[@chiba:xpath='/jobApplication[1]/resume[1]/education-info[2]/completion-date[1]/value[1]']/@chiba:type");
    assertEquals("date", type);
}

From source file:org.chiba.xml.xforms.ui.Common.java

/**
 * Performs element init./* w  ww .  jav  a 2  s .  c o m*/
 *
 * @throws XFormsException if any error occurred during init.
 */
public void init() throws XFormsException {
    if (getLogger().isDebugEnabled()) {
        getLogger().debug(this + " init");
    }

    String value = null;
    if (isBound()) {
        // get instance value
        value = getInstanceValue();
    }

    if (isLinked()) {
        // get linked value
        String src = this.element.getAttributeNS(NamespaceCtx.XFORMS_NS, SRC_ATTRIBUTE);
        Object result;

        try {
            result = this.container.getConnectorFactory().createURIResolver(src, this.element).resolve();
        } catch (Exception e) {
            throw new XFormsLinkException("uri resolution failed", e, this.model.getTarget(), src);
        }

        try {
            value = JXPathContext.newContext(result).getValue("/").toString();
        } catch (Exception e) {
            throw new XFormsLinkException("object model not supported", e, this.model.getTarget(), src);
        }
    }

    if (value != null) {
        // initialize bound or linked value
        DOMUtil.removeAllChildren(this.element);
        this.element.appendChild(this.element.getOwnerDocument().createTextNode(value));
    } else {
        // initialize children
        initializeChildren();
    }
}

From source file:org.chiba.xml.xforms.ui.RepeatedModelItemsTest.java

/**
 * Sets up the test.//from   w w w. j av  a2s .  co m
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    this.chibaBean = new ChibaBean();
    this.chibaBean.setXMLContainer(getClass().getResourceAsStream("RepeatedModelItemsTest.xhtml"));
    this.chibaBean.init();

    this.context = JXPathContext.newContext(this.chibaBean.getContainer().lookup("repeat-1").getElement());
}

From source file:org.chiba.xml.xforms.ui.RepeatIndexTest.java

/**
 * Sets up the test.//from w w  w. j a  v a  2 s .  c  om
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    this.chibaBean = new ChibaBean();
    this.chibaBean.setXMLContainer(getClass().getResourceAsStream("RepeatIndexTest.xhtml"));
    this.chibaBean.init();

    JXPathContext context = JXPathContext.newContext(this.chibaBean.getXMLContainer());

    // iterate unrolled repeats
    Iterator repeatIterator = context.iterate("//xf:repeat[chiba:data]/@id");
    this.enclosingRepeat = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat1 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat2 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat3 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());

    // iterate repeated inputs
    Iterator inputIterator = context.iterate("//xf:input/@id");
    this.input11 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());
    this.input12 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());
    inputIterator.next(); // skip repeat prototype

    this.input21 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());
    this.input22 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());
    inputIterator.next(); // skip repeat prototype

    this.input31 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());
    this.input32 = (Text) this.chibaBean.getContainer().lookup(inputIterator.next().toString());

    // iterate repeated actions
    Iterator actionIterator = context.iterate("//xf:action/@id");
    this.action11 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
    this.action12 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
    actionIterator.next(); // skip repeat prototype

    this.action21 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
    this.action22 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
    actionIterator.next(); // skip repeat prototype

    this.action31 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
    this.action32 = (XFormsAction) this.chibaBean.getContainer().lookup(actionIterator.next().toString());
}

From source file:org.chiba.xml.xforms.ui.RepeatItemTest.java

/**
 * Sets up the test./*from   w w  w. j  a v a  2  s.c  om*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    this.chibaBean = new ChibaBean();
    this.chibaBean.setXMLContainer(getClass().getResourceAsStream("RepeatItemTest.xhtml"));
    this.chibaBean.init();

    JXPathContext context = JXPathContext.newContext(this.chibaBean.getXMLContainer());

    // iterate unrolled repeats
    Iterator repeatIterator = context.iterate("//xf:repeat[chiba:data]/@id");
    this.enclosingRepeat = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat1 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat2 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
    this.nestedRepeat3 = (Repeat) this.chibaBean.getContainer().lookup(repeatIterator.next().toString());
}

From source file:org.chiba.xml.xforms.ui.RepeatTest.java

/**
 * Tests index updating.//www .  j  a  v a2s .c o m
 *
 * @throws Exception if any error occurred during the test.
 */
public void testSetIndex() throws Exception {
    Repeat repeat = (Repeat) this.chibaBean.getContainer().lookup("repeat");
    register(repeat.getTarget());

    Output output = (Output) this.chibaBean.getContainer().lookup("selection");
    assertEquals("first", output.getValue());

    JXPathContext context = JXPathContext.newContext(repeat.getElement());
    assertEquals("1", context.getValue("chiba:data/@chiba:index"));

    this.chibaBean.dispatch("trigger-setindex", DOMEventNames.ACTIVATE);
    deregister(repeat.getTarget());

    assertEquals(3, repeat.getIndex());
    assertEquals(3, repeat.getContextSize());
    assertEquals("last", output.getValue());
    assertEquals("3", context.getValue("chiba:data/@chiba:index"));

    assertItemInserted(null, null, null);
    assertItemDeleted(null, null, null);
    assertIndexChanged("repeat", "repeat", "3");
    assertStateChanged(null, null);
}

From source file:org.chiba.xml.xforms.ui.RepeatTest.java

/**
 * Tests for correct prototype handling.
 *
 * @throws Exception if any error occurred during the test.
 *//*from   ww w.j  a va 2s  .c o m*/
public void testRepeatPrototype() throws Exception {
    Repeat repeat = (Repeat) this.chibaBean.getContainer().lookup("repeat");
    JXPathContext context = JXPathContext.newContext(repeat.getElement());

    /*
    <chiba:data ... >
    <xf:group id="<generated>" appearance="repeated">
        <xf:input id="input" ref="."/>
        <xf:output id="output" ref="."/>
    </xf:group>
    </chiba:data>
    */
    assertEquals(new Double(1d), context.getValue("count(chiba:data/*)"));

    assertEquals("group", context.getValue("local-name(chiba:data/*[1])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/*[1])"));
    assertEquals(Boolean.TRUE, context.getValue("string-length(chiba:data/xf:group/@id) > 0"));
    assertEquals("repeated", context.getValue("chiba:data/xf:group/@appearance"));
    assertEquals(new Double(2d), context.getValue("count(chiba:data/xf:group/*)"));

    assertEquals("input", context.getValue("local-name(chiba:data/xf:group/*[1])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/xf:group/*[1])"));
    assertEquals("input", context.getValue("chiba:data/xf:group/xf:input/@id"));
    assertEquals(".", context.getValue("chiba:data/xf:group/xf:input/@ref"));

    assertEquals("output", context.getValue("local-name(chiba:data/xf:group/*[2])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/xf:group/*[2])"));
    assertEquals("output", context.getValue("chiba:data/xf:group/xf:output/@id"));
    assertEquals(".", context.getValue("chiba:data/xf:group/xf:output/@ref"));
}

From source file:org.chiba.xml.xforms.ui.RepeatTest.java

/**
 * Tests for correct prototype handling.
 *
 * @throws Exception if any error occurred during the test.
 *//*from  w w w  .  ja v  a2 s .c o  m*/
public void testRepeatPrototypeAnon() throws Exception {
    Repeat repeat = (Repeat) this.chibaBean.getContainer().lookup("repeat-anon");
    JXPathContext context = JXPathContext.newContext(repeat.getElement());

    /*
    <chiba:data ... >
    <xf:group id="<generated>" appearance="repeated">
        <xf:input id="input" ref="."/>
        <xf:output id="output" ref="."/>
    </xf:group>
    </chiba:data>
    */
    assertEquals(new Double(1d), context.getValue("count(chiba:data/*)"));

    assertEquals("group", context.getValue("local-name(chiba:data/*[1])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/*[1])"));
    assertEquals(Boolean.TRUE, context.getValue("string-length(chiba:data/xf:group/@id) > 0"));
    assertEquals("repeated", context.getValue("chiba:data/xf:group/@appearance"));
    assertEquals(new Double(2d), context.getValue("count(chiba:data/xf:group/*)"));

    assertEquals("input", context.getValue("local-name(chiba:data/xf:group/*[1])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/xf:group/*[1])"));
    assertEquals(Boolean.TRUE, context.getValue("string-length(chiba:data/xf:group/xf:input/@id) > 0"));
    assertEquals(".", context.getValue("chiba:data/xf:group/xf:input/@ref"));

    assertEquals("output", context.getValue("local-name(chiba:data/xf:group/*[2])"));
    assertEquals(NamespaceConstants.XFORMS_NS, context.getValue("namespace-uri(chiba:data/xf:group/*[2])"));
    assertEquals(Boolean.TRUE, context.getValue("string-length(chiba:data/xf:group/xf:output/@id) > 0"));
    assertEquals(".", context.getValue("chiba:data/xf:group/xf:output/@ref"));
}

From source file:org.chiba.xml.xforms.ui.RepeatTest.java

/**
 * Tests location path resolution./*w ww  . j  a va 2  s  . c  o m*/
 *
 * @throws Exception if any error occurred during the test.
 */
public void testLocationPath() throws Exception {
    Repeat repeat = (Repeat) this.chibaBean.getContainer().lookup("repeat");
    List ids = new ArrayList();
    JXPathContext context = JXPathContext.newContext(repeat.getElement());
    Iterator iterator = context.iterate("//xf:input[chiba:data]/@id");
    while (iterator.hasNext()) {
        String id = (String) iterator.next();
        ids.add(id);
    }

    assertEquals(3, ids.size());
    for (int index = 0; index < ids.size(); index++) {
        String id = (String) ids.get(index);
        Text input = (Text) this.chibaBean.getContainer().lookup(id);
        assertEquals("/*[1]/item[" + String.valueOf(index + 1) + "]", input.getLocationPath());
    }
}