Search in sources :

Example 21 with XObject

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

the class JAXPVariableStack method getVariableOrParam.

public XObject getVariableOrParam(XPathContext xctxt, QName qname) throws TransformerException, IllegalArgumentException {
    if (qname == null) {
        //JAXP 1.3 spec says that if variable name is null then 
        // we need to through IllegalArgumentException
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Variable qname" });
        throw new IllegalArgumentException(fmsg);
    }
    javax.xml.namespace.QName name = new javax.xml.namespace.QName(qname.getNamespace(), qname.getLocalPart());
    Object varValue = resolver.resolveVariable(name);
    if (varValue == null) {
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL, new Object[] { name.toString() });
        throw new TransformerException(fmsg);
    }
    return XObject.create(varValue, xctxt);
}
Also used : QName(org.apache.xml.utils.QName) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 22 with XObject

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

the class FuncId 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 {
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocument();
    if (DTM.NULL == docContext)
        error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
    XObject arg = m_arg0.execute(xctxt);
    int argType = arg.getType();
    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM nodeSet = nodes.mutableNodeset();
    if (XObject.CLASS_NODESET == argType) {
        DTMIterator ni = arg.iter();
        StringVector usedrefs = null;
        int pos = ni.nextNode();
        while (DTM.NULL != pos) {
            DTM ndtm = ni.getDTM(pos);
            String refval = ndtm.getStringValue(pos).toString();
            pos = ni.nextNode();
            usedrefs = getNodesByID(xctxt, docContext, refval, usedrefs, nodeSet, DTM.NULL != pos);
        }
    // ni.detach();
    } else if (XObject.CLASS_NULL == argType) {
        return nodes;
    } else {
        String refval = arg.str();
        getNodesByID(xctxt, docContext, refval, null, nodeSet, false);
    }
    return nodes;
}
Also used : NodeSetDTM(org.apache.xpath.NodeSetDTM) StringVector(org.apache.xml.utils.StringVector) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 23 with XObject

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

the class FuncNormalizeSpace method executeCharsToContentHandler.

/**
   * Execute an expression in the XPath runtime context, and return the 
   * result of the expression.
   *
   *
   * @param xctxt The XPath runtime context.
   *
   * @return The result of the expression in the form of a <code>XObject</code>.
   *
   * @throws javax.xml.transform.TransformerException if a runtime exception 
   *         occurs.
   */
public void executeCharsToContentHandler(XPathContext xctxt, ContentHandler handler) throws javax.xml.transform.TransformerException, org.xml.sax.SAXException {
    if (Arg0IsNodesetExpr()) {
        int node = getArg0AsNode(xctxt);
        if (DTM.NULL != node) {
            DTM dtm = xctxt.getDTM(node);
            dtm.dispatchCharactersEvents(node, handler, true);
        }
    } else {
        XObject obj = execute(xctxt);
        obj.dispatchCharactersEvents(handler);
    }
}
Also used : DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 24 with XObject

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

the class MatchPatternIterator method acceptNode.

/**
   *  Test whether a specified node is visible in the logical view of a
   * TreeWalker or NodeIterator. This function will be called by the
   * implementation of TreeWalker and NodeIterator; it is not intended to
   * be called directly from user code.
   * @param n  The node to check to see if it passes the filter or not.
   * @return  a constant to determine whether the node is accepted,
   *   rejected, or skipped, as defined  above .
   */
public short acceptNode(int n, XPathContext xctxt) {
    try {
        xctxt.pushCurrentNode(n);
        xctxt.pushIteratorRoot(m_context);
        if (DEBUG) {
            System.out.println("traverser: " + m_traverser);
            System.out.print("node: " + n);
            System.out.println(", " + m_cdtm.getNodeName(n));
            // if(m_cdtm.getNodeName(n).equals("near-east"))
            System.out.println("pattern: " + m_pattern.toString());
            m_pattern.debugWhatToShow(m_pattern.getWhatToShow());
        }
        XObject score = m_pattern.execute(xctxt);
        if (DEBUG) {
            // System.out.println("analysis: "+Integer.toBinaryString(m_analysis));
            System.out.println("score: " + score);
            System.out.println("skip: " + (score == NodeTest.SCORE_NONE));
        }
        // System.out.println("\n::acceptNode - score: "+score.num()+"::");
        return (score == NodeTest.SCORE_NONE) ? DTMIterator.FILTER_SKIP : DTMIterator.FILTER_ACCEPT;
    } catch (javax.xml.transform.TransformerException se) {
        // TODO: Fix this.
        throw new RuntimeException(se.getMessage());
    } finally {
        xctxt.popCurrentNode();
        xctxt.popIteratorRoot();
    }
}
Also used : XObject(org.apache.xpath.objects.XObject)

Example 25 with XObject

use of org.apache.xpath.objects.XObject 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)

Aggregations

XObject (org.apache.xpath.objects.XObject)107 TransformerException (javax.xml.transform.TransformerException)27 DTM (org.apache.xml.dtm.DTM)24 XPathContext (org.apache.xpath.XPathContext)24 XNodeSet (org.apache.xpath.objects.XNodeSet)15 DTMIterator (org.apache.xml.dtm.DTMIterator)12 VariableStack (org.apache.xpath.VariableStack)11 Vector (java.util.Vector)9 QName (org.apache.xml.utils.QName)9 Node (org.w3c.dom.Node)9 Expression (org.apache.xpath.Expression)8 XString (org.apache.xpath.objects.XString)7 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 XMLString (org.apache.xml.utils.XMLString)6 org.apache.xpath (org.apache.xpath)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 NodeVector (org.apache.xml.utils.NodeVector)5 SAXException (org.xml.sax.SAXException)5 Hashtable (java.util.Hashtable)4