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

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

Introduction

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

Prototype

public abstract Object getValue(String xpath);

Source Link

Document

Evaluates the xpath and returns the resulting object.

Usage

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 ww.  java2 s .  c om
 *      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;// w  w  w.ja v  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(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);//w w w.java2s  .co m
    Assert.assertEquals(message, expected, ctx.getValue(xpath));
}

From source file:org.apache.cocoon.forms.samples.bindings.CustomValueWrapBinding.java

/**
 * This unwraps the value from the model by removing the 2 prefix and suffix-chars 
 * before setting it onto the model//  w w w . ja  va2  s  . co m
 * 
 * Method signature and semantics complies to {@link AbstractCustomBinding#doLoad(Widget, JXPathContext)}
 */
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    String appValue = (String) jxpc.getValue(getXpath());
    String formValue = null;
    if (appValue.startsWith(this.prefix) && appValue.endsWith(suffix)
            && appValue.length() >= this.prefix.length() + this.suffix.length()) {
        formValue = appValue.substring(this.prefix.length(), appValue.length() - this.suffix.length());
    }
    frmModel.setValue(formValue);
}

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;//from   w  w w  .  jav a2  s  .com
    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  ww w .  j  a va  2  s. c  o  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.transformation.CopletTransformer.java

/**
 * Overridden from superclass./* www  . ja v  a  2  s . c  o m*/
 */
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.taglib.VarTagSupport.java

protected final Object getVariable(String name) {
    JXPathContext context = getVariableContext();
    if (name.charAt(0) == '$') {
        return context.getValue(name);
    }// www.  java2 s. c o m
    return context.getVariables().getVariable(name);
    //getRequest().getAttribute(name);
}

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

/**
 * /*from  w w w .  ja  v a  2s .  com*/
 * @param rowContext
 * @return
 */
private List getMatchIds(JXPathContext rowContext) {
    List matchIds = new ArrayList();
    Iterator iter = this.uniqueRowBinding.iterator();
    while (iter.hasNext()) {
        UniqueFieldJXPathBinding key = (UniqueFieldJXPathBinding) iter.next();
        Object matchId = rowContext.getValue(key.getXpath());
        if (matchId != null && key.getConvertor() != null) {
            if (matchId instanceof String) {
                matchId = key.getConvertor().convertFromString((String) matchId, key.getConvertorLocale(),
                        null);
            } else {
                if (getLogger().isWarnEnabled()) {
                    getLogger().warn("Convertor ignored on backend-value " + "which isn't of type String.");
                }
            }
        }
        matchIds.add(matchId);
    }
    return matchIds;
}

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

/**
 * Actively performs the binding from the ObjectModel wrapped in a jxpath
 * context to the Woody-form-widget specified in this object.
 *//*  www  . j  a v a 2  s.c o m*/
public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = frmModel.getWidget(this.fieldId);
    if (widget == null) {
        throw new BindingException("The widget with the ID [" + this.fieldId
                + "] referenced in the binding does not exist in the form definition.");
    }

    Object value = jxpc.getValue(this.xpath);
    if (value != null && convertor != null) {
        if (value instanceof String) {
            value = convertor.convertFromString((String) value, convertorLocale, null);
        } else {
            getLogger().warn("Convertor ignored on backend-value which isn't of type String.");
        }
    }

    widget.setValue(value);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Done loading " + toString() + " -- value= " + value);
    }
}