Search in sources :

Example 6 with DTM

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

the class ElemNumber method getCountMatchPattern.

/**
   * Get the count match pattern, or a default value.
   *
   * @param support The XPath runtime state for this.
   * @param contextNode The node that "." expresses.
   *
   * @return the count match pattern, or a default value. 
   *
   * @throws javax.xml.transform.TransformerException
   */
XPath getCountMatchPattern(XPathContext support, int contextNode) throws javax.xml.transform.TransformerException {
    XPath countMatchPattern = m_countMatchPattern;
    DTM dtm = support.getDTM(contextNode);
    if (null == countMatchPattern) {
        switch(dtm.getNodeType(contextNode)) {
            case DTM.ELEMENT_NODE:
                MyPrefixResolver resolver;
                if (dtm.getNamespaceURI(contextNode) == null) {
                    resolver = new MyPrefixResolver(dtm.getNode(contextNode), dtm, contextNode, false);
                } else {
                    resolver = new MyPrefixResolver(dtm.getNode(contextNode), dtm, contextNode, true);
                }
                countMatchPattern = new XPath(dtm.getNodeName(contextNode), this, resolver, XPath.MATCH, support.getErrorListener());
                break;
            case DTM.ATTRIBUTE_NODE:
                // countMatchPattern = m_stylesheet.createMatchPattern("@"+contextNode.getNodeName(), this);
                countMatchPattern = new XPath("@" + dtm.getNodeName(contextNode), this, this, XPath.MATCH, support.getErrorListener());
                break;
            case DTM.CDATA_SECTION_NODE:
            case DTM.TEXT_NODE:
                // countMatchPattern = m_stylesheet.createMatchPattern("text()", this);
                countMatchPattern = new XPath("text()", this, this, XPath.MATCH, support.getErrorListener());
                break;
            case DTM.COMMENT_NODE:
                // countMatchPattern = m_stylesheet.createMatchPattern("comment()", this);
                countMatchPattern = new XPath("comment()", this, this, XPath.MATCH, support.getErrorListener());
                break;
            case DTM.DOCUMENT_NODE:
                // countMatchPattern = m_stylesheet.createMatchPattern("/", this);
                countMatchPattern = new XPath("/", this, this, XPath.MATCH, support.getErrorListener());
                break;
            case DTM.PROCESSING_INSTRUCTION_NODE:
                // countMatchPattern = m_stylesheet.createMatchPattern("pi("+contextNode.getNodeName()+")", this);
                countMatchPattern = new XPath("pi(" + dtm.getNodeName(contextNode) + ")", this, this, XPath.MATCH, support.getErrorListener());
                break;
            default:
                countMatchPattern = null;
        }
    }
    return countMatchPattern;
}
Also used : XPath(org.apache.xpath.XPath) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 7 with DTM

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

the class ElemNumber method getMatchingAncestors.

/**
   * Get the ancestors, up to the root, that match the
   * pattern.
   * 
   * @param xctxt The XPath runtime state for this.
   * @param node Count this node and it's ancestors.
   * @param stopAtFirstFound Flag indicating to stop after the
   * first node is found (difference between level = single
   * or multiple)
   * @return The number of ancestors that match the pattern.
   *
   * @throws javax.xml.transform.TransformerException
   */
NodeVector getMatchingAncestors(XPathContext xctxt, int node, boolean stopAtFirstFound) throws javax.xml.transform.TransformerException {
    NodeSetDTM ancestors = new NodeSetDTM(xctxt.getDTMManager());
    XPath countMatchPattern = getCountMatchPattern(xctxt, node);
    DTM dtm = xctxt.getDTM(node);
    while (DTM.NULL != node) {
        if ((null != m_fromMatchPattern) && (m_fromMatchPattern.getMatchScore(xctxt, node) != XPath.MATCH_SCORE_NONE)) {
            // that we still don't understand.
            if (!stopAtFirstFound)
                break;
        }
        if (null == countMatchPattern)
            System.out.println("Programmers error! countMatchPattern should never be null!");
        if (countMatchPattern.getMatchScore(xctxt, node) != XPath.MATCH_SCORE_NONE) {
            ancestors.addElement(node);
            if (stopAtFirstFound)
                break;
        }
        node = dtm.getParent(node);
    }
    return ancestors;
}
Also used : XPath(org.apache.xpath.XPath) NodeSetDTM(org.apache.xpath.NodeSetDTM) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

Example 8 with DTM

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

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)

Example 9 with DTM

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

the class TreeWalker2Result method startNode.

/**
   * Start traversal of the tree at the given node
   *
   *
   * @param node Starting node for traversal
   *
   * @throws TransformerException
   */
protected void startNode(int node) throws org.xml.sax.SAXException {
    XPathContext xcntxt = m_transformer.getXPathContext();
    try {
        if (DTM.ELEMENT_NODE == m_dtm.getNodeType(node)) {
            xcntxt.pushCurrentNode(node);
            if (m_startNode != node) {
                super.startNode(node);
            } else {
                String elemName = m_dtm.getNodeName(node);
                String localName = m_dtm.getLocalName(node);
                String namespace = m_dtm.getNamespaceURI(node);
                //xcntxt.pushCurrentNode(node);       
                // SAX-like call to allow adding attributes afterwards
                m_handler.startElement(namespace, localName, elemName);
                boolean hasNSDecls = false;
                DTM dtm = m_dtm;
                for (int ns = dtm.getFirstNamespaceNode(node, true); DTM.NULL != ns; ns = dtm.getNextNamespaceNode(node, ns, true)) {
                    SerializerUtils.ensureNamespaceDeclDeclared(m_handler, dtm, ns);
                }
                for (int attr = dtm.getFirstAttribute(node); DTM.NULL != attr; attr = dtm.getNextAttribute(attr)) {
                    SerializerUtils.addAttribute(m_handler, attr);
                }
            }
        } else {
            xcntxt.pushCurrentNode(node);
            super.startNode(node);
            xcntxt.popCurrentNode();
        }
    } catch (javax.xml.transform.TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM)

Example 10 with DTM

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

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)

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