Search in sources :

Example 1 with XNodeSet

use of com.sun.org.apache.xpath.internal.objects.XNodeSet in project jdk8u_jdk by JetBrains.

the class FuncHere method execute.

/**
     * The here function returns a node-set containing the attribute or
     * processing instruction node or the parent element of the text node
     * that directly bears the XPath expression.  This expression results
     * in an error if the containing XPath expression does not appear in the
     * same XML document against which the XPath expression is being evaluated.
     *
     * @param xctxt
     * @return the xobject
     * @throws javax.xml.transform.TransformerException
     */
@Override
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    Node xpathOwnerNode = (Node) xctxt.getOwnerObject();
    if (xpathOwnerNode == null) {
        return null;
    }
    int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode);
    int currentNode = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(currentNode);
    int docContext = dtm.getDocument();
    if (DTM.NULL == docContext) {
        error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
    }
    {
        // check whether currentNode and the node containing the XPath expression
        // are in the same document
        Document currentDoc = XMLUtils.getOwnerDocument(dtm.getNode(currentNode));
        Document xpathOwnerDoc = XMLUtils.getOwnerDocument(xpathOwnerNode);
        if (currentDoc != xpathOwnerDoc) {
            throw new TransformerException(I18n.translate("xpath.funcHere.documentsDiffer"));
        }
    }
    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM nodeSet = nodes.mutableNodeset();
    {
        int hereNode = DTM.NULL;
        switch(dtm.getNodeType(xpathOwnerNodeDTM)) {
            case Node.ATTRIBUTE_NODE:
            case Node.PROCESSING_INSTRUCTION_NODE:
                {
                    // returns a node-set containing the attribute /  processing instruction node
                    hereNode = xpathOwnerNodeDTM;
                    nodeSet.addNode(hereNode);
                    break;
                }
            case Node.TEXT_NODE:
                {
                    // returns a node-set containing the parent element of the
                    // text node that directly bears the XPath expression
                    hereNode = dtm.getParent(xpathOwnerNodeDTM);
                    nodeSet.addNode(hereNode);
                    break;
                }
            default:
                break;
        }
    }
    /** $todo$ Do I have to do this detach() call? */
    nodeSet.detach();
    return nodes;
}
Also used : NodeSetDTM(com.sun.org.apache.xpath.internal.NodeSetDTM) Node(org.w3c.dom.Node) DTM(com.sun.org.apache.xml.internal.dtm.DTM) NodeSetDTM(com.sun.org.apache.xpath.internal.NodeSetDTM) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) XNodeSet(com.sun.org.apache.xpath.internal.objects.XNodeSet)

Example 2 with XNodeSet

use of com.sun.org.apache.xpath.internal.objects.XNodeSet in project freemarker by apache.

the class SunInternalXalanXPathSupport method executeQuery.

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." + ERRMSG_RECOMMEND_JAXEN);
                } 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(com.sun.org.apache.xpath.internal.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(com.sun.org.apache.xpath.internal.objects.XNull) XNumber(com.sun.org.apache.xpath.internal.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(com.sun.org.apache.xpath.internal.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(com.sun.org.apache.xpath.internal.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(com.sun.org.apache.xpath.internal.objects.XString) List(java.util.List) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)2 TransformerException (javax.xml.transform.TransformerException)2 Node (org.w3c.dom.Node)2 DTM (com.sun.org.apache.xml.internal.dtm.DTM)1 NodeSetDTM (com.sun.org.apache.xpath.internal.NodeSetDTM)1 XPath (com.sun.org.apache.xpath.internal.XPath)1 XBoolean (com.sun.org.apache.xpath.internal.objects.XBoolean)1 XNull (com.sun.org.apache.xpath.internal.objects.XNull)1 XNumber (com.sun.org.apache.xpath.internal.objects.XNumber)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1 XString (com.sun.org.apache.xpath.internal.objects.XString)1 SimpleNumber (freemarker.template.SimpleNumber)1 SimpleScalar (freemarker.template.SimpleScalar)1 TemplateModelException (freemarker.template.TemplateModelException)1 List (java.util.List)1 Document (org.w3c.dom.Document)1 NodeIterator (org.w3c.dom.traversal.NodeIterator)1