Search in sources :

Example 1 with XNull

use of org.apache.xpath.objects.XNull in project freemarker by apache.

the class XalanXPathSupport method executeQuery.

/* " + ERRMSG_RECOMMEND_JAXEN;*/
public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
    if (!(context instanceof Node)) {
        if (context != null) {
            if (isNodeList(context)) {
                int cnt = ((List) context).size();
                if (cnt != 0) {
                    throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node.");
                } else {
                    throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
                }
            } else {
                throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
            }
        } else {
            throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
        }
    }
    Node node = (Node) context;
    try {
        XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
        int ctxtNode = xpathContext.getDTMHandleFromNode(node);
        XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
        if (xresult instanceof XNodeSet) {
            NodeListModel result = new NodeListModel(node);
            result.xpathSupport = this;
            NodeIterator nodeIterator = xresult.nodeset();
            Node n;
            do {
                n = nodeIterator.nextNode();
                if (n != null) {
                    result.add(n);
                }
            } while (n != null);
            return result.size() == 1 ? result.get(0) : result;
        }
        if (xresult instanceof XBoolean) {
            return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
        }
        if (xresult instanceof XNull) {
            return null;
        }
        if (xresult instanceof XString) {
            return new SimpleScalar(xresult.toString());
        }
        if (xresult instanceof XNumber) {
            return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
        }
        throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
    } catch (TransformerException te) {
        throw new TemplateModelException(te);
    }
}
Also used : XPath(org.apache.xpath.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(org.apache.xpath.objects.XNull) XNumber(org.apache.xpath.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(org.apache.xpath.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(org.apache.xpath.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(org.apache.xpath.objects.XString) List(java.util.List) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 2 with XNull

use of org.apache.xpath.objects.XNull in project j2objc by google.

the class FuncExtFunction method execute.

/**
 * Execute the function.  The function must return
 * a valid object.
 * @param xctxt The current execution context.
 * @return A valid XObject.
 *
 * @throws javax.xml.transform.TransformerException
 */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    if (xctxt.isSecureProcessing())
        throw new javax.xml.transform.TransformerException(XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { toString() }));
    XObject result;
    Vector argVec = new Vector();
    int nArgs = m_argVec.size();
    for (int i = 0; i < nArgs; i++) {
        Expression arg = (Expression) m_argVec.elementAt(i);
        XObject xobj = arg.execute(xctxt);
        /*
       * Should cache the arguments for func:function
       */
        xobj.allowDetachToRelease(false);
        argVec.addElement(xobj);
    }
    // dml
    ExtensionsProvider extProvider = (ExtensionsProvider) xctxt.getOwnerObject();
    Object val = extProvider.extFunction(this, argVec);
    if (null != val) {
        result = XObject.create(val, xctxt);
    } else {
        result = new XNull();
    }
    return result;
}
Also used : ExtensionsProvider(org.apache.xpath.ExtensionsProvider) XNull(org.apache.xpath.objects.XNull) Expression(org.apache.xpath.Expression) XObject(org.apache.xpath.objects.XObject) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject)

Example 3 with XNull

use of org.apache.xpath.objects.XNull in project robovm by robovm.

the class FuncExtFunction method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    if (xctxt.isSecureProcessing())
        throw new javax.xml.transform.TransformerException(XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { toString() }));
    XObject result;
    Vector argVec = new Vector();
    int nArgs = m_argVec.size();
    for (int i = 0; i < nArgs; i++) {
        Expression arg = (Expression) m_argVec.elementAt(i);
        XObject xobj = arg.execute(xctxt);
        /*
       * Should cache the arguments for func:function
       */
        xobj.allowDetachToRelease(false);
        argVec.addElement(xobj);
    }
    //dml
    ExtensionsProvider extProvider = (ExtensionsProvider) xctxt.getOwnerObject();
    Object val = extProvider.extFunction(this, argVec);
    if (null != val) {
        result = XObject.create(val, xctxt);
    } else {
        result = new XNull();
    }
    return result;
}
Also used : ExtensionsProvider(org.apache.xpath.ExtensionsProvider) XNull(org.apache.xpath.objects.XNull) Expression(org.apache.xpath.Expression) XObject(org.apache.xpath.objects.XObject) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject)

Aggregations

XNull (org.apache.xpath.objects.XNull)3 XObject (org.apache.xpath.objects.XObject)3 Vector (java.util.Vector)2 Expression (org.apache.xpath.Expression)2 ExtensionsProvider (org.apache.xpath.ExtensionsProvider)2 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateModelException (freemarker.template.TemplateModelException)1 List (java.util.List)1 TransformerException (javax.xml.transform.TransformerException)1 XPath (org.apache.xpath.XPath)1 XBoolean (org.apache.xpath.objects.XBoolean)1 XNodeSet (org.apache.xpath.objects.XNodeSet)1 XNumber (org.apache.xpath.objects.XNumber)1 XString (org.apache.xpath.objects.XString)1 Node (org.w3c.dom.Node)1 NodeIterator (org.w3c.dom.traversal.NodeIterator)1