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.xpath.ChibaExtensionFunctionsTest.java

public void testFileSize() throws Exception {
    //determine size of test input file
    URL url = getClass().getResource("FileFunctionsTest.xhtml");
    File inFile = new File(url.getFile());
    String size = "" + inFile.length();

    ChibaBean chibaBean = initProcessor(inFile);

    Instance instance = chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    JXPathContext context = JXPathContext.newContext(instance);
    String resultofCalc = (String) context.getValue("//filesize");

    assertEquals(size, resultofCalc);/*from w  w  w. jav  a  2  s . co  m*/
}

From source file:org.chiba.xml.xforms.xpath.ChibaExtensionFunctionsTest.java

public void testFileDate() throws Exception {
    //determine lastmodified date of test input file
    URL url = getClass().getResource("FileFunctionsTest.xhtml");
    File inFile = new File(url.getFile());
    long modified = inFile.lastModified();

    //test default date formatting when argument 'format' is omitted
    Calendar calendar = new GregorianCalendar(Locale.getDefault());
    calendar.setTimeInMillis(modified);//www.  ja  va2s  .c o  m

    SimpleDateFormat simple1 = new SimpleDateFormat("dd.MM.yyyy H:m:s");
    String s = simple1.format(calendar.getTime());

    ChibaBean chibaBean = initProcessor(inFile);

    Instance instance = chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    JXPathContext context = JXPathContext.newContext(instance);
    String resultofCalc = (String) context.getValue("//filedate1");

    //        DOMUtil.prettyPrintDOM(chibaBean.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument());
    assertEquals(s, resultofCalc);

    //test date formatting with given pattern
    SimpleDateFormat simple2 = new SimpleDateFormat("yyyy.MM.dd");
    s = simple2.format(calendar.getTime());

    resultofCalc = (String) context.getValue("//filedate2");
    assertEquals(s, resultofCalc);

}

From source file:org.chiba.xml.xforms.xpath.test.ChibaExtensionFunctionsTest.java

public void testFileSize() throws Exception {
    //determine size of test input file
    URL url = getClass().getResource("FileFunctionsTest.xml");
    File inFile = new File(url.getFile());
    String size = "" + inFile.length();

    ChibaBean chibaBean = initProcessor(inFile);

    Instance instance = chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    JXPathContext context = JXPathContext.newContext(instance);
    String resultofCalc = (String) context.getValue("//filesize");

    assertEquals(size, resultofCalc);/* www  . ja v a2  s. c  o  m*/
}

From source file:org.chiba.xml.xforms.xpath.test.ChibaExtensionFunctionsTest.java

public void testFileDate() throws Exception {
    //determine lastmodified date of test input file
    URL url = getClass().getResource("FileFunctionsTest.xml");
    File inFile = new File(url.getFile());
    long modified = inFile.lastModified();

    //test default date formatting when argument 'format' is omitted
    Calendar calendar = new GregorianCalendar(Locale.getDefault());
    calendar.setTimeInMillis(modified);/*w w  w.jav a  2s .  c om*/

    SimpleDateFormat simple1 = new SimpleDateFormat("dd.MM.yyyy H:m:s");
    String s = simple1.format(calendar.getTime());

    ChibaBean chibaBean = initProcessor(inFile);

    Instance instance = chibaBean.getContainer().getDefaultModel().getDefaultInstance();
    JXPathContext context = JXPathContext.newContext(instance);
    String resultofCalc = (String) context.getValue("//filedate1");

    //        DOMUtil.prettyPrintDOM(chibaBean.getContainer().getDefaultModel().getDefaultInstance().getInstanceDocument());
    assertEquals(s, resultofCalc);

    //test date formatting with given pattern
    SimpleDateFormat simple2 = new SimpleDateFormat("yyyy.MM.dd");
    s = simple2.format(calendar.getTime());

    resultofCalc = (String) context.getValue("//filedate2");
    assertEquals(s, resultofCalc);

}

From source file:org.chiba.xml.xforms.xpath.test.ExtensionFunctionsTest.java

/**
 * __UNDOCUMENTED__/*w w w.  jav a2 s  . com*/
 *
 * @throws Exception __UNDOCUMENTED__
 */
protected void setUp() throws Exception {
    Document document = getXmlResource("functions.xml");

    //dirty fix
    ChibaBean chibaBean = new ChibaBean();
    chibaBean.setXMLContainer(getClass().getResourceAsStream("ChibaExtensionFunctionsTest.xml"));
    chibaBean.init();

    XPathExtensionFunctions functions = new XPathExtensionFunctions();
    functions.setNamespaceContext(document.getDocumentElement());

    this.context = JXPathContext.newContext(document);
    this.context.setFunctions(functions);
}

From source file:org.chiba.xml.xforms.xpath.test.InstanceFactoryTest.java

/**
 * Sets up the test./*from   w  w w .  j a  va2 s  .c  o  m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    Document testDocument = factory.newDocumentBuilder()
            .parse(getClass().getResourceAsStream("InstanceFactoryTest.xml"));
    Element instanceElement = (Element) testDocument.getElementsByTagNameNS(NamespaceCtx.XFORMS_NS, "instance")
            .item(0);
    InstanceFactory instanceFactory = new InstanceFactory();
    instanceFactory.setNamespaceContext(instanceElement);

    this.document = factory.newDocumentBuilder().newDocument();
    this.context = JXPathContext.newContext(this.document);
    this.context.setFactory(instanceFactory);

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

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

From source file:org.chiba.xml.xforms.xpath.test.JXPathTest.java

/**
 * Sets up the test./*  ww w.  j  a v  a  2 s  . c o m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(getClass().getResourceAsStream("JXPathTest.xhtml"));

    this.context = JXPathContext.newContext(document);
    this.context.registerNamespace("html", "http://www.w3.org/2002/06/xhtml2");
    this.context.registerNamespace("xf", "http://www.w3.org/2002/xforms");
    this.context.registerNamespace("ev", "http://www.w3.org/2001/xml-events");
    this.context.registerNamespace("", "");
    this.context.registerNamespace("my", "http://tempuri.org/my");
}

From source file:org.chiba.xml.xforms.xpath.test.PredicateTest.java

/**
 * Sets up the test./*w w w . ja  v a  2s . c  o m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(PredicateTest.class.getResourceAsStream("PredicateTest.xml"));
    this.rootContext = JXPathContext.newContext(document);

    this.pointer = this.rootContext.getPointer("/people/new/@name");
    this.relativeContext = this.rootContext.getRelativeContext(this.pointer);
}

From source file:org.chiba.xml.xforms.xpath.XFormsExtensionFunctionsTest.java

public void testCurrent() throws Exception {

    assertEquals("8023.451", this.defaultContext.getValue("instance('instance-3')/convertedAmount"));

    // DOMUtil.prettyPrintDOM(this.chibaBean.getXMLContainer());

    JXPathContext context = JXPathContext.newContext(this.chibaBean.getXMLContainer());
    context.setLenient(true);//from   w w  w . j  a v a 2 s  . co  m
    assertEquals("Jan", context.getValue("//xf:repeat[2]/xf:group[1]//xf:output/chiba:data[1]"));
    assertEquals("Feb", context.getValue("//xf:repeat[2]/xf:group[2]//xf:output/chiba:data[1]"));
    assertEquals("Mar", context.getValue("//xf:repeat[2]/xf:group[3]//xf:output/chiba:data[1]"));

    this.chibaBean.dispatch("current-trigger", DOMEventNames.ACTIVATE);
    // DOMUtil.prettyPrintDOM(chibaBean.getContainer().getDefaultModel().getInstance("i1").getInstanceDocument());
    assertEquals("Jan", this.defaultContext.getValue("instance('i1')/mon/@result"));

}

From source file:org.chiba.xml.xpath.impl.JXPathDOMFactoryTest.java

/**
 * Sets up the test./*from  w w w .  j  a v a2 s . c  o  m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    Document testDocument = factory.newDocumentBuilder()
            .parse(getClass().getResourceAsStream("JXPathDOMFactoryTest.xhtml"));
    Element instanceElement = (Element) testDocument
            .getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "instance").item(0);
    JXPathDOMFactory jxpathFactory = new JXPathDOMFactory();
    jxpathFactory.setNamespaceContext(instanceElement);

    this.document = factory.newDocumentBuilder().newDocument();
    this.context = JXPathContext.newContext(this.document);
    this.context.setFactory(jxpathFactory);

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

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