Search in sources :

Example 96 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class ContextMatchStepPattern method executeRelativePathPattern.

/**
   * Execute the match pattern step relative to another step.
   *
   *
   * @param xctxt The XPath runtime context.
   * NEEDSDOC @param prevStep
   *
   * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject executeRelativePathPattern(XPathContext xctxt, StepPattern prevStep) throws javax.xml.transform.TransformerException {
    XObject score = NodeTest.SCORE_NONE;
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    if (null != dtm) {
        int predContext = xctxt.getCurrentNode();
        DTMAxisTraverser traverser;
        int axis = m_axis;
        boolean needToTraverseAttrs = WalkerFactory.isDownwardAxisOfMany(axis);
        boolean iterRootIsAttr = (dtm.getNodeType(xctxt.getIteratorRoot()) == DTM.ATTRIBUTE_NODE);
        if ((Axis.PRECEDING == axis) && iterRootIsAttr) {
            axis = Axis.PRECEDINGANDANCESTOR;
        }
        traverser = dtm.getAxisTraverser(axis);
        for (int relative = traverser.first(context); DTM.NULL != relative; relative = traverser.next(context, relative)) {
            try {
                xctxt.pushCurrentNode(relative);
                score = execute(xctxt);
                if (score != NodeTest.SCORE_NONE) {
                    //       predContext, relative);
                    if (executePredicates(xctxt, dtm, context))
                        return score;
                    score = NodeTest.SCORE_NONE;
                }
                if (needToTraverseAttrs && iterRootIsAttr && (DTM.ELEMENT_NODE == dtm.getNodeType(relative))) {
                    int xaxis = Axis.ATTRIBUTE;
                    for (int i = 0; i < 2; i++) {
                        DTMAxisTraverser atraverser = dtm.getAxisTraverser(xaxis);
                        for (int arelative = atraverser.first(relative); DTM.NULL != arelative; arelative = atraverser.next(relative, arelative)) {
                            try {
                                xctxt.pushCurrentNode(arelative);
                                score = execute(xctxt);
                                if (score != NodeTest.SCORE_NONE) {
                                    if (score != NodeTest.SCORE_NONE)
                                        return score;
                                }
                            } finally {
                                xctxt.popCurrentNode();
                            }
                        }
                        xaxis = Axis.NAMESPACE;
                    }
                }
            } finally {
                xctxt.popCurrentNode();
            }
        }
    }
    return score;
}
Also used : DTMAxisTraverser(org.apache.xml.dtm.DTMAxisTraverser) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 97 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class FuncQname 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 = getArg0AsNode(xctxt);
    XObject val;
    if (DTM.NULL != context) {
        DTM dtm = xctxt.getDTM(context);
        String qname = dtm.getNodeNameX(context);
        val = (null == qname) ? XString.EMPTYSTRING : new XString(qname);
    } else {
        val = XString.EMPTYSTRING;
    }
    return val;
}
Also used : XString(org.apache.xpath.objects.XString) XString(org.apache.xpath.objects.XString) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 98 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class FuncUnparsedEntityURI 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 {
    String name = m_arg0.execute(xctxt).str();
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int doc = dtm.getDocument();
    String uri = dtm.getUnparsedEntityURI(name);
    return new XString(uri);
}
Also used : XString(org.apache.xpath.objects.XString) XString(org.apache.xpath.objects.XString) DTM(org.apache.xml.dtm.DTM)

Example 99 with DTM

use of org.apache.xml.dtm.DTM in project nokogiri by sparklemotion.

the class XalanDTMManagerPatch method getDTMHandleFromNode.

/**
     * Given a W3C DOM node, try and return a DTM handle.
     * Note: calling this may be non-optimal, and there is no guarantee that
     * the node will be found in any particular DTM.
     *
     * @param node Non-null reference to a DOM node.
     *
     * @return a valid DTM handle.
     */
@Override
public /* synchronized */
int getDTMHandleFromNode(org.w3c.dom.Node node) {
    //    throw new IllegalArgumentException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NODE_NON_NULL, null));
    assert node != null;
    if (node instanceof org.apache.xml.dtm.ref.DTMNodeProxy) {
        return ((org.apache.xml.dtm.ref.DTMNodeProxy) node).getDTMNodeNumber();
    }
    //   Have each DTM cache last DOM node search?
    for (int i = 0; i < m_dtms.length; i++) {
        DTM thisDTM = m_dtms[i];
        if (thisDTM instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTM) {
            int handle = ((org.apache.xml.dtm.ref.dom2dtm.DOM2DTM) thisDTM).getHandleOfNode(node);
            if (handle != DTM.NULL) {
                return handle;
            }
        }
    }
    // Not found; generate a new DTM.
    //
    // %REVIEW% Is this really desirable, or should we return null
    // and make folks explicitly instantiate from a DOMSource? The
    // latter is more work but gives the caller the opportunity to
    // explicitly add the DTM to a DTMManager... and thus to know when
    // it can be discarded again, which is something we need to pay much
    // more attention to. (Especially since only DTMs which are assigned
    // to a manager can use the overflow addressing scheme.)
    //
    // %BUG% If the source node was a DOM2DTM$defaultNamespaceDeclarationNode
    // and the DTM wasn't registered with this DTMManager, we will create
    // a new DTM and _still_ not be able to find the node (since it will
    // be resynthesized). Another reason to push hard on making all DTMs
    // be managed DTMs.
    // Since the real root of our tree may be a DocumentFragment, we need to
    // use getParent to find the root, instead of getOwnerDocument.  Otherwise
    // DOM2DTM#getHandleOfNode will be very unhappy.
    Node root = node;
    int rootType = root.getNodeType();
    Node p = (rootType == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr) root).getOwnerElement() : root.getParentNode();
    for (; p != null; p = p.getParentNode()) root = p;
    // DOM2DTM dtm = (DOM2DTM) getDTM(new DOMSource(root), false, null);
    DOM2DTMExt dtm = getDTMExt(new DOMSource(root), false, null);
    int handle;
    if (node instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTMdefaultNamespaceDeclarationNode) {
        // Can't return the same node since it's unique to a specific DTM,
        // but can return the equivalent node -- find the corresponding
        // Document Element, then ask it for the xml: namespace decl.
        handle = dtm.getHandleOfNode(((org.w3c.dom.Attr) node).getOwnerElement());
        handle = dtm.getAttributeNode(handle, node.getNamespaceURI(), node.getLocalName());
    } else {
        handle = dtm.getHandleOfNode(node);
        rootType = root.getNodeType();
        // we decided to allow wrapping a DTM around a subtree.
        if ((root == node) || (rootType == Node.DOCUMENT_NODE && root == node.getOwnerDocument()) || (rootType != Node.DOCUMENT_NODE && root.getOwnerDocument() == node.getOwnerDocument())) {
            // available!
            for (Node cursor = node; cursor != null; cursor = (cursor.getNodeType() != Node.ATTRIBUTE_NODE) ? cursor.getParentNode() : ((org.w3c.dom.Attr) cursor).getOwnerElement()) {
                if (cursor == root) {
                    // We know this node; find its handle.
                    return (dtm).getHandleFromNode(node);
                }
            }
        // for ancestors of node
        }
    // if node and m_root in same Document
    }
    if (DTM.NULL == handle)
        //"Could not resolve the node to a handle!");
        throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_RESOLVE_NODE, null));
    return handle;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Node(org.w3c.dom.Node) DOM2DTMExt(org.apache.xml.dtm.ref.dom2dtm.DOM2DTMExt) DTM(org.apache.xml.dtm.DTM)

Example 100 with DTM

use of org.apache.xml.dtm.DTM in project intellij-community by JetBrains.

the class XalanStyleFrame method eval.

public Value eval(String expr) throws Debugger.EvaluationException {
    assert isValid();
    try {
        final DTMIterator context = myTransformer.getContextNodeList();
        final int ctx;
        final DTM dtm = context.getDTM(myCurrentNode);
        if (dtm.getDocumentRoot(myCurrentNode) == myCurrentNode) {
            ctx = dtm.getFirstChild(myCurrentNode);
        } else {
            ctx = myCurrentNode;
        }
        final DTMNodeProxy c = new DTMNodeProxy(dtm, ctx);
        final PrefixResolver prefixResolver = new PrefixResolverDefault(c) {

            public String getNamespaceForPrefix(String prefix, Node context) {
                if (context instanceof DTMNodeProxy) {
                    final DTMNodeProxy proxy = (DTMNodeProxy) context;
                    final DTM dtm = proxy.getDTM();
                    int p = proxy.getDTMNodeNumber();
                    while (p != DTM.NULL) {
                        int nsNode = dtm.getFirstNamespaceNode(p, true);
                        while (nsNode != DTM.NULL) {
                            final String s = dtm.getLocalName(nsNode);
                            if (s.equals(prefix)) {
                                return dtm.getNodeValue(nsNode);
                            }
                            nsNode = dtm.getNextNamespaceNode(p, nsNode, true);
                        }
                        p = dtm.getParent(p);
                    }
                }
                return super.getNamespaceForPrefix(prefix, context);
            }
        };
        final XPath xPath = new XPath(expr, myCurrentElement, prefixResolver, XPath.SELECT, myTransformer.getErrorListener());
        return new XObjectValue(xPath.execute(myContext, myCurrentNode, myCurrentElement));
    } catch (Exception e) {
        debug(e);
        final String message = e.getMessage();
        throw new Debugger.EvaluationException(message != null ? message : e.getClass().getSimpleName());
    }
}
Also used : XPath(org.apache.xpath.XPath) Debugger(org.intellij.plugins.xsltDebugger.rt.engine.Debugger) Node(org.w3c.dom.Node) PrefixResolver(org.apache.xml.utils.PrefixResolver) TransformerException(javax.xml.transform.TransformerException) DTMIterator(org.apache.xml.dtm.DTMIterator) DTM(org.apache.xml.dtm.DTM) DTMNodeProxy(org.apache.xml.dtm.ref.DTMNodeProxy) PrefixResolverDefault(org.apache.xml.utils.PrefixResolverDefault)

Aggregations

DTM (org.apache.xml.dtm.DTM)100 XObject (org.apache.xpath.objects.XObject)24 DTMIterator (org.apache.xml.dtm.DTMIterator)23 NodeSetDTM (org.apache.xpath.NodeSetDTM)20 TransformerException (javax.xml.transform.TransformerException)17 XPathContext (org.apache.xpath.XPathContext)16 XMLString (org.apache.xml.utils.XMLString)12 XString (org.apache.xpath.objects.XString)10 SAXException (org.xml.sax.SAXException)10 XNodeSet (org.apache.xpath.objects.XNodeSet)8 XPath (org.apache.xpath.XPath)7 IOException (java.io.IOException)6 Vector (java.util.Vector)6 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)6 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)6 QName (org.apache.xml.utils.QName)6 DOMSource (javax.xml.transform.dom.DOMSource)5 Hashtable (java.util.Hashtable)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4