Search in sources :

Example 1 with DTM

use of com.sun.org.apache.xml.internal.dtm.DTM 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)

Aggregations

DTM (com.sun.org.apache.xml.internal.dtm.DTM)1 NodeSetDTM (com.sun.org.apache.xpath.internal.NodeSetDTM)1 XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)1 TransformerException (javax.xml.transform.TransformerException)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1