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:com.idega.chiba.web.xml.xforms.elements.action.IdegaDeleteAction.java

/**
 * Performs the <code>delete</code> action for all nodes.
 * /*from w  w  w. j a v a  2s  .co m*/
 * @throws XFormsException
 *             if an error occurred during <code>delete</code> processing.
 */
public void perform() throws XFormsException {
    super.perform();

    // get instance and nodeset information
    Instance instance = this.model.getInstance(getInstanceId());
    String pathExpression = getLocationPath();
    int contextSize = instance.countNodeset(pathExpression);
    if (contextSize == 0) {
        getLogger().warn(this + " perform: nodeset '" + pathExpression + "' is empty");
        return;
    }

    String path = null;
    // since jxpath doesn't provide a means for evaluating an expression
    // in a certain context, we use a trick here: the expression will be
    // evaluated during getPointer and the result stored as a variable
    JXPathContext context = instance.getInstanceContext();
    boolean lenient = context.isLenient();
    context.setLenient(true);
    context.getPointer(pathExpression + "[chiba:declare('delete-position', 1)]");
    context.setLenient(lenient);

    // since jxpath's round impl is buggy (returns 0 for NaN) we perform
    // 'round' manually
    double value = ((Double) context.getValue("number(chiba:undeclare('delete-position'))")).doubleValue();
    long position = Math.round(value);
    if (Double.isNaN(value) || position < 1 || position > contextSize) {
        getLogger().warn(this + " perform: expression '1' does not point to an existing node");
        return;
    }

    path = new StringBuffer(pathExpression).append('[').append(position).append(']').toString();

    // delete specified node and dispatch notification event
    while (instance.existsNode(path)) {
        try {
            instance.deleteNode(path);
            getLogger().info("Node deleted in delete action:" + path);
        } catch (JXPathException e) {
            getLogger().warn("Unable to delete:" + path);
        }
    }

    if (StringUtil.isEmpty(path)) {
        return;
    }

    this.container.dispatch(instance.getTarget(), XFormsEventNames.DELETE, path);

    // update behaviour
    doRebuild(true);
    doRecalculate(true);
    doRevalidate(true);
    doRefresh(true);
}

From source file:com.lrodriguez.SVNBrowser.java

private void doXpathQuery(HttpServletRequest request, HttpServletResponse response, HttpSession session)
        throws ServletException, IOException {
    //example//from ww w  .ja va  2s  .c  o  m
    //?xpathQuery=.[revision < 9315 and revision>9000 and author='echow' or author ='lrodrigu' and type='A']
    logDebug("dispatching xpath query: " + request.getParameter(XPATH_QUERY));

    SVNRepository repository = ((SVNHttpSession) request.getSession().getAttribute(SVNSESSION)).getRepository();
    Collection svnLogEntries = null;
    try {
        svnLogEntries = getSVNLogEntries(request, 0, repository.getDatedRevision(new Date()));
    } catch (SVNException e) {
        e.printStackTrace();
        request.setAttribute(ERROR, e.getErrorMessage());
        request.getRequestDispatcher(INDEX_JSP).forward(request, response);
        return;
    }
    List entriesList = new ArrayList();
    if (svnLogEntries != null && svnLogEntries.size() > 0) {
        List entryFacadeList = getAllEntries(svnLogEntries);

        JXPathContext context = JXPathContext.newContext(entryFacadeList);
        context.setLenient(true);
        for (Iterator iter = context.iterate(request.getParameter(XPATH_QUERY)); iter.hasNext();) {
            Object currEntryFacade = iter.next();
            entriesList.add(currEntryFacade);
        }
    }
    session.setAttribute(UNIQUE_ENTRIES, entriesList);
    request.getRequestDispatcher(INDEX_JSP).forward(request, response);
    return;
}

From source file:fr.cls.atoll.motu.library.misc.data.CatalogData.java

static public List<Object> findJaxbElementUsingJXPath(Object object, String xPath) {

    List<Object> listObjectFound = new ArrayList<Object>();

    JXPathContext context = JXPathContext.newContext(object);
    context.setLenient(true);
    // Object oo =
    // context.getValue("//threddsMetadataGroup[name='{http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0}serviceName']/value");
    // Object oo =
    // context.getValue("//threddsMetadataGroup[name='{http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0}serviceName']/value");
    // Iterator it =
    // context.iterate("//threddsMetadataGroup[name='{http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0}serviceName']/value");
    Iterator<?> it = context.iterate(xPath);
    while (it.hasNext()) {
        listObjectFound.add(it.next());/*from   w w w .j  av  a 2  s  .co m*/
    }
    return listObjectFound;
}

From source file:org.apache.camel.language.jxpath.JXPathExpression.java

public <T> T evaluate(Exchange exchange, Class<T> tClass) {
    try {//from w ww  . j a  v  a  2 s  . c  o  m
        JXPathContext context = JXPathContext.newContext(exchange);
        context.setLenient(lenient);
        Object result = getJXPathExpression().getValue(context, type);
        assertResultType(exchange, result);
        return exchange.getContext().getTypeConverter().convertTo(tClass, result);
    } catch (JXPathException e) {
        throw new ExpressionEvaluationException(this, exchange, e);
    }
}

From source file:org.apache.cocoon.components.expression.jxpath.JXPathExpression.java

private JXPathContext getContext(ExpressionContext context) {
    // This could be made more efficient by caching the
    // JXPathContext within the Context object.
    JXPathContext jxcontext = JXPathContext.newContext(context.getContextBean());
    jxcontext.setVariables(new VariableAdapter(context));
    jxcontext.setLenient(this.lenient);
    jxcontext.setNamespaceContextPointer(new NamespacesTablePointer(context.getNamespaces()));
    return jxcontext;
}

From source file:org.apache.cocoon.components.modules.input.JXPathHelper.java

/**
 * Actually add global functions and packages as well as those
 * listed in the configuration object.//from   ww  w  . j a  v a2s. c o  m
 *
 * @param context a <code>JXPathContext</code> value
 * @param conf a <code>Configuration</code> value holding local
 * packages and functions.
 */
private static void setup(JXPathHelperConfiguration setup, JXPathContext context, Configuration conf)
        throws ConfigurationException {

    // Create local config (if necessary)
    JXPathHelperConfiguration local = conf == null ? setup : new JXPathHelperConfiguration(setup, conf);

    // Setup context with local config
    context.setLenient(setup.isLenient());
    context.setFunctions(local.getLibrary());
    if (local.getNamespaces() != null) {
        for (Iterator i = local.getNamespaces().entrySet().iterator(); i.hasNext();) {
            final Map.Entry entry = (Map.Entry) i.next();
            context.registerNamespace((String) entry.getKey(), (String) entry.getValue());
        }
    }
}

From source file:org.apache.cocoon.components.source.impl.XModuleSource.java

public void notify(Document insertDoc) throws SAXException {

    // handle xpaths, we are only handling inserts, i.e. if there is no
    // attribute of the given name and type the operation will fail
    if (!(this.xPath.length() == 0 || this.xPath.equals("/"))) {

        Object value = getInputAttribute(this.attributeType, this.attributeName);
        if (value == null)
            throw new SAXException(" The attribute: " + this.attributeName + " is empty");

        JXPathContext context = JXPathContext.newContext(value);

        if (value instanceof Document) {
            // If the attribute contains a dom document we
            // create the elements in the given xpath if
            // necesary, import the input document and put it
            // in the place described by the xpath.
            Document doc = (Document) value;

            Node importedNode = doc.importNode(insertDoc.getDocumentElement(), true);

            context.setLenient(true);
            context.setFactory(new DOMFactory());
            context.createPathAndSetValue(this.xPath, importedNode);
        } else {//from  w  ww  . j  a v a 2s . co  m
            // Otherwise just try to put a the input document in
            // the place pointed to by the xpath
            context.setValue(this.xPath, insertDoc);
        }

    } else {
        setOutputAttribute(this.attributeType, this.attributeName, insertDoc);
    }
}

From source file:org.apache.cocoon.el.impl.jxpath.JXPathExpression.java

private JXPathContext getContext(ObjectModel objectModel) {
    // This could be made more efficient by caching the
    // JXPathContext within the Context object.

    JXPathContext jxobjectModel = JXPathContext.newContext(objectModel.get(ObjectModel.CONTEXTBEAN));
    jxobjectModel.setVariables(new VariableAdapter(objectModel));
    jxobjectModel.setLenient(this.lenient);
    jxobjectModel.setNamespaceContextPointer(
            new NamespacesTablePointer((NamespacesTable) objectModel.get(ObjectModel.NAMESPACE)));
    return jxobjectModel;
}

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

/**
 * Redefines the Binding action as working on a JXPathContext Type rather
 * then on generic objects./*  ww  w  . ja  v  a2 s  .  co  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);
    applyNSDeclarations(jxpc);
    if (this.commonAtts.loadEnabled) {
        doLoad(frmModel, jxpc);
    }
    jxpc.setLenient(inheritedLeniency);
}

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

/**
 * Redefines the Binding action as working on a JXPathContext Type rather
 * then on generic objects.//  ww w  .jav a 2  s  .com
 * 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);
    applyNSDeclarations(jxpc);
    if (this.commonAtts.saveEnabled) {
        doSave(frmModel, jxpc);
    }
    jxpc.setLenient(inheritedLeniency);
}