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.forms.binding.JXPathBindingBase.java

private void applyLeniency(JXPathContext jxpc) {
    if (this.commonAtts.leniency != null) {
        jxpc.setLenient(this.commonAtts.leniency.booleanValue());
    }/* ww  w  .ja  v a  2 s  . c o  m*/
}

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

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);

        AbstractFactory jxPathFactory;/*from  ww w  .  jav a 2s .c o m*/
        if (commonAtts.jxPathFactory != null)
            jxPathFactory = commonAtts.jxPathFactory;
        else
            jxPathFactory = new BindingJXPathFactory();
        jxpc.setFactory(jxPathFactory);
    } else {
        jxpc = (JXPathContext) objModel;
    }
    return jxpc;
}

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

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;// w  w w .  j  ava 2 s . co 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(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
    if (this.nullable) {
        final AttributesImpl voidAttrs = new AttributesImpl();
        voidAttrs.addCDATAAttribute("value", "");
        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);

        if (this.nullText != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);

            if (this.nullTextIsI18nKey) {
                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(this.nullText, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(this.nullText).toSAX(contentHandler);
                }
            } else {
                contentHandler.characters(this.nullText.toCharArray(), 0, this.nullText.length());
            }

            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }

        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    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(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
        if (label != null) {
            contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
            if (label instanceof XMLizable) {
                ((XMLizable) label).toSAX(contentHandler);
            } else if (this.labelIsI18nKey) {
                String stringLabel = label.toString();

                if ((this.i18nCatalog != null) && (this.i18nCatalog.trim().length() > 0)) {
                    new I18nMessage(stringLabel, this.i18nCatalog).toSAX(contentHandler);
                } else {
                    new I18nMessage(stringLabel).toSAX(contentHandler);
                }
            } else {
                String stringLabel = label.toString();
                contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
            }
            contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL,
                    FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
        }
        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL,
                FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
    }

    // End the selection-list
    contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL,
            FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
}

From source file:org.apache.cocoon.forms.formmodel.WidgetTestHelper.java

public static void assertXPathEquals(String message, String expected, String xpath, Document doc) {
    JXPathContext ctx = JXPathContext.newContext(doc);
    ctx.setLenient(true);
    Assert.assertEquals(message, expected, ctx.getValue(xpath));
}

From source file:org.apache.cocoon.forms.formmodel.WidgetTestHelper.java

public static void assertXPathExists(String message, String xpath, Document doc) {
    JXPathContext ctx = JXPathContext.newContext(doc);
    ctx.setLenient(true);
    Pointer pointer = ctx.getPointer(xpath);
    Assert.assertNotNull(message, pointer.getNode());
}

From source file:org.apache.cocoon.forms.formmodel.WidgetTestHelper.java

public static void assertXPathNotExists(String message, String xpath, Document doc) {
    JXPathContext ctx = JXPathContext.newContext(doc);
    ctx.setLenient(true);
    Pointer pointer = ctx.getPointer(xpath);
    Assert.assertNull(message, pointer.getNode());
}

From source file:org.apache.cocoon.generation.JXTemplateGenerator.java

static private Object getValue(JXTExpression expr, JexlContext jexlContext, JXPathContext jxpathContext,
        Boolean lenient) throws Exception {
    if (expr != null) {
        Object compiled = expr.compiledExpression;
        try {/*w w  w. j a  v  a  2 s. co m*/
            if (compiled instanceof CompiledExpression) {
                CompiledExpression e = (CompiledExpression) compiled;
                boolean oldLenient = jxpathContext.isLenient();
                if (lenient != null) {
                    jxpathContext.setLenient(lenient.booleanValue());
                }
                try {
                    return e.getValue(jxpathContext);
                } finally {
                    jxpathContext.setLenient(oldLenient);
                }
            } else if (compiled instanceof Expression) {
                Expression e = (Expression) compiled;
                return e.evaluate(jexlContext);
            }
            return compiled;
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            if (t instanceof Exception) {
                throw (Exception) t;
            }
            throw (Error) t;
        }
    } else {
        return null;
    }
}

From source file:org.apache.cocoon.generation.JXTemplateGenerator.java

private Object getNode(JXTExpression expr, JexlContext jexlContext, JXPathContext jxpathContext,
        Boolean lenient) throws Exception {
    try {/*from www .  j av a 2 s  . c  o  m*/
        Object compiled = expr.compiledExpression;
        if (compiled instanceof CompiledExpression) {
            CompiledExpression e = (CompiledExpression) compiled;
            boolean oldLenient = jxpathContext.isLenient();
            if (lenient != null)
                jxpathContext.setLenient(lenient.booleanValue());
            try {
                Iterator iter = e.iteratePointers(jxpathContext);
                if (iter.hasNext()) {
                    Pointer first = (Pointer) iter.next();
                    if (iter.hasNext()) {
                        List result = new LinkedList();
                        result.add(first.getNode());
                        boolean dom = (first.getNode() instanceof Node);
                        while (iter.hasNext()) {
                            Object obj = ((Pointer) iter.next()).getNode();
                            dom = dom && (obj instanceof Node);
                            result.add(obj);
                        }
                        Object[] arr;
                        if (dom) {
                            arr = new Node[result.size()];
                        } else {
                            arr = new Object[result.size()];
                        }
                        result.toArray(arr);
                        return arr;
                    }
                    return first.getNode();
                }
                return null;
            } finally {
                jxpathContext.setLenient(oldLenient);
            }
        } else if (compiled instanceof Expression) {
            Expression e = (Expression) compiled;
            return e.evaluate(jexlContext);
        }
        return expr.raw;
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t instanceof Exception) {
            throw (Exception) t;
        }
        throw (Error) t;
    }
}

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

/**
 * Redefines the Binding action as working on a JXPathContext Type rather
 * then on generic objects./*from   w w  w.  j  a va 2 s.  c o  m*/
 * Executes the actual loading via {@link #doLoad(Widget, JXPathContext)}
 * depending on the configured value of the "direction" attribute.
 */
public final void loadFormFromModel(Widget frmModel, JXPathContext jxpc) throws BindingException {
    boolean inheritedLeniency = jxpc.isLenient();
    applyLeniency(jxpc);
    if (this.commonAtts.loadEnabled) {
        doLoad(frmModel, jxpc);
    }
    jxpc.setLenient(inheritedLeniency);
}

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

/**
 * Redefines the Binding action as working on a JXPathContext Type rather
 * then on generic objects./* w w  w. jav  a  2s  . c o  m*/
 * Executes the actual saving via {@link #doSave(Widget, JXPathContext)}
 * depending on the configured value of the "direction" attribute.
 */
public final void saveFormToModel(Widget frmModel, JXPathContext jxpc) throws BindingException {
    boolean inheritedLeniency = jxpc.isLenient();
    applyLeniency(jxpc);
    if (this.commonAtts.saveEnabled) {
        doSave(frmModel, jxpc);
    }
    jxpc.setLenient(inheritedLeniency);
}