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

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;//w  ww  .j a  va  2  s.c o  m
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);

        AbstractFactory jxPathFactory;
        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.convertor.BeanConvertor.java

/**
 * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object,
 *      java.util.Locale,//  w w w  .  j a  v  a 2 s. co  m
 *      org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache)
 */
public String convertToString(final Object value, final Locale locale, final FormatCache formatCache) {
    String idValue = "";

    if (null != value) {
        if (m_idPath != null) {
            final JXPathContext ctx = JXPathContext.newContext(value);
            idValue = ctx.getValue(m_idPath).toString();
        } else {
            idValue = value.toString();
        }
    }

    m_objects.put(idValue, value);

    return idValue;
}

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

public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
    JXPathContext ctx = null;//from   w  w  w.ja  v  a2 s. 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(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);/*from   w ww  .j  a  v  a  2 s  .  c  om*/
    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);//w ww .j  a  v a2  s . c  om
    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);/*from w w w . j  a v  a2s. c  o  m*/
    Pointer pointer = ctx.getPointer(xpath);
    Assert.assertNull(message, pointer.getNode());
}

From source file:org.apache.cocoon.forms.transformation.FormsPipelineConfig.java

/**
 * Creates and initializes a FormsPipelineConfig object based on the passed
 * arguments of the setup() of the specific Pipeline-component.
 *
 * @param objectModel the objectmodel as passed in the setup()
 * @param parameters the parameters as passed in the setup()
 * @return an instance of FormsPipelineConfig initialized according to the
 * settings in the sitemap./*from   ww w .  j  ava 2 s .c o m*/
 */
public static FormsPipelineConfig 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);
    // We manually create a cocoon object here to provide the same way
    // of accessing things as in the jxtg
    // as soon as we have our unified om, we should use that
    Request request = ObjectModelHelper.getRequest(objectModel);
    Session session = request.getSession(false);
    final Map cocoonOM = new HashMap();
    cocoonOM.put("continuation", wk);
    cocoonOM.put("request", request);
    if (session != null) {
        cocoonOM.put("session", session);
    }
    cocoonOM.put("parameters", parameters);

    FormsVariables vars = new FormsVariables();
    vars.declareVariable("cocoon", cocoonOM);
    // These four are deprecated!
    vars.declareVariable("continuation", wk);
    vars.declareVariable("request", request);
    vars.declareVariable("session", session);
    vars.declareVariable("parameters", parameters);
    vars.addDeprecatedVariable("continuation");
    vars.addDeprecatedVariable("request");
    vars.addDeprecatedVariable("session");
    vars.addDeprecatedVariable("parameters");
    jxpc.setVariables(vars);

    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", null);
    //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 FormsPipelineConfig(jxpc, request, localeParameter, attributeName, actionExpression, formMethod);
}

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

public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException {
    PortalService portalService = null;/* w  ww  .  j  av  a 2 s . c om*/
    try {

        portalService = (PortalService) this.manager.lookup(PortalService.ROLE);

        // determine coplet id
        String copletId = null;
        Map context = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
        if (context != null) {
            copletId = (String) context.get(Constants.COPLET_ID_KEY);
        } else {
            copletId = (String) objectModel.get(Constants.COPLET_ID_KEY);
        }

        if (copletId == null) {
            return null;
        }

        // return the coplet id
        if (name.equals("#")) {
            return copletId;
        }
        JXPathContext jxpathContext = JXPathContext.newContext(
                portalService.getComponentManager().getProfileManager().getCopletInstanceData(copletId));
        return jxpathContext.getValue(name);
    } catch (ServiceException e) {
        throw new ConfigurationException("Unable to lookup portal service.", e);
    } finally {
        this.manager.release(portalService);
    }
}

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

public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException {
    PortalService portalService = null;/*from  w  ww. j  av a2  s . co m*/
    try {

        portalService = (PortalService) this.manager.lookup(PortalService.ROLE);

        int pos = name.indexOf('/');
        String path;
        if (pos == -1) {
            path = null;
        } else {
            path = name.substring(pos + 1);
            name = name.substring(0, pos);
        }
        // is the layout key specified?
        pos = name.indexOf(':');
        String layoutKey = null;
        String layoutId = name;
        if (pos != -1) {
            layoutKey = name.substring(0, pos);
            layoutId = name.substring(pos + 1);
        }

        // get the layout
        final Object layout = portalService.getComponentManager().getProfileManager().getPortalLayout(layoutKey,
                layoutId);
        Object value = layout;
        if (layout != null && path != null) {
            final JXPathContext jxpathContext = JXPathContext.newContext(layout);
            value = jxpathContext.getValue(path);
        }
        return value;
    } catch (ServiceException e) {
        throw new ConfigurationException("Unable to lookup portal service.", e);
    } finally {
        this.manager.release(portalService);
    }
}

From source file:org.apache.cocoon.portal.event.subscriber.impl.DefaultJXPathEventSubscriber.java

/**
 * @see Receiver/*from   ww  w.  ja v  a2 s  .  c  o  m*/
 */
public void inform(JXPathEvent event, PortalService service) {
    final Object target = event.getTarget();
    if (target != null) {
        final JXPathContext jxpathContext = JXPathContext.newContext(target);
        jxpathContext.setValue(event.getPath(), event.getValue());
    }
}