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

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

Introduction

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

Prototype

public void setFactory(AbstractFactory factory) 

Source Link

Document

Install an abstract factory that should be used by the createPath() and createPathAndSetValue() methods.

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);/*from w ww  .  j a v  a  2s.  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.ContextJXPathBinding.java

/**
 * Actively performs the binding from the CocoonForm to the ObjectModel
 * wrapped in a jxpath context./*ww w  .j  av  a  2  s.  c  o m*/
 */
public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
    if (this.factory != null) {
        jxpc.setFactory(this.factory);
    }
    Pointer ptr = jxpc.getPointer(this.xpath);
    if (ptr.getNode() == null) {
        jxpc.createPath(this.xpath);
        // Need to recreate the pointer after creating the path
        ptr = jxpc.getPointer(this.xpath);
    }
    JXPathContext subContext = jxpc.getRelativeContext(ptr);
    super.doSave(frmModel, subContext);
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("done saving " + toString());
    }
}

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

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);//  w ww  . ja v a2s  .c om

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

private JXPathContext makeJXPathContext(Object objModel) {
    JXPathContext jxpc;
    if (!(objModel instanceof JXPathContext)) {
        jxpc = JXPathContext.newContext(objModel);
        jxpc.setLenient(true);//w  ww.  jav  a 2  s  . c o  m
        if (objModel instanceof Node) {
            jxpc.setFactory(new DOMFactory());
        }
    } else {
        jxpc = (JXPathContext) objModel;
    }
    return jxpc;
}

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//from w  ww.  j av  a 2  s  .  co  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

private static Object evaluateXpathExpression(Expression fireflowExpression,
        Map<String, Object> contextObjects) {
    Map<String, String> namespacePrefixUriMap = fireflowExpression.getNamespaceMap();

    JXPathContext jxpathContext = JXPathContext.newContext(contextObjects);
    jxpathContext.setFactory(w3cDomFactory);
    if (namespacePrefixUriMap != null) {
        Iterator<String> prefixIterator = namespacePrefixUriMap.keySet().iterator();
        while (prefixIterator.hasNext()) {
            String prefix = prefixIterator.next();
            String nsUri = namespacePrefixUriMap.get(prefix);
            jxpathContext.registerNamespace(prefix, nsUri);
        }/*from  ww w. j a v a  2 s .  c  o  m*/
    }

    //W3C DOM
    //TODO ?dom4j document?jdom document
    Object obj = null;
    Object _node = jxpathContext.selectSingleNode(fireflowExpression.getBody());
    if (_node instanceof org.w3c.dom.Node) {
        if (_node instanceof org.w3c.dom.Document) {
            obj = _node;
        } else {
            obj = ((org.w3c.dom.Node) _node).getTextContent();
        }
    } else {
        obj = _node;
    }
    return obj;
}

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;
    }//from   w w  w  .  j a  va 2s  .c  om

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

protected JXPathContext createContext(Object obj) {
    JXPathContext context = JXPathContext.newContext(obj);
    context.setFactory(getAbstractFactory());
    context.registerNamespace("product", "productNS");
    context.registerNamespace(Constants.DEFAULT_NS_PREFIX, "http://test/");//default namespace
    return context;
}

From source file:org.mule.module.xml.filters.JXPathFilter.java

/**
 * Initializes the JXPathContext based on any relevant properties set for the
 * filter.//w  w w  . jav  a 2  s .  c o  m
 *
 * @param context the JXPathContext to initialize
 */
protected void initialise(JXPathContext context) {
    if (namespaces != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing JXPathContext with namespaces: " + namespaces);
        }

        for (Map.Entry<String, String> entry : namespaces.entrySet()) {
            context.registerNamespace(entry.getKey(), entry.getValue());
        }
    }

    Map.Entry entry;
    if (contextProperties != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing JXPathContext with properties: " + contextProperties);
        }

        for (Iterator iterator = contextProperties.entrySet().iterator(); iterator.hasNext();) {
            entry = (Map.Entry) iterator.next();
            context.setValue(entry.getKey().toString(), entry.getValue());
        }
    }

    if (factory != null) {
        context.setFactory(factory);
    }

    context.setLenient(lenient);
}

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

/**
 * This method will create a default object using the specified archetype
 * descriptor. Fundamentally, it will set the default value when specified
 * and it will also create an object through a default constructur if a
 * cardinality constraint is specified./* ww w  . ja v a 2  s. co  m*/
 *
 * @param descriptor the archetype descriptor
 * @return IMObject
 * @throws ArchetypeServiceException if it failed to create the object
 */
protected IMObject create(ArchetypeDescriptor descriptor) {
    IMObject result;
    try {
        Class domainClass = Thread.currentThread().getContextClassLoader().loadClass(descriptor.getClassName());
        if (!IMObject.class.isAssignableFrom(domainClass)) {
            throw new ArchetypeServiceException(ArchetypeServiceException.ErrorCode.InvalidDomainClass,
                    descriptor.getClassName());
        }

        result = (IMObject) domainClass.newInstance();
        result.setArchetypeId(descriptor.getType());

        // first create a JXPath context and use it to process the nodes
        // in the archetype
        JXPathContext context = JXPathHelper.newContext(result);
        context.setFactory(new JXPathGenericObjectCreationFactory());
        create(context, descriptor.getNodeDescriptors());
    } catch (Exception exception) {
        // rethrow as a runtime exception
        throw new ArchetypeServiceException(ArchetypeServiceException.ErrorCode.FailedToCreateObject, exception,
                descriptor.getType().getShortName());
    }

    return result;
}