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

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

Introduction

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

Prototype

void setValue(Object value);

Source Link

Document

Modifies the value of the object, property or collection element this pointer represents.

Usage

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

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    Widget widget = selectWidget(frmModel, this.multiValueId);
    Object[] values = (Object[]) widget.getValue();

    JXPathContext multiValueContext = jctx.getRelativeContext(jctx.createPath(this.multiValuePath));

    // Delete all that is already present

    // Unfortunately the following statement doesn't work (it doesn't removes all elements from the 
    // list because of a bug in JXPath I wasn't able to locate).
    //multiValueContext.removeAll(this.rowPath);

    // TODO: This is a workaround until the bug in commons-jxpath is found, fixed and released
    Iterator rowPointers = multiValueContext.iteratePointers(this.rowPath);
    int cnt = 0;//w  w  w. j  a  v a2s.  c  o m
    while (rowPointers.hasNext()) {
        cnt++;
        rowPointers.next();
    }
    while (cnt >= 1) {
        String thePath = this.rowPath + "[" + cnt + "]";
        multiValueContext.removePath(thePath);
        cnt--;
    }

    boolean update = false;

    if (values != null) {
        // first update the values
        for (int i = 0; i < values.length; i++) {
            String path = this.rowPath + '[' + (i + 1) + ']';
            Pointer rowPtr = multiValueContext.createPath(path);

            Object value = values[i];
            if (value != null && convertor != null) {
                value = convertor.convertToString(value, convertorLocale, null);
            }

            rowPtr.setValue(value);
        }

        // now perform any other bindings that need to be performed when the value is updated
        this.updateBinding.saveFormToModel(frmModel, multiValueContext);

        update = true;
    }

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString() + " -- on-update == " + update);
    }

}

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

public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException {
    Widget widget = frmModel.getWidget(this.multiValueId);
    Object[] values = (Object[]) widget.getValue();

    JXPathContext multiValueContext = jctx.getRelativeContext(jctx.createPath(this.multiValuePath));
    // Delete all that is already present
    multiValueContext.removeAll(this.rowPath);

    boolean update = false;

    if (values != null) {
        // first update the values
        for (int i = 0; i < values.length; i++) {
            String path = this.rowPath + '[' + (i + 1) + ']';
            Pointer rowPtr = multiValueContext.createPath(path);

            Object value = values[i];
            if (value != null && convertor != null) {
                value = convertor.convertToString(value, convertorLocale, null);
            }/*  w  ww  .  jav  a2 s .  c o m*/

            rowPtr.setValue(value);
        }

        // now perform any other bindings that need to be performed when the value is updated
        this.updateBinding.saveFormToModel(frmModel, multiValueContext);

        update = true;
    }

    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString() + " -- on-update == " + update);
    }

}

From source file:org.dcm4chee.xds2.registry.ws.XDSPersistenceWrapper.java

/**
 * Correct metadata parameters to conform to XDS specification.
 * Replace non-conformant ids to uuids, update references, check classification scemes, nodes, etc
 *//*from  www  . j  av a 2 s  .c o m*/
void checkAndCorrectSubmitObjectsRequest(SubmitObjectsRequest req) throws XDSException {

    // TODO: DB_RESTRUCT - CODE INSPECTION - is it correct to replace ids with uuids for ALL identifiables, not only ROs?

    JXPathContext requestContext = JXPathContext.newContext(req);

    ////// Pre-process - move detached classifications from registryObjectList into corresponding objects

    Iterator<JAXBElement<? extends IdentifiableType>> objectListIterator = req.getRegistryObjectList()
            .getIdentifiable().iterator();
    while (objectListIterator.hasNext()) {

        JAXBElement<? extends IdentifiableType> elem = objectListIterator.next();

        /// filter Classifications only
        if (!ClassificationType.class.isAssignableFrom(elem.getValue().getClass()))
            continue;

        /// find referenced object and add the classification to the referenced object 
        ClassificationType cl = (ClassificationType) elem.getValue();
        // this xpath return all nodes in the tree with the specified id 
        Iterator referencedObjs = (Iterator) requestContext
                .iteratePointers(String.format("//*[id = '%s']", cl.getClassifiedObject()));
        try {
            Object o = ((Pointer) referencedObjs.next()).getValue();

            if (!RegistryObjectType.class.isAssignableFrom(o.getClass()))
                throw new XDSException(XDSException.XDS_ERR_REGISTRY_METADATA_ERROR,
                        "Classification " + cl.getId() + " classifies object " + cl.getClassifiedObject()
                                + " which is not a Registry Object",
                        null);
            RegistryObjectType registryObj = (RegistryObjectType) o;

            // add this classification to the classification list
            registryObj.getClassification().add(cl);

        } catch (NoSuchElementException e) {
            throw new XDSException(XDSException.XDS_ERR_REGISTRY_METADATA_ERROR,
                    "Classified object " + cl.getClassifiedObject()
                            + " not found in the request (Classification " + cl.getId() + " )",
                    null);
        }
        // there must be a single node with referenced id
        if (referencedObjs.hasNext())
            throw new XDSException(
                    XDSException.XDS_ERR_REGISTRY_METADATA_ERROR, "Classification " + cl.getId()
                            + " references an object " + cl.getClassifiedObject() + " that is not unique",
                    null);

        /// remove the detached classification from the list
        objectListIterator.remove();
    }

    ////// First run - replace non-uuid IDs with UUIDs for all identifiables, included nested ones

    // Use //id xpath to find all id fields of identifiables in the request
    Iterator ids = (Iterator) requestContext.iteratePointers("//id");

    while (ids.hasNext()) {

        Pointer p = (Pointer) ids.next();
        String oldId = (String) p.getValue();
        String newIdUUID = oldId;

        if (oldId == null)
            continue;

        // Replace non-UUID id with a generated UUID
        if (!oldId.startsWith("urn:")) {
            newIdUUID = "urn:uuid:" + UUID.randomUUID().toString();
            p.setValue(newIdUUID);
            log.debug("Replacing id {} with uuid {}", oldId, newIdUUID);
        }

        newUUIDs.put(oldId, newIdUUID);
    }

    ////// Second run - perform check and correction recursively
    for (JAXBElement<? extends IdentifiableType> elem : req.getRegistryObjectList().getIdentifiable()) {

        // filter RegistryObjects only
        if (!RegistryObjectType.class.isAssignableFrom(elem.getValue().getClass()))
            continue;
        RegistryObjectType ro = (RegistryObjectType) elem.getValue();

        checkAndCorrectMetadata(ro);
    }
}

From source file:org.openl.rules.variation.JXPathVariation.java

@Override
public Object[] applyModification(Object[] originalArguments) {
    if (updatedArgumentIndex >= originalArguments.length) {
        throw new VariationRuntimeException(
                "Failed to apply variaion \"" + getVariationID() + "\". Number of argument to modify is ["
                        + updatedArgumentIndex + "] but arguments length is " + originalArguments.length);
    }/*from www  .ja v  a2 s .co  m*/
    JXPathContext context = JXPathContext.newContext(originalArguments[updatedArgumentIndex]);
    Pointer pointer = compiledExpression.createPath(context);
    pointer.setValue(valueToSet);
    return originalArguments;
}

From source file:org.openvpms.component.business.service.archetype.JXPathGenericObjectCreationFactory.java

@Override
public boolean createObject(JXPathContext context, Pointer ptr, Object parent, String name, int index) {
    try {// w w w  .  j  ava  2  s .co m
        NodeDescriptor node = (NodeDescriptor) context.getVariables().getVariable("node");

        if (logger.isDebugEnabled()) {
            logger.debug("root: " + context.getContextBean().toString() + " parent: " + parent.toString()
                    + " name: " + name + " index: " + index + " type: " + node.getType());
        }

        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(node.getType());
        if (clazz == Boolean.class) {
            ptr.setValue(new Boolean(false));
        } else if (clazz == Integer.class) {
            ptr.setValue(new Integer(0));
        } else if (clazz == Long.class) {
            ptr.setValue(new Long(0L));
        } else if (clazz == Double.class) {
            ptr.setValue(new Double(0.0));
        } else if (clazz == Float.class) {
            ptr.setValue(new Float(0.0));
        } else if (clazz == Short.class) {
            ptr.setValue(new Short((short) 0));
        } else if (clazz == Byte.class) {
            ptr.setValue(new Byte((byte) 0));
        } else if (clazz == Money.class) {
            ptr.setValue(new Money("0.0"));
        } else if (clazz == BigDecimal.class) {
            ptr.setValue(BigDecimal.valueOf(0.0));
        } else {
            ptr.setValue(clazz.newInstance());
        }
    } catch (Exception exception) {
        logger.error("root: " + context.getContextBean().toString() + " parent: " + parent.toString()
                + " name: " + name + " index: " + index, exception);
        return false;
    }

    return true;
}