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:org.chiba.xml.xforms.connector.ConnectorFactory.java

private String evalXPath(Element element, String valueTemplate) {
    XFormsElement xElement = (XFormsElement) ((ElementImpl) element).getUserData();
    JXPathContext context = xElement.getModel().getDefaultInstance().getInstanceContext();
    String xpath;/*  ww w .  j  av a2s. c  o m*/

    if (XPathUtil.hasInstanceFunction(valueTemplate)) {
        xpath = valueTemplate;
    } else {
        String path;
        if (xElement instanceof AbstractBoundAction) {
            path = ((AbstractBoundAction) xElement).getLocationPath();
        } else if (xElement instanceof Submission) {
            path = ((Submission) xElement).getLocationPath();
        } else {
            LOGGER.warn(
                    "no AVT processing for this element implemented - returning default instance root as context path");
            path = "instance('default')";
        }
        xpath = evalJoinedXPath(path, valueTemplate);
    }
    return (String) context.getValue(xpath);
}

From source file:org.chiba.xml.xforms.constraints.RelevanceSelectorTest.java

/**
 * Test case 1 for relevance selection of submitted instance data.
 *
 * @throws Exception if any error occurred during the test.
 *///w w  w  .  j  ava2 s .  c  o m
public void testSelectRelevant1() throws Exception {
    Instance instance = this.chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    Document document = RelevanceSelector.selectRelevant(instance);
    document.normalize();
    JXPathContext context = JXPathContext.newContext(document);

    assertTrue("name(/*[1]) = 'root'", context.getValue("name(/*[1])").toString().equals("root"));
    assertTrue("/root/@xmlns = ''", context.getValue("/root/@xmlns").toString().equals(""));
    assertTrue("/root/@xmlns:test = 'http://test.org/ns'",
            context.getValue("/root/@xmlns:test").toString().equals("http://test.org/ns"));
    assertTrue("count(/root/*) = 3", context.getValue("count(/root/*)", Integer.class).toString().equals("3"));

    assertTrue("name(/root/*[1]) = 'name'", context.getValue("name(/root/*[1])").toString().equals("name"));
    assertTrue("/root/name/@id = 'n-1'", context.getValue("/root/name/@id").toString().equals("n-1"));
    assertTrue("count(/root/name/*) = 0",
            context.getValue("count(/root/name/*)", Integer.class).toString().equals("0"));

    assertTrue("name(/root/*[2]) = 'paragraph'",
            context.getValue("name(/root/*[2])").toString().equals("paragraph"));
    assertTrue("/root/paragraph[1]/@id = 'p-1'",
            context.getValue("/root/paragraph[1]/@id").toString().equals("p-1"));
    assertTrue("count(/root/paragraph[1]/*) = 1",
            context.getValue("count(/root/paragraph[1]/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/root/paragraph[1]/*[1]) = 'content'",
            context.getValue("name(/root/paragraph[1]/*[1])").toString().equals("content"));
    assertTrue("/root/paragraph[1]/content/@id = 'c-1'",
            context.getValue("/root/paragraph[1]/content/@id").toString().equals("c-1"));
    assertTrue("/root/paragraph[1]/content/text()[1] = 'some mixed content 1'",
            context.getValue("/root/paragraph[1]/content/text()[1]").toString().equals("some mixed content 1"));
    assertTrue("/root/paragraph[1]/content/text()[2] = 'some more mixed content 1'", context
            .getValue("/root/paragraph[1]/content/text()[2]").toString().equals("some more mixed content 1"));
    assertTrue("count(/root/paragraph[1]/content/*) = 1",
            context.getValue("count(/root/paragraph[1]/content/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/root/paragraph[1]/content/*[1]) = 'input'",
            context.getValue("name(/root/paragraph[1]/content/*[1])").toString().equals("input"));
    assertTrue("/root/paragraph[1]/content/input/@id = 'i-1'",
            context.getValue("/root/paragraph[1]/content/input/@id").toString().equals("i-1"));
    assertTrue("/root/paragraph[1]/content/input/text()[1] = 'input 1'",
            context.getValue("/root/paragraph[1]/content/input/text()[1]").toString().equals("input 1"));
    assertTrue("count(/root/paragraph[1]/content/input/*) = 0", context
            .getValue("count(/root/paragraph[1]/content/input/*)", Integer.class).toString().equals("0"));

    assertTrue("name(/root/*[3]) = 'paragraph'",
            context.getValue("name(/root/*[3])").toString().equals("paragraph"));
    assertTrue("/root/paragraph[2]/@id = 'p-2'",
            context.getValue("/root/paragraph[2]/@id").toString().equals("p-2"));
    assertTrue("count(/root/paragraph[2]/*) = 1",
            context.getValue("count(/root/paragraph[2]/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/root/paragraph[2]/*[1]) = 'content'",
            context.getValue("name(/root/paragraph[2]/*[1])").toString().equals("content"));
    assertTrue("/root/paragraph[2]/content/@id = 'c-2'",
            context.getValue("/root/paragraph[2]/content/@id").toString().equals("c-2"));
    assertTrue("/root/paragraph[2]/content/text()[1] = 'some mixed content 2'",
            context.getValue("/root/paragraph[2]/content/text()[1]").toString().equals("some mixed content 2"));
    assertTrue("/root/paragraph[2]/content/text()[2] = 'some more mixed content 2'", context
            .getValue("/root/paragraph[2]/content/text()[2]").toString().equals("some more mixed content 2"));
    assertTrue("count(/root/paragraph[2]/content/*) = 1",
            context.getValue("count(/root/paragraph[2]/content/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/root/paragraph[2]/content/*[1]) = 'input'",
            context.getValue("name(/root/paragraph[2]/content/*[1])").toString().equals("input"));
    assertTrue("/root/paragraph[2]/content/input/@id = 'i-2'",
            context.getValue("/root/paragraph[2]/content/input/@id").toString().equals("i-2"));
    assertTrue("/root/paragraph[2]/content/input/text()[1] = 'input 2'",
            context.getValue("/root/paragraph[2]/content/input/text()[1]").toString().equals("input 2"));
    assertTrue("count(/root/paragraph[2]/content/input/*) = 0", context
            .getValue("count(/root/paragraph[2]/content/input/*)", Integer.class).toString().equals("0"));
}

From source file:org.chiba.xml.xforms.constraints.RelevanceSelectorTest.java

/**
 * Test case 2 for relevance selection of submitted instance data.
 *
 * @throws Exception if any error occurred during the test.
 *//*from ww  w. jav  a 2  s . c o m*/
public void testSelectRelevant2() throws Exception {
    Instance instance = this.chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    Document document = RelevanceSelector.selectRelevant(instance, "/root/paragraph");
    document.normalize();
    JXPathContext context = JXPathContext.newContext(document);

    assertTrue("name(/*[1]) = 'paragraph'", context.getValue("name(/*[1])").toString().equals("paragraph"));
    assertTrue("/paragraph/@xmlns = ''", context.getValue("/paragraph/@xmlns").toString().equals(""));
    assertTrue("/paragraph/@xmlns:test = 'http://test.org/ns'",
            context.getValue("/paragraph/@xmlns:test").toString().equals("http://test.org/ns"));
    assertTrue("/paragraph/@id = 'p-1'", context.getValue("/paragraph/@id").toString().equals("p-1"));
    assertTrue("count(/paragraph/*) = 1",
            context.getValue("count(/paragraph/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/paragraph/*[1]) = 'content'",
            context.getValue("name(/paragraph/*[1])").toString().equals("content"));
    assertTrue("/paragraph/content/@id = 'c-1'",
            context.getValue("/paragraph/content/@id").toString().equals("c-1"));
    assertTrue("/paragraph/content/text()[1] = 'some mixed content 1'",
            context.getValue("/paragraph/content/text()[1]").toString().equals("some mixed content 1"));
    assertTrue("/paragraph/content/text()[2] = 'some more mixed content 1'",
            context.getValue("/paragraph/content/text()[2]").toString().equals("some more mixed content 1"));
    assertTrue("count(/paragraph/content/*) = 1",
            context.getValue("count(/paragraph/content/*)", Integer.class).toString().equals("1"));

    assertTrue("name(/paragraph/content/*[1]) = 'input'",
            context.getValue("name(/paragraph/content/*[1])").toString().equals("input"));
    assertTrue("/paragraph/content/input/@id = 'i-1'",
            context.getValue("/paragraph/content/input/@id").toString().equals("i-1"));
    assertTrue("/paragraph/content/input/text()[1] = 'input 1'",
            context.getValue("/paragraph/content/input/text()[1]").toString().equals("input 1"));
    assertTrue("count(/paragraph/content/input/*) = 0",
            context.getValue("count(/paragraph/content/input/*)", Integer.class).toString().equals("0"));
}

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

public void testRelativeToText() throws Exception {
    this.chibaBean.setXMLContainer(getXmlResource("resolutionTest8.xhtml"));
    this.chibaBean.init();
    JXPathContext context = this.chibaBean.getContainer().getRootContext();

    String text1 = (String) context.getValue("//chiba:data[1]/text()");
    assertEquals("My other data", text1);

    String text2 = (String) context.getValue("//xforms:label[1]/text()");
    assertEquals("My other label", text2);

}

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

public void testBindRelativeToText() throws Exception {
    this.chibaBean.setXMLContainer(getXmlResource("resolutionTest9.xhtml"));
    this.chibaBean.init();
    JXPathContext context = this.chibaBean.getContainer().getRootContext();

    String text1 = (String) context.getValue("//chiba:data[1]/text()");
    assertEquals("My other data", text1);

    String text2 = (String) context.getValue("//xforms:label[1]/text()");
    assertEquals("My other label", text2);

}

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

private void assertValues(JXPathContext context) {
    String text1 = (String) context.getValue("//chiba:data[1]/text()");
    assertEquals("My data", text1);

    String text2 = (String) context.getValue("//xforms:label[1]/text()");
    assertEquals("My label", text2);
}

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

/**
 * tests the dispatching of an event to an element.
 *
 * @throws Exception __UNDOCUMENTED__//from w  w  w .j av a2s.com
 */
public void testDispatchEvent() throws Exception {
    processor.setXMLContainer(getClass().getResourceAsStream("actions.xhtml"));
    processor.init();
    assertTrue(!processor.dispatch("setvalue-trigger", EventFactory.DOM_ACTIVATE));

    //test the side effect of dispatching the DOMActivate to a setvalue trigger -> the instance has a new value
    Document instance = processor.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument();
    JXPathContext context = JXPathContext.newContext(instance);
    assertTrue(context.getValue("//helloworld").equals("Hello World"));
}

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

/**
 * test, if updateControlValue correctly changes the value and a value-changed event occurs on the relevant
 * control./*from ww  w .  ja  v a 2s  .  com*/
 *
 * @throws Exception
 */
public void testUpdateControlValue1() throws Exception {
    processor.setXMLContainer(getClass().getResourceAsStream("actions.xhtml"));
    processor.init();

    Listener listener = new ChibaBeanTest.Listener();
    JXPathContext context = JXPathContext.newContext(this.processor.getXMLContainer());
    Node node = (Node) context.getPointer("//*[@id='hello-input']").getNode();
    EventTarget eventTarget = (EventTarget) node;
    eventTarget.addEventListener(EventFactory.VALUE_CHANGED, listener, false);

    processor.updateControlValue("hello-input", "Hello new World");
    assertTrue(listener.type.equals(EventFactory.VALUE_CHANGED));
    assertTrue(listener.target.equals("hello-input"));

    //test the side effect of dispatching the DOMActivate to a setvalue trigger -> the instance has a new value
    Document instance = processor.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument();
    JXPathContext context1 = JXPathContext.newContext(instance);
    assertTrue(context1.getValue("//helloworld").equals("Hello new World"));
}

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

/**
 * test, if updateControlValue correctly changes the value and a value-changed event occurs on the relevant control.
 * Check the new updateControlValue method suitable for Upload controls with base64 binary data.
 *
 * @throws Exception// ww  w .j  av a2  s .com
 */
public void testUploadBase64() throws Exception {
    String testFilename = "hello-upload.xhtml";
    String id = "upload-input";
    String nodePath = "//file";
    processor.setXMLContainer(getClass().getResourceAsStream(testFilename));
    processor.init();

    Listener listener = new ChibaBeanTest.Listener();
    JXPathContext context = JXPathContext.newContext(this.processor.getXMLContainer());
    Node node = (Node) context.getPointer("//*[@id='" + id + "']").getNode();
    EventTarget eventTarget = (EventTarget) node;
    eventTarget.addEventListener(EventFactory.VALUE_CHANGED, listener, false);
    BufferedInputStream bf = new BufferedInputStream(getClass().getResourceAsStream(testFilename));
    byte[] bytes = new byte[bf.available()];
    bf.read(bytes);
    String testString = new String(bytes);
    String uploadFilename = "upload-test.txt";
    String uploadMediatype = "text/xml";
    processor.updateControlValue(id, uploadMediatype, uploadFilename, bytes);

    assertTrue(listener.type.equals(EventFactory.VALUE_CHANGED));
    assertTrue(listener.target.equals(id));

    //test the side effect of dispatching the DOMActivate to a setvalue 
    //trigger -> the instance has a new value
    Document instance = processor.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument();
    JXPathContext context1 = JXPathContext.newContext(instance);
    byte[] value = context1.getValue(nodePath).toString().getBytes();
    assertEquals(testString, new String(Base64.decodeBase64(value)));

    //Verify upload properties
    assertEquals(uploadFilename, context1.getValue(nodePath + "/@path").toString());
    assertEquals(uploadMediatype, context1.getValue(nodePath + "/@contentType").toString());
}

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

/**
 * test, if updateControlValue correctly changes the value and a value-changed event occurs on the relevant control.
 * Check the new updateControlValue method suitable for Upload controls with hex binary data.
 *
 * @throws Exception/* w w w . j ava  2 s.  c  o  m*/
 */
public void testUploadHex() throws Exception {
    String testFilename = "hello-upload.xhtml";
    String id = "upload-input-hex";
    String nodePath = "//file2";
    processor.setXMLContainer(getClass().getResourceAsStream(testFilename));
    processor.init();

    Listener listener = new ChibaBeanTest.Listener();
    JXPathContext context = JXPathContext.newContext(this.processor.getXMLContainer());
    Node node = (Node) context.getPointer("//*[@id='" + id + "']").getNode();
    EventTarget eventTarget = (EventTarget) node;
    eventTarget.addEventListener(EventFactory.VALUE_CHANGED, listener, false);
    BufferedInputStream bf = new BufferedInputStream(getClass().getResourceAsStream(testFilename));
    byte[] bytes = new byte[bf.available()];
    bf.read(bytes);
    String testString = new String(bytes);
    String uploadFilename = "upload-test.txt";
    String uploadMediatype = "text/xml";
    processor.updateControlValue(id, uploadMediatype, uploadFilename, bytes);

    assertTrue(listener.type.equals(EventFactory.VALUE_CHANGED));
    assertTrue(listener.target.equals(id));

    //test the side effect of dispatching the DOMActivate to a setvalue
    //trigger -> the instance has a new value
    Document instance = processor.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument();
    JXPathContext context1 = JXPathContext.newContext(instance);
    char[] value = context1.getValue(nodePath).toString().toCharArray();
    assertEquals(testString, new String(Hex.decodeHex(value)));

    //Verify upload properties
    assertEquals(uploadFilename, context1.getValue(nodePath + "/@path").toString());
    assertEquals(uploadMediatype, context1.getValue(nodePath + "/@contentType").toString());

}