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

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

Introduction

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

Prototype

public abstract void setValue(String xpath, Object value);

Source Link

Document

Modifies the value of the property described by the supplied xpath.

Usage

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);/* w  w w. j  av a 2 s.c o m*/
            context.setFactory(new DOMFactory());
            context.createPathAndSetValue(this.xPath, importedNode);
        } else {
            // 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.forms.binding.SetAttributeJXPathBinding.java

/**
 * Sets the attribute value on the passed JXPathContext
 *//*  w ww.  j a va 2s  .c om*/
public void doSave(Widget frmModel, JXPathContext jxpc) {
    jxpc.setValue("@" + this.name, this.value);
    if (getLogger().isDebugEnabled())
        getLogger().debug("done saving " + toString());
}

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

/**
 * This wraps the value from the form between 2 prefix and suffix-chars 
 * before saving to the model /*  w ww . j av a 2  s. c  o  m*/
 * 
 * Method signature and semantics complies to {@link AbstractCustomBinding#doSave(Widget, JXPathContext)}
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Object formValue = frmModel.getValue();
    jxpc.setValue(getXpath(), "" + this.prefix + formValue + this.suffix);
}

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

/**
 * @see Receiver//ww w  . j  av  a  2s  . co 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());
    }
}

From source file:org.apache.cocoon.taglib.VarTagSupport.java

/**
 * Register the name and object specified.
 *
 * @param name the name of the attribute to set
 * @param value  the object to associate with the name
 */// www  .  j av  a  2 s  . c  o m
protected final void setVariable(String name, Object value) {
    JXPathContext context = getVariableContext();
    if (name.charAt(0) == '$')
        context.setValue(name, value);
    else
        context.getVariables().declareVariable(name, value);
    //getRequest().setAttribute(name, value);
}

From source file:org.apache.cocoon.taglib.VarTagSupport.java

protected final void removeVariable(String name) {
    JXPathContext context = getVariableContext();
    if (name.charAt(0) == '$')
        context.setValue(name, null);
    else/*from w  ww.  ja va 2 s  .  c o m*/
        context.getVariables().declareVariable(name, null);
    //getRequest().removeAttribute(name);
}

From source file:org.firesoa.common.jxpath.JXPathTestCase.java

protected void assertXPathSetValue(JXPathContext ctx, String xpath, Object value, Object expected) {
    ctx.setValue(xpath, value);
    Object actual = ctx.getValue(xpath);
    assertEquals("Modifying <" + xpath + ">", expected, actual);
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Object parseAndSetFieldValue(Object obj, String fieldName, String fieldValue) throws Exception {
    if (obj instanceof Map) {
        ((Map) obj).put(fieldName, parseValue(fieldValue, String.class)); //Map of Strings
    } else if (obj instanceof List) {
        int index = Integer.parseInt(fieldName);
        while (((List) obj).size() <= index)
            ((List) obj).add(null);
        ((List) obj).set(index, parseValue(fieldValue, String.class)); //List of Strings
    } else if (fieldName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        JXPathContext ctx = JXPathContext.newContext(obj);
        ctx.setValue(fieldName.replace('.', '/'), parseValue(fieldValue, String.class));
    } else {//from  w  w w.  ja v  a 2 s  . c o  m
        // Find a Field
        Field field = getField(obj, fieldName);
        if (field != null) {
            Class fieldClass = field.getType();
            Object value = parseValue(fieldValue, fieldClass);
            field.set(obj, value);
            return obj;
        }
        // Find a getter and setter
        Method getter = getGetter(obj, fieldName);
        Method setter = getSetter(obj, getter, fieldName);
        if (setter != null && getter != null) {
            Object value = parseValue(fieldValue, getter.getReturnType());
            setter.invoke(obj, value);
        }
    }
    return obj;
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Object setFieldValue(Object obj, String fieldName, Object fieldValue) throws Exception {
    if (obj instanceof Map) {
        ((Map) obj).put(fieldName, fieldValue); //Map of Strings
    } else if (obj instanceof List) {
        int index = Integer.parseInt(fieldName);
        while (((List) obj).size() <= index)
            ((List) obj).add(null);
        ((List) obj).set(index, fieldValue); //List of Strings
    } else if (fieldName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        JXPathContext ctx = JXPathContext.newContext(obj);
        ctx.setValue(fieldName.replace('.', '/'), fieldValue);
    } else {/*from   ww  w . j  av a 2 s.  c o m*/
        // Find a Field
        Field field = getField(obj, fieldName);
        if (field != null) {
            field.set(obj, fieldValue);
            return obj;
        }
        // Find a getter and setter.
        Method getter = getGetter(obj, fieldName);
        Method setter = getSetter(obj, getter, fieldName);
        if (setter != null && getter != null) {
            setter.invoke(obj, fieldValue);
        }
    }
    return obj;
}

From source file:org.jboss.dashboard.factory.Component.java

protected Object setObjectProperty(Object obj, String propertyName, List propertyValue, JXPathContext ctx)
        throws Exception {
    if (log.isDebugEnabled())
        log.debug("Setting in " + clazz + " property " + propertyName + " with values " + propertyValue);
    if (obj instanceof Map) {
        ((Map) obj).put(propertyName, getValueForProperty(propertyValue, String.class)); //Map of Strings
    } else if (obj instanceof List) {
        try {/*from  w  w  w  . j a v  a2 s  . c  o  m*/
            int index = Integer.parseInt(propertyName);
            while (((List) obj).size() <= index)
                ((List) obj).add(null);
            ((List) obj).set(index, getValueForProperty(propertyValue, String.class)); //List of Strings
        } catch (Exception e) {
            log.error("Error setting position " + propertyName + " in List " + name + ". ", e);
        }
    } else if (propertyName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        ctx = ctx != null ? ctx : JXPathContext.newContext(obj);
        ctx.setValue(propertyName.replace('.', '/'), getValueForProperty(propertyValue, String.class));
    } else {
        //Find a Field
        Field field = getField(obj, propertyName);
        if (field != null) {
            Class fieldClass = field.getType();
            Object value = getValueForProperty(propertyValue, fieldClass);
            field.set(obj, value);
            return obj;
        }

        // Find a getter and setter.
        Method getter = getGetter(obj, propertyName);
        if (getter == null) {
            log.error("Cannot find a getter for property " + propertyName + " in class " + clazz
                    + ". Ignoring property.");
            return obj;
        }
        Method setter = getSetter(obj, getter, propertyName);
        if (setter == null) {
            log.error("Cannot find a setter for property " + propertyName + " in class " + clazz
                    + ". Ignoring property.");
            return obj;
        }
        Object value = getValueForProperty(propertyValue, getter.getReturnType());
        if (log.isDebugEnabled())
            log.debug("Invoking " + setter + " with value=" + value);
        setter.invoke(obj, new Object[] { value });
    }
    return obj;
}