Search in sources :

Example 26 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class LocPathIterator method executeCharsToContentHandler.

/**
   * Execute an expression in the XPath runtime context, and return the
   * result of the expression.
   *
   *
   * @param xctxt The XPath runtime context.
   * @param handler The target content handler.
   *
   * @return The result of the expression in the form of a <code>XObject</code>.
   *
   * @throws javax.xml.transform.TransformerException if a runtime exception
   *         occurs.
   * @throws org.xml.sax.SAXException
   */
public void executeCharsToContentHandler(XPathContext xctxt, org.xml.sax.ContentHandler handler) throws javax.xml.transform.TransformerException, org.xml.sax.SAXException {
    LocPathIterator clone = (LocPathIterator) m_clones.getInstance();
    int current = xctxt.getCurrentNode();
    clone.setRoot(current, xctxt);
    int node = clone.nextNode();
    DTM dtm = clone.getDTM(node);
    clone.detach();
    if (node != DTM.NULL) {
        dtm.dispatchCharactersEvents(node, handler, false);
    }
}
Also used : DTM(org.apache.xml.dtm.DTM)

Example 27 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class NodeSequence method addNodeInDocOrder.

/**
   * Add the node into a vector of nodes where it should occur in
   * document order.
   * @param node The node to be added.
   * @return insertIndex.
   * @throws RuntimeException thrown if this NodeSetDTM is not of 
   * a mutable type.
   */
protected int addNodeInDocOrder(int node) {
    assertion(hasCache(), "addNodeInDocOrder must be done on a mutable sequence!");
    int insertIndex = -1;
    NodeVector vec = getVector();
    // This needs to do a binary search, but a binary search 
    // is somewhat tough because the sequence test involves 
    // two nodes.
    int size = vec.size(), i;
    for (i = size - 1; i >= 0; i--) {
        int child = vec.elementAt(i);
        if (child == node) {
            // Duplicate, suppress insert
            i = -2;
            break;
        }
        DTM dtm = m_dtmMgr.getDTM(node);
        if (!dtm.isNodeAfter(node, child)) {
            break;
        }
    }
    if (i != -2) {
        insertIndex = i + 1;
        vec.insertElementAt(node, insertIndex);
    }
    // checkDups();
    return insertIndex;
}
Also used : NodeVector(org.apache.xml.utils.NodeVector) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 28 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class NodeSetDTM method addNodeInDocOrder.

// %TBD%
//  /**
//   * Add the node list to this node set in document order.
//   *
//   * @param start index.
//   * @param end index.
//   * @param testIndex index.
//   * @param nodelist The nodelist to add.
//   * @param support The XPath runtime context.
//   *
//   * @return false always.
//   * @throws RuntimeException thrown if this NodeSetDTM is not of 
//   * a mutable type.
//   */
//  private boolean addNodesInDocOrder(int start, int end, int testIndex,
//                                     NodeList nodelist, XPathContext support)
//  {
//
//    if (!m_mutable)
//      throw new RuntimeException("This NodeSetDTM is not mutable!");
//
//    boolean foundit = false;
//    int i;
//    int node = nodelist.item(testIndex);
//
//    for (i = end; i >= start; i--)
//    {
//      int child = elementAt(i);
//
//      if (child == node)
//      {
//        i = -2;  // Duplicate, suppress insert
//
//        break;
//      }
//
//      if (!support.getDOMHelper().isNodeAfter(node, child))
//      {
//        insertElementAt(node, i + 1);
//
//        testIndex--;
//
//        if (testIndex > 0)
//        {
//          boolean foundPrev = addNodesInDocOrder(0, i, testIndex, nodelist,
//                                                 support);
//
//          if (!foundPrev)
//          {
//            addNodesInDocOrder(i, size() - 1, testIndex, nodelist, support);
//          }
//        }
//
//        break;
//      }
//    }
//
//    if (i == -1)
//    {
//      insertElementAt(node, 0);
//    }
//
//    return foundit;
//  }
/**
   * Add the node into a vector of nodes where it should occur in
   * document order.
   * @param node The node to be added.
   * @param test true if we should test for doc order
   * @param support The XPath runtime context.
   * @return insertIndex.
   * @throws RuntimeException thrown if this NodeSetDTM is not of 
   * a mutable type.
   */
public int addNodeInDocOrder(int node, boolean test, XPathContext support) {
    if (!m_mutable)
        //"This NodeSetDTM is not mutable!");
        throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null));
    int insertIndex = -1;
    if (test) {
        // This needs to do a binary search, but a binary search 
        // is somewhat tough because the sequence test involves 
        // two nodes.
        int size = size(), i;
        for (i = size - 1; i >= 0; i--) {
            int child = elementAt(i);
            if (child == node) {
                // Duplicate, suppress insert
                i = -2;
                break;
            }
            DTM dtm = support.getDTM(node);
            if (!dtm.isNodeAfter(node, child)) {
                break;
            }
        }
        if (i != -2) {
            insertIndex = i + 1;
            insertElementAt(node, insertIndex);
        }
    } else {
        insertIndex = this.size();
        boolean foundit = false;
        for (int i = 0; i < insertIndex; i++) {
            if (i == node) {
                foundit = true;
                break;
            }
        }
        if (!foundit)
            addElement(node);
    }
    // checkDups();
    return insertIndex;
}
Also used : DTM(org.apache.xml.dtm.DTM)

Example 29 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class DTMManagerDefault 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.
   */
public synchronized int getDTMHandleFromNode(org.w3c.dom.Node node) {
    if (null == node)
        //"node must be non-null for getDTMHandleFromNode!");
        throw new IllegalArgumentException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NODE_NON_NULL, null));
    if (node instanceof org.apache.xml.dtm.ref.DTMNodeProxy)
        return ((org.apache.xml.dtm.ref.DTMNodeProxy) node).getDTMNodeNumber();
    else {
        // Find the DOM2DTMs wrapped around this Document (if any)
        // and check whether they contain the Node in question.
        //
        // NOTE that since a DOM2DTM may represent a subtree rather
        // than a full document, we have to be prepared to check more
        // than one -- and there is no guarantee that we will find
        // one that contains ancestors or siblings of the node we're
        // seeking.
        //
        // %REVIEW% We could search for the one which contains this
        // node at the deepest level, and thus covers the widest
        // subtree, but that's going to entail additional work
        // checking more DTMs... and getHandleOfNode is not a
        // cheap operation in most implementations.
        //
        // TODO: %REVIEW% If overflow addressing, we may recheck a DTM
        // already examined. Ouch. But with the increased number of DTMs,
        // scanning back to check this is painful. 
        // POSSIBLE SOLUTIONS: 
        //   Generate a list of _unique_ DTM objects?
        //   Have each DTM cache last DOM node search?
        int max = m_dtms.length;
        for (int i = 0; i < max; i++) {
            DTM thisDTM = m_dtms[i];
            if ((null != thisDTM) && thisDTM instanceof DOM2DTM) {
                int handle = ((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;
        Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr) root).getOwnerElement() : root.getParentNode();
        for (; p != null; p = p.getParentNode()) {
            root = p;
        }
        DOM2DTM dtm = (DOM2DTM) getDTM(new javax.xml.transform.dom.DOMSource(root), false, null, true, true);
        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 = ((DOM2DTM) dtm).getHandleOfNode(node);
        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) DOM2DTM(org.apache.xml.dtm.ref.dom2dtm.DOM2DTM) Node(org.w3c.dom.Node) SAX2RTFDTM(org.apache.xml.dtm.ref.sax2dtm.SAX2RTFDTM) DOM2DTM(org.apache.xml.dtm.ref.dom2dtm.DOM2DTM) SAX2DTM(org.apache.xml.dtm.ref.sax2dtm.SAX2DTM) DTM(org.apache.xml.dtm.DTM)

Example 30 with DTM

use of org.apache.xml.dtm.DTM in project j2objc by google.

the class DTMTreeWalker method startNode.

/**
   * Start processing given node
   *
   *
   * @param node Node to process
   *
   * @throws org.xml.sax.SAXException
   */
protected void startNode(int node) throws org.xml.sax.SAXException {
    if (m_contentHandler instanceof NodeConsumer) {
    // %TBD%
    //      ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    }
    switch(m_dtm.getNodeType(node)) {
        case DTM.COMMENT_NODE:
            {
                XMLString data = m_dtm.getStringValue(node);
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    data.dispatchAsComment(lh);
                }
            }
            break;
        case DTM.DOCUMENT_FRAGMENT_NODE:
            // ??;
            break;
        case DTM.DOCUMENT_NODE:
            this.m_contentHandler.startDocument();
            break;
        case DTM.ELEMENT_NODE:
            DTM dtm = m_dtm;
            for (int nsn = dtm.getFirstNamespaceNode(node, true); DTM.NULL != nsn; nsn = dtm.getNextNamespaceNode(node, nsn, true)) {
                // String prefix = dtm.getPrefix(nsn);
                String prefix = dtm.getNodeNameX(nsn);
                this.m_contentHandler.startPrefixMapping(prefix, dtm.getNodeValue(nsn));
            }
            // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
            // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
            String ns = dtm.getNamespaceURI(node);
            if (null == ns)
                ns = "";
            // %OPT% !!
            org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl();
            for (int i = dtm.getFirstAttribute(node); i != DTM.NULL; i = dtm.getNextAttribute(i)) {
                attrs.addAttribute(dtm.getNamespaceURI(i), dtm.getLocalName(i), dtm.getNodeName(i), "CDATA", dtm.getNodeValue(i));
            }
            this.m_contentHandler.startElement(ns, m_dtm.getLocalName(node), m_dtm.getNodeName(node), attrs);
            break;
        case DTM.PROCESSING_INSTRUCTION_NODE:
            {
                String name = m_dtm.getNodeName(node);
                // String data = pi.getData();
                if (name.equals("xslt-next-is-raw")) {
                    nextIsRaw = true;
                } else {
                    this.m_contentHandler.processingInstruction(name, m_dtm.getNodeValue(node));
                }
            }
            break;
        case DTM.CDATA_SECTION_NODE:
            {
                boolean isLexH = (m_contentHandler instanceof LexicalHandler);
                LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;
                if (isLexH) {
                    lh.startCDATA();
                }
                dispatachChars(node);
                {
                    if (isLexH) {
                        lh.endCDATA();
                    }
                }
            }
            break;
        case DTM.TEXT_NODE:
            {
                if (nextIsRaw) {
                    nextIsRaw = false;
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
                    dispatachChars(node);
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
                } else {
                    dispatachChars(node);
                }
            }
            break;
        case DTM.ENTITY_REFERENCE_NODE:
            {
                if (m_contentHandler instanceof LexicalHandler) {
                    ((LexicalHandler) this.m_contentHandler).startEntity(m_dtm.getNodeName(node));
                } else {
                // warning("Can not output entity to a pure SAX ContentHandler");
                }
            }
            break;
        default:
    }
}
Also used : NodeConsumer(org.apache.xml.utils.NodeConsumer) LexicalHandler(org.xml.sax.ext.LexicalHandler) XMLString(org.apache.xml.utils.XMLString) XMLString(org.apache.xml.utils.XMLString) DTM(org.apache.xml.dtm.DTM)

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