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.apache.cocoon.portal.transformation.CopletTransformer.java

/**
 * Overridden from superclass./*from ww  w.  ja  va  2s . c  om*/
 */
public void startTransformingElement(String uri, String name, String raw, Attributes attr)
        throws ProcessingException, IOException, SAXException {
    if (name.equals(COPLET_ELEM)) {
        String expression = attr.getValue(SELECT_ATTR);
        if (expression == null) {
            throw new ProcessingException("Attribute " + SELECT_ATTR + " must be spcified.");
        }

        final CopletInstanceData cid = this.getCopletInstanceData();

        final JXPathContext jxpathContext = JXPathContext.newContext(cid);
        final Object object = jxpathContext.getValue(expression);

        if (object != null) {
            XMLUtils.valueOf(contentHandler, object);
        }

    } else if (name.equals(LINK_ELEM)) {

        final LinkService linkService = this.portalService.getComponentManager().getLinkService();
        final String format = attr.getValue("format");
        AttributesImpl newAttrs = new AttributesImpl();
        newAttrs.setAttributes(attr);
        newAttrs.removeAttribute("format");

        if (attr.getValue("href") != null) {
            final CopletInstanceData cid = this.getCopletInstanceData();
            ChangeCopletInstanceAspectDataEvent event = new ChangeCopletInstanceAspectDataEvent(cid, null,
                    null);

            String value = linkService.getLinkURI(event);
            if (value.indexOf('?') == -1) {
                value = value + '?' + attr.getValue("href");
            } else {
                value = value + '&' + attr.getValue("href");
            }
            newAttrs.removeAttribute("href");
            this.output(value, format, newAttrs);
        } else {
            final String path = attr.getValue("path");
            final String value = attr.getValue("value");

            newAttrs.removeAttribute("path");
            newAttrs.removeAttribute("value");

            JXPathEvent event = null;
            if (attr.getValue("layout") != null) {
                newAttrs.removeAttribute("layout");
                final String layoutId = attr.getValue("layout");
                Object layout = this.portalService.getComponentManager().getProfileManager()
                        .getPortalLayout(null, layoutId);
                if (layout != null) {
                    event = new JXPathEvent(layout, path, value);
                }
            } else {
                String copletId = attr.getValue("coplet");
                newAttrs.removeAttribute("coplet");
                final CopletInstanceData cid = this.getCopletInstanceData(copletId);
                if (cid != null) {
                    event = new CopletJXPathEvent(cid, path, value);
                }
            }
            if (this.insideLinks) {
                if (event != null) {
                    this.collectedEvents.add(event);
                }
            } else {
                final String href = linkService.getLinkURI(event);
                this.output(href, format, newAttrs);
            }
        }
    } else if (name.equals(PARAMETER_ELEM)) {
        if (this.insideLinks) {
            String href = attr.getValue("href");
            if (href != null) {
                final int pos = href.indexOf('?');
                if (pos != -1) {
                    href = href.substring(pos + 1);
                }
                this.collectedEvents.add(new LinkService.ParameterDescription(href));
            }
        }
    } else if (name.equals(LINKS_ELEM)) {
        this.insideLinks = true;
        final AttributesImpl newAttrs = new AttributesImpl();
        newAttrs.setAttributes(attr);
        newAttrs.removeAttribute("format");
        this.stack.push(newAttrs);

        String format = attr.getValue("format");
        if (format == null) {
            format = "html-link";
        }
        this.stack.push(format);
    } else if (name.equals(CONTENT_ELEM) && this.insideLinks) {
        this.startSAXRecording();
    } else {
        super.startTransformingElement(uri, name, raw, attr);
    }
}

From source file:org.apache.cocoon.selection.XPathExceptionSelector.java

/**
 * Compute the exception type, given the configuration and the exception stored in the object model.
 *///from   www.ja v a2s .c  om
public Object getSelectorContext(Map objectModel, Parameters parameters) {

    // get exception from super class
    FindResult selectorContext = (FindResult) super.getSelectorContext(objectModel, parameters);

    if (selectorContext != null) {
        String exceptionName = selectorContext.getName();
        Throwable t = selectorContext.getThrowable();

        Map xPathMap = (Map) exception2XPath.get(exceptionName);

        if (xPathMap != null) {
            // create a context for the thrown exception
            JXPathContext context = JXPathContext.newContext(t);

            for (Iterator iterator = xPathMap.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();

                if (((CompiledExpression) entry.getValue()).getValue(context).equals(Boolean.TRUE)) {
                    // set the configured name if the expression is succesfull
                    selectorContext.setName((String) entry.getKey());
                    return selectorContext;
                }
            }
        }
    }

    return selectorContext;
}

From source file:org.apache.cocoon.transformation.JPathTransformer.java

/**
 * Setup this transformer//from   ww w.  j  a  v a 2 s. com
 *
 * @param resolver a {@link SourceResolver} instance
 * @param objectModel the objectModel
 * @param src <code>src</code> parameter
 * @param parameters optional parameters
 * @exception ProcessingException if an error occurs
 * @exception SAXException if an error occurs
 * @exception IOException if an error occurs
 */
public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters)
        throws ProcessingException, SAXException, IOException {

    super.setup(resolver, objectModel, src, parameters);

    // setup the jpath transformer for this thread
    Object bean = FlowHelper.getContextObject(objectModel);
    m_kont = FlowHelper.getWebContinuation(objectModel);
    m_jxpathContext = JXPathContext.newContext(bean);
}

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

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;//from w  w  w . j a va 2 s . c  om
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);
        if (objModel instanceof Node) {
            jxpc.setFactory(new DOMFactory());
        }
    } 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  ww  .j av  a  2  s.  c om*/
    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.apache.cocoon.woody.transformation.WoodyPipelineConfig.java

/**
 * Creates and initializes a WoodyPipelineConfig object based on the passed
 * arguments of the setup() of the specific Pipeline-component.
 * //from   w w w  . j  ava2  s  .com
 * @param objectModel the objectmodel as passed in the setup()
 * @param parameters the parameters as passed in the setup()
 * @return an instance of WoodyPipelineConfig initialized according to the 
 * settings in the sitemap.
 */
public static WoodyPipelineConfig createConfig(Map objectModel, Parameters parameters) {
    // create and set the jxpathContext...
    Object flowContext = FlowHelper.getContextObject(objectModel);
    WebContinuation wk = FlowHelper.getWebContinuation(objectModel);
    JXPathContext jxpc = JXPathContext.newContext(flowContext);
    Variables vars = jxpc.getVariables();
    vars.declareVariable("continuation", wk);
    Request request = ObjectModelHelper.getRequest(objectModel);
    vars.declareVariable("request", request);
    Session session = request.getSession(false);
    vars.declareVariable("session", session);
    vars.declareVariable("parameters", parameters);

    Locale localeParameter = null;
    String localeStr = parameters.getParameter("locale", null);
    if (localeStr != null) {
        localeParameter = I18nUtils.parseLocale(localeStr);
    }

    String attributeName = parameters.getParameter("attribute-name", null);
    String actionExpression = parameters.getParameter("form-action", null);
    String formMethod = parameters.getParameter("form-method", "POST");
    //TODO (20031223 mpo)think about adding form-encoding for the Generator.
    // Note generator will also need some text to go on the submit-button? 
    // Alternative to adding more here is to apply xinclude ?

    return new WoodyPipelineConfig(jxpc, request, localeParameter, attributeName, actionExpression, formMethod);
}

From source file:org.apache.ctakes.jdl.data.loader.XmlLoader.java

/**
 * @param loader/*from   w  ww  . ja v a2s.co  m*/
 *            the loader
 * @param document
 *            the document
 */
public XmlLoader(final XmlLoadType loader, final Document document) {
    context = JXPathContext.newContext(document);
    this.loader = loader;
}

From source file:org.apache.ctakes.jdl.data.loader.XmlLoader.java

/**
 * @param jdlConnection/*from   www  .  j  a  v a 2 s  .  co m*/
 *            the jdlConnection to manage
 */
@Override
public final void dataInsert(final JdlConnection jdlConnection) {
    String sql = getSqlInsert(loader);
    Number ncommit = loader.getCommit();
    int r = 0;
    try {
        Iterator<?> iterator = context.iteratePointers(loader.getXroot());
        while (iterator.hasNext()) {
            r++;
            NodePointer pointer = (NodePointer) iterator.next();
            Node node = (Node) pointer.getNode();
            JXPathContext context = JXPathContext.newContext(DomUtil.nodeToDocument(node));
            try {
                int c = 0;
                PreparedStatement preparedStatement = jdlConnection.getOpenConnection().prepareStatement(sql);
                if (ncommit == null) {
                    jdlConnection.setAutoCommit(true);
                } else {
                    jdlConnection.setAutoCommit(false);
                }
                for (Column column : loader.getColumn()) {
                    c++;
                    Object value = column.getConstant();
                    if (value == null) {
                        if (column.getSeq() != null) {
                            value = r + column.getSeq().intValue();
                        } else if (column.getXpath() != null) {
                            value = this.context.getValue(column.getXpath());
                        } else {
                            value = context.getPointer(column.getXleaf()).getValue();
                        }
                    }
                    preparedStatement.setObject(c, value);
                }
                executeBatch(preparedStatement);
                if (!jdlConnection.isAutoCommit() && (r % ncommit.intValue() == 0)) {
                    jdlConnection.commitConnection();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        if (!jdlConnection.isAutoCommit()) {
            jdlConnection.commitConnection();
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java

/**
 * apply jxpath to object/* www .  ja v  a  2 s . co  m*/
 * 
 * @param jxpath
 * @param child
 * @return child if jxpath null, else apply jxpath
 */
private Object extractFeature(String jxpath, Object child) {
    return jxpath != null ? JXPathContext.newContext(child).getValue(jxpath) : child;
}

From source file:org.apache.maven.lifecycle.MojoExecutionXPathContainer.java

public MojoExecutionXPathContainer(MojoExecution mojoExecution) throws IOException {
    context = JXPathContext.newContext(mojoExecution);
}