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

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

Introduction

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

Prototype

public synchronized void setLenient(boolean lenient) 

Source Link

Document

If the context is in the lenient mode, then getValue() returns null for inexistent paths.

Usage

From source file:org.apache.cocoon.woody.binding.JXPathBindingBase.java

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);
        if (objModel instanceof Node) {
            jxpc.setFactory(new DOMFactory());
        }/*from w w w  .  j a v a2 s .  c  o  m*/
    } else {
        jxpc = (JXPathContext) objModel;
    }
    return jxpc;
}

From source file:org.apache.cocoon.woody.datatype.FlowJXPathSelectionList.java

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;//from w  w w  .  j  av a2s. c o m
    Iterator iter = null;
    if (model == null) {
        Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
        if (flowData == null) {
            throw new SAXException("No flow data to produce selection list");
        }

        // Move to the list location
        ctx = JXPathContext.newContext(flowData);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(this.listPath);
    } else {
        // Move to the list location
        ctx = JXPathContext.newContext(model);

        // Iterate on all elements of the list
        iter = ctx.iteratePointers(".");
    }

    // Start the selection-list
    contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);

    while (iter.hasNext()) {
        String stringValue = "";
        Object label = null;

        // Get a context on the current item
        Pointer ptr = (Pointer) iter.next();
        if (ptr.getValue() != null) {
            JXPathContext itemCtx = ctx.getRelativeContext(ptr);

            // Get the value as a string
            Object value = itemCtx.getValue(this.valuePath);

            // List may contain null value, and (per contract with convertors),
            // convertors are not invoked on nulls.
            if (value != null) {
                stringValue = this.datatype.convertToString(value, locale);
            }

            // Get the label (can be ommitted)
            itemCtx.setLenient(true);
            label = itemCtx.getValue(this.labelPath);
            if (label == null) {
                label = stringValue;
            }
        }

        // Output this item
        AttributesImpl itemAttrs = new AttributesImpl();
        itemAttrs.addCDATAAttribute("value", stringValue);
        contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL,
                    Constants.EMPTY_ATTRS);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL,
            Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.chiba.xml.xforms.action.InsertAction.java

/**
 * Performs the <code>insert</code> action.
 *
 * @return always <code>true</code>.
 * @throws org.chiba.xml.xforms.exception.XFormsException if an error
 * occurred during <code>insert</code> processing.
 */// www . j a  va2 s .c  o  m
public boolean perform() throws XFormsException {
    // get instance data
    String instanceId = getInstanceId(getLocationPath());
    Instance instance = this.model.getInstance(instanceId);

    // get path information
    String locationPath = stripInstanceFunction(getLocationPath());
    int contextSize = instance.countNodeset(locationPath);

    // set defaults
    String insertPath = locationPath;
    int insertPosition = 1;

    // check for existing path
    if (contextSize > 0) {
        // canonicalize path
        JXPathContext context = instance.getInstanceContext();
        context.setLenient(true);
        String instancePath = context.getPointer(locationPath + "[round(" + this.atAttribute + ")]").asPath();
        context.setLenient(false);

        if (instancePath.equals("null()")) {
            // caused by NaN
            insertPath = PathUtil.stripLastPredicate(context.getPointer(locationPath).asPath());
            insertPosition = contextSize + 1;
        } else {
            // strip down to insert path and position
            insertPath = PathUtil.stripLastPredicate(instancePath);
            contextSize = instance.countNodeset(insertPath);
            String lastStep = PathUtil.lastStep(instancePath);
            insertPosition = PathUtil.stepIndex(lastStep);

            // sanity checks (see 9.3.5)
            if (insertPosition < 1) {
                insertPosition = 1;
            }
            if (insertPosition > contextSize) {
                insertPosition = contextSize;
            }

            // check position specifier
            if (this.positionAttribute.equals("after")) {
                insertPosition++;
            }
        }
    }

    // insert specified node and dispatch notification event
    instance.insertNode(insertPath, insertPosition);
    this.container.dispatch(instance.getTarget(), EventFactory.INSERT, insertPath + "[" + insertPosition + "]");

    // update behaviour
    setDeferredRebuild(this.model.getId(), true);
    setDeferredRecalculate(this.model.getId(), true);
    setDeferredRevalidate(this.model.getId(), true);
    setDeferredRefresh(this.model.getId(), true);

    // always indicate success
    return true;
}

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);
    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.codehaus.mojo.javascript.assembler.JsBuilderAssemblerReader.java

/**
 * {@inheritDoc}/*from ww  w.j a  va2s. c  om*/
 * 
 * @see org.codehaus.mojo.javascript.assembler.AssemblerReader#getAssembler(java.io.File)
 */
public Assembler getAssembler(File file) throws Exception {
    logger.info("Reading assembler descriptor " + file.getAbsolutePath());
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document dom = builder.parse(file);

    Assembler assembler = new Assembler();
    JXPathContext xpath = JXPathContext.newContext(dom);
    xpath.setLenient(true);
    String src = (String) xpath.getValue("//directory/@name");
    List nodes = xpath.selectNodes("//target");
    for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
        Node node = (Node) iterator.next();
        Script script = new Script();
        assembler.addScript(script);
        JXPathContext nodeContext = JXPathContext.newContext(node);
        String fileName = (String) nodeContext.getValue("@file");
        fileName = fileName.replace('\\', '/');
        if (fileName.startsWith(OUTPUT)) {
            fileName = fileName.substring(OUTPUT.length());
        }
        script.setFileName(fileName);

        for (Iterator iter = nodeContext.iterate("//include/@name"); iter.hasNext();) {
            String include = ((String) iter.next()).replace('\\', '/');
            if (src != null && src.length() > 0) {
                include = include.substring(src.length() + 1);
            }
            script.addInclude(include);
        }
    }
    return assembler;
}

From source file:org.commonjava.maven.galley.maven.parse.JXPathUtils.java

public static JXPathContext newContext(final Node node) {
    final JXPathContext ctx = JXPathContext.newContext(node);
    ctx.setLenient(true);
    ctx.setFunctions(new ClassFunctions(ResolveFunctions.class, "ext"));

    return ctx;/*from ww  w. j  a v  a2  s . c o  m*/
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathValue(JXPathContext ctx, String xpath, Object expected) {
    ctx.setLenient(false);
    Object actual = ctx.getValue(xpath);
    assertEquals("Evaluating <" + xpath + ">", expected, actual);
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathValue(JXPathContext ctx, String xpath, Object expected, Class resultType) {
    ctx.setLenient(false);
    Object actual = ctx.getValue(xpath, resultType);
    assertEquals("Evaluating <" + xpath + ">", expected, actual);
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathValueLenient(JXPathContext ctx, String xpath, Object expected) {
    ctx.setLenient(true);
    Object actual = ctx.getValue(xpath);
    ctx.setLenient(false);// w  w w .  ja v  a2 s . c  om
    assertEquals("Evaluating lenient <" + xpath + ">", expected, actual);
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathPointer(JXPathContext ctx, String xpath, String expected) {
    ctx.setLenient(false);
    Pointer pointer = ctx.getPointer(xpath);
    String actual = pointer.toString();
    assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
}