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

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

Introduction

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

Prototype

public abstract Pointer getPointer(String xpath);

Source Link

Document

Traverses the xpath and returns a Pointer.

Usage

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

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

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

    processor.updateControlValue("hello-input", "Hello new World");
    assertTrue(listener.getType().equals(XFormsEventNames.VALUE_CHANGED));
    assertTrue(listener.getId().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.ChibaBeanTest.java

/**
 * test if updateControlValue suppresses update for unchanged value.
 *
 * @throws Exception/*from w  ww  .j a va2s  .  com*/
 */
public void testUpdateControlValue2() throws Exception {
    processor.setXMLContainer(getClass().getResourceAsStream("actions.xhtml"));
    processor.init();

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

    processor.updateControlValue("hello-input", "Hello World");
    //value mustn't change cause value of node is 'Hello World' initially - test the case that setValue isn't called
    assertNull(listener.getType());
    assertNull(listener.getId());
}

From source file:org.chiba.xml.xforms.config.Config.java

/**
 * Returns the specified configuration value in a String.
 *
 * @param configContext the JXPath context holding the configuration.
 * @param path          the context relative path to the section.
 * @param nameAttribute the name of the attribute to load from
 * @return the specified configuration value in a String
 * @throws Exception if any error occured during configuration loading.
 *///from w  w w.  ja v  a 2s  .  c om
private String load(JXPathContext configContext, String path, String nameAttribute) throws Exception {
    String value;

    Pointer pointer = configContext.getPointer(path);
    Element element = (Element) pointer.getNode();
    value = element.getAttribute(nameAttribute);

    return value;
}

From source file:org.chiba.xml.xforms.connector.context.ContextResolver.java

/**
 * Performs a lookup in Chiba's context map and returns the object denoted
 * by the scheme specific part of the resolver's URI.
 *
 * @return the object denoted by the scheme specific part of the
 * resolver's URI.//from ww w . j av a 2 s .co  m
 * @throws XFormsException if any error occurred during link traversal.
 */
public Object resolve() throws XFormsException {
    try {
        String xpath = new URI(getURI()).getSchemeSpecificPart();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("resolve: " + xpath);
        }

        JXPathContext contextContext = JXPathContext.newContext(getContext());
        Pointer pointer = contextContext.getPointer(xpath);
        if (pointer.getValue() == null) {
            throw new JXPathException("No pointer for xpath: " + xpath);
        }

        return pointer.getNode();
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:org.chiba.xml.xforms.connector.context.test.ContextSubmissionHandlerTest.java

public void testSubmit() throws Exception {
    Document doc = getXmlResource("ContextSubmissionTest.xhtml");
    chibaBean.setXMLContainer(doc);//  w w w .j  a  v  a 2 s  .  c  om
    chibaBean.init();
    chibaBean.dispatch("trigger", "DOMActivate");

    JXPathContext context = JXPathContext.newContext(doc);
    Element instance = (Element) context.getPointer("//data").getNode();

    Document doc2 = (Document) chibaBean.getContext().get("key");
    assertTrue(getComparator().compare(instance, doc2.getDocumentElement()));
}

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  .j a  v  a  2s  .c o  m
 *
 * @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 suppresses update for unchanged value.
 *
 * @throws Exception/*from ww w  .  j  a  v  a2 s  .co m*/
 */
public void testUpdateControlValue2() 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 World");
    //value mustn't change cause value of node is 'Hello World' initially - test the case that setValue isn't called
    assertNull(listener.type);
    assertNull(listener.target);
}

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//www.j  a  v  a 2  s .c  o m
 */
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.  ja va  2  s .  c  om*/
 */
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());

}

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 anyURI type data.
 *
 * @throws Exception//from   w w w. j  a va  2s.c  o m
 */
public void testUploadAnyURI() throws Exception {
    String testFilename = "hello-upload.xhtml";
    String id = "upload-uri";
    String nodePath = "//file4";
    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);
    /* the content must be the URI !!
    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,
            getClass().getResource(testFilename).toString().getBytes());

    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);
    String value = context1.getValue(nodePath).toString();
    assertEquals(getClass().getResource(testFilename).toString(), value);

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

}