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.ChibaBeanTest.java

/**
 * tests the dispatching of an event to an element.
 *
 * @throws Exception __UNDOCUMENTED__//ww w .  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", DOMEventNames.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.ChibaBeanTest.java

/**
 * test, if updateControlValue correctly changes the value and a value-changed event occurs on the relevant
 * control./*  ww w .  j a va2  s .  co  m*/
 *
 * @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/*  ww w .  j av  a  2s . c  om*/
 */
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

/**
 * Creates a new configuration.// w  w w. ja va2 s.c  om
 *
 * @param stream the input stream to be read.
 */
private Config(InputStream stream) throws XFormsConfigException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);

        Document document = factory.newDocumentBuilder().parse(stream);
        JXPathContext context = JXPathContext.newContext(document.getDocumentElement());

        this.properties = load(context, "/properties/property", "name", "value");
        this.stylesheets = load(context, "/stylesheets/stylesheet", "name", "value");
        this.uriResolvers = load(context, "/connectors/uri-resolver", "scheme", "class");
        this.submissionHandlers = load(context, "/connectors/submission-handler", "scheme", "class");
        this.modelItemCalculators = load(context, "/connectors/modelitem-calculator", "scheme", "class");
        this.modelItemValidators = load(context, "/connectors/modelitem-validator", "scheme", "class");
        this.errorMessages = load(context, "/error-messages/message", "id", "value");
        this.instanceSerializerMap = loadSerializer(context, "/register-serializer/instance-serializer",
                "scheme", "method", "mediatype", "class");

        this.extensionFunctions = loadExtensionFunctions(context, "/extension-functions/function");

        //            this.actions = load (context,"/actions/action","name","class");
        //            this.generators = load(context, "/generators/generator", "name", "class");

        connectorFactory = load(context, "/connectors", "factoryClass");
    } catch (Exception e) {
        throw new XFormsConfigException(e);
    }
}

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

/**
 * Creates and loads a new configuration.
 * //from  w  ww. j  a  v  a2  s  . c  o  m
 * @param stream
 *            InputStream to read XML data in the default format.
 */
public DefaultConfig(InputStream stream) throws XFormsConfigException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);

        Document document = factory.newDocumentBuilder().parse(stream);
        JXPathContext context = JXPathContext.newContext(document.getDocumentElement());

        this.properties = load(context, "/properties/property", "name", "value");
        this.stylesheets = load(context, "/stylesheets/stylesheet", "name", "value");
        this.uriResolvers = load(context, "/connectors/uri-resolver", "scheme", "class");
        this.submissionHandlers = load(context, "/connectors/submission-handler", "scheme", "class");
        this.errorMessages = load(context, "/error-messages/message", "id", "value");

        //this.actions = load (context,"/actions/action", "name", "class");
        //this.generators = load(context, "/generators/generator", "name",
        // "class");
        this.extensionFunctions = loadExtensionFunctions(context, "/extension-functions/function");
        this.customElements = loadCustomElements(context, "/custom-elements/element[not(@type) or @type='ui']");

        this.customActionsElements = loadCustomElements(context, "/custom-elements/element[@type='action']");

        this.connectorFactory = load(context, "/connectors", "factoryClass");

        this.instanceSerializerMap = loadSerializer(context, "/register-serializer/instance-serializer",
                "scheme", "method", "mediatype", "class");

    } catch (Exception e) {
        throw new XFormsConfigException(e);
    }
}

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   w w  w .  j  a  v a 2s .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  .  ja  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.constraints.RelevanceSelectorTest.java

/**
 * Test case 1 for relevance selection of submitted instance data.
 *
 * @throws Exception if any error occurred during the test.
 *///w ww  .  j  av a  2s .  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.
 *//*  ww  w.j a v a 2  s  .co  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.Container.java

/**
 * passes the XML container document which contains XForms markup. This method must be called before init() and
 * already builds up the NamespaceCtx and RootContext objects.
 *
 * @param document a DOM Document/*  w  w  w .  j  av a  2 s.c o m*/
 */
public void setDocument(Document document) {
    this.document = document;
    this.root = this.document.getDocumentElement();

    // preprocess document to adjust ids, namespaces, ...
    PreProcessor preProcessor = new PreProcessor();
    preProcessor.setContainer(this);
    preProcessor.setDocument(this.document);
    preProcessor.process();

    this.rootContext = JXPathContext.newContext(root);
}