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.core.Instance.java

/**
 * initialize JXPath Context correctly to contain all namespaces in scope.
 *///  w ww . ja v a 2s  .c o  m
protected void initInstanceContext() {
    // create instance context
    JXPathDOMFactory factory = new JXPathDOMFactory();
    factory.setNamespaceContext(this.element);

    ExtensionFunctions functions = new ExtensionFunctions();
    functions.setNamespaceContext(this.element);

    this.instanceContext = JXPathContext.newContext(this);
    this.instanceContext.setFactory(factory);
    this.instanceContext.setFunctions(functions);

    Map namespaces = NamespaceResolver.getAllNamespaces(this.element);
    Iterator iterator = namespaces.keySet().iterator();
    while (iterator.hasNext()) {
        String prefix = (String) iterator.next();
        String uri = (String) namespaces.get(prefix);

        this.instanceContext.registerNamespace(prefix, uri);
    }
}

From source file:org.chiba.xml.xforms.core.RefreshTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    chibaBean = new ChibaBean();
    String path = getClass().getResource(resourceName).getPath();
    chibaBean.setBaseURI("file://" + path.substring(0, path.lastIndexOf(resourceName)));
    chibaBean.setXMLContainer(getClass().getResourceAsStream(resourceName));
    chibaBean.init();//from   w  w w  . ja v a  2  s  .  c  o  m
    context = JXPathContext.newContext(chibaBean.getContainer().getDocument());
}

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

/**
 * Performs element init.//w w w.  java 2  s  .com
 *
 * @throws XFormsException if any error occurred during init.
 */
public void init() throws XFormsException {
    if (getLogger().isDebugEnabled()) {
        getLogger().debug(this + " init");
    }

    // create instance context
    InstanceFactory factory = new InstanceFactory();
    factory.setNamespaceContext(this.element);

    XPathExtensionFunctions functions = new XPathExtensionFunctions();
    functions.setNamespaceContext(this.element);

    // load initial instance
    this.initialInstance = getInitiallInstance();

    // create instance document
    this.instanceDocument = createInstanceDocument();

    this.instanceContext = JXPathContext.newContext(this);
    this.instanceContext.setFactory(factory);
    this.instanceContext.setFunctions(functions);

    Map namespaces = NamespaceCtx.getAllNamespaces(this.element);
    Iterator iterator = namespaces.keySet().iterator();
    while (iterator.hasNext()) {
        String prefix = (String) iterator.next();
        String uri = (String) namespaces.get(prefix);

        this.instanceContext.registerNamespace(prefix, uri);
    }

    //id handling
    //        this.instanceDataItems = new HashMap();
    //        createIds();
}

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

private JXPathContext getOriginalContext() {
    if (this.originalContext == null) {
        try {//from  www.  j a  va2  s.  c  o  m
            this.originalContext = JXPathContext.newContext(createInstanceDocument());
        } catch (XFormsException e) {
            // todo: error handling
            e.printStackTrace();
        }
    }

    return this.originalContext;
}

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

/**
 * tests the dispatching of an event to an element.
 *
 * @throws Exception __UNDOCUMENTED__//from  www.  j  a  va 2s. c  o  m
 */
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./* w w w.j  a v a2s.  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  va2 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/*from  w  w  w.j  a va 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/*from   ww w . j  a  va2  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());

}

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//ww w .  j a v  a 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());

}