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

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

Introduction

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

Prototype

public abstract Pointer createPathAndSetValue(String xpath, Object value);

Source Link

Document

The same as setValue, except it creates intermediate elements of the path by invoking an AbstractFactory , which should first be installed on the context by calling #setFactory .

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.ValueJXPathBinding.java

/**
 * Actively performs the binding from the CForms-form to the ObjectModel
 * wrapped in a jxpath context//  w  w  w .j ava 2 s  .c o m
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = selectWidget(frmModel, this.fieldId);
    Object value = widget.getValue();
    if (value != null && convertor != null) {
        value = convertor.convertToString(value, convertorLocale, null);
    }

    Object oldValue = jxpc.getValue(this.xpath);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
    }

    boolean update = false;

    if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
        // first update the value itself
        if (value != null) {
            jxpc.createPathAndSetValue(this.xpath, value);
        } else {
            jxpc.removePath(this.xpath);
        }

        // now perform any other bindings that need to be performed when the value is updated
        JXPathContext subContext = null;
        try {
            subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
        } catch (JXPathException e) {
            // if the value has been set to null and the underlying model is a bean, then
            // JXPath will not be able to create a relative context
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("(Ignorable) problem binding field " + widget.getRequestParameterName(), e);
            }
        }
        if (subContext != null) {
            this.updateBinding.saveFormToModel(frmModel, subContext);
        }

        update = true;
    }

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

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

/**
 * Actively performs the binding from the Woody-form to the ObjectModel
 * wrapped in a jxpath context//from   ww  w .  j  a  v  a  2s  .  c om
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    Widget widget = frmModel.getWidget(this.fieldId);
    Object value = widget.getValue();
    if (value != null && convertor != null) {
        value = convertor.convertToString(value, convertorLocale, null);
    }

    Object oldValue = jxpc.getValue(this.xpath);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
    }

    boolean update = false;

    if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
        // first update the value itself
        jxpc.createPathAndSetValue(this.xpath, value);

        // now perform any other bindings that need to be performed when the value is updated
        JXPathContext subContext = null;
        try {
            subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
        } catch (JXPathException e) {
            // if the value has been set to null and the underlying model is a bean, then
            // JXPath will not be able to create a relative context
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("(Ignorable) problem binding field " + widget.getFullyQualifiedId(), e);
            }
        }
        if (subContext != null) {
            this.updateBinding.saveFormToModel(frmModel, subContext);
        }

        update = true;
    }

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

From source file:org.dcm4che3.conf.core.util.ConfigNodeUtil.java

public static void replaceNode(Object rootConfigNode, String path, Object replacementConfigNode) {

    JXPathContext jxPathContext = JXPathContext.newContext(rootConfigNode);
    jxPathContext.setFactory(new AbstractFactory() {
        @Override//  w  w  w .j a v a2  s  .c  o  m
        public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name,
                int index) {
            if (parent instanceof Map) {
                ((Map<String, Object>) parent).put(name, new TreeMap<String, Object>());
                return true;
            }
            return false;
        }

        @Override
        public boolean declareVariable(JXPathContext context, String name) {
            return super.declareVariable(context, name);
        }
    });
    jxPathContext.createPathAndSetValue(path, replacementConfigNode);
}

From source file:org.fireflow.engine.modules.script.ScriptEngineHelper.java

public static Map<String, Object> resolveAssignments(RuntimeContext runtimeContext,
        List<Assignment> assignments, Map<String, Object> contextVars) throws ScriptException {

    if (assignments == null || assignments.size() == 0) {
        return null;
    }/*w  w  w .  j av a  2  s.  c  o  m*/

    Map<String, Object> jxpathRoot = new HashMap<String, Object>();
    if (contextVars.get(ScriptContextVariableNames.INPUTS) != null) {
        jxpathRoot.put(ScriptContextVariableNames.INPUTS, contextVars.get(ScriptContextVariableNames.INPUTS));
    } else {
        jxpathRoot.put(ScriptContextVariableNames.INPUTS, new HashMap<String, Object>());
    }

    if (contextVars.get(ScriptContextVariableNames.OUTPUTS) != null) {
        jxpathRoot.put(ScriptContextVariableNames.OUTPUTS, contextVars.get(ScriptContextVariableNames.OUTPUTS));
    } else {
        jxpathRoot.put(ScriptContextVariableNames.OUTPUTS, new HashMap<String, Object>());
    }

    jxpathRoot.put(ScriptContextVariableNames.PROCESS_VARIABLES, new HashMap<String, Object>());
    jxpathRoot.put(ScriptContextVariableNames.ACTIVITY_VARIABLES, new HashMap<String, Object>());
    jxpathRoot.put(ScriptContextVariableNames.SESSION_ATTRIBUTES, new HashMap<String, Object>());

    for (Assignment assignment : assignments) {
        Expression fromExpression = assignment.getFrom();

        Object obj = evaluateExpression(runtimeContext, fromExpression, contextVars);

        if (fromExpression.getDataType() != null && obj != null) {
            try {
                obj = JavaDataTypeConvertor.dataTypeConvert(fromExpression.getDataType(), obj, null);
            } catch (ClassCastException e) {
                throw new ScriptException(e);
            } catch (ClassNotFoundException e) {
                throw new ScriptException(e);
            }
        }

        // TODO To ?JXpath??? 
        Expression toExpression = assignment.getTo();

        QName dataType = toExpression.getDataType();
        if (dataType != null && dataType.getNamespaceURI().equals(NameSpaces.JAVA.getUri()) && obj != null) {// ??
            try {
                obj = JavaDataTypeConvertor.dataTypeConvert(dataType, obj, null);
            } catch (ClassCastException e) {
                throw new ScriptException(e);
            } catch (ClassNotFoundException e) {
                throw new ScriptException(e);
            }
        } else {
            // XSD??
            //TODO (?)datestring 
            if (obj instanceof java.util.Date) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                obj = df.format((java.util.Date) obj);
            }
        }

        JXPathContext jxpathContext = JXPathContext.newContext(jxpathRoot);
        jxpathContext.setFactory(w3cDomFactory);

        Map<String, String> nsMap = toExpression.getNamespaceMap();
        Iterator<String> prefixIterator = nsMap.keySet().iterator();
        while (prefixIterator.hasNext()) {
            String prefix = prefixIterator.next();
            String nsUri = nsMap.get(prefix);
            jxpathContext.registerNamespace(prefix, nsUri);
        }

        //?
        jxpathContext.createPathAndSetValue(toExpression.getBody(), obj);
    }

    return jxpathRoot;
}

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

protected void assertXPathCreatePathAndSetValue(JXPathContext ctx, String xpath, Object value,
        String expectedPath) {//from  w w w  .  j a  va  2  s.  c  o m
    Pointer pointer = ctx.createPathAndSetValue(xpath, value);
    assertEquals("Creating path <" + xpath + ">", expectedPath, pointer.asPath());

    assertEquals("Creating path (pointer value) <" + xpath + ">", value, pointer.getValue());

    assertEquals("Creating path (context value) <" + xpath + ">", value, ctx.getValue(pointer.asPath()));
}