1 16 19 package org.apache.xalan.transformer; 20 21 import org.apache.xalan.serialize.SerializerUtils; 22 import org.apache.xml.dtm.DTM; 23 import org.apache.xml.dtm.ref.DTMTreeWalker; 24 import org.apache.xml.serializer.SerializationHandler; 25 import org.apache.xpath.XPathContext; 26 27 32 public class TreeWalker2Result extends DTMTreeWalker 33 { 34 35 36 TransformerImpl m_transformer; 37 38 39 SerializationHandler m_handler; 40 41 42 int m_startNode; 43 44 50 public TreeWalker2Result(TransformerImpl transformer, 51 SerializationHandler handler) 52 { 53 54 super(handler, null); 55 56 m_transformer = transformer; 57 m_handler = handler; 58 } 59 60 67 public void traverse(int pos) throws org.xml.sax.SAXException 68 { 69 m_dtm = m_transformer.getXPathContext().getDTM(pos); 70 m_startNode = pos; 71 72 super.traverse(pos); 73 } 74 75 83 protected void endNode(int node) throws org.xml.sax.SAXException 84 { 85 super.endNode(node); 86 if(DTM.ELEMENT_NODE == m_dtm.getNodeType(node)) 87 { 88 m_transformer.getXPathContext().popCurrentNode(); 89 } 90 } 91 92 100 protected void startNode(int node) throws org.xml.sax.SAXException 101 { 102 103 XPathContext xcntxt = m_transformer.getXPathContext(); 104 try 105 { 106 107 if (DTM.ELEMENT_NODE == m_dtm.getNodeType(node)) 108 { 109 xcntxt.pushCurrentNode(node); 110 111 if(m_startNode != node) 112 { 113 super.startNode(node); 114 } 115 else 116 { 117 String elemName = m_dtm.getNodeName(node); 118 String localName = m_dtm.getLocalName(node); 119 String namespace = m_dtm.getNamespaceURI(node); 120 121 m_handler.startElement(namespace, localName, elemName); 124 boolean hasNSDecls = false; 125 DTM dtm = m_dtm; 126 for (int ns = dtm.getFirstNamespaceNode(node, true); 127 DTM.NULL != ns; ns = dtm.getNextNamespaceNode(node, ns, true)) 128 { 129 SerializerUtils.ensureNamespaceDeclDeclared(m_handler,dtm, ns); 130 } 131 132 133 for (int attr = dtm.getFirstAttribute(node); 134 DTM.NULL != attr; attr = dtm.getNextAttribute(attr)) 135 { 136 SerializerUtils.addAttribute(m_handler, attr); 137 } 138 } 139 140 } 141 else 142 { 143 xcntxt.pushCurrentNode(node); 144 super.startNode(node); 145 xcntxt.popCurrentNode(); 146 } 147 } 148 catch(javax.xml.transform.TransformerException te) 149 { 150 throw new org.xml.sax.SAXException (te); 151 } 152 } 153 } 154 | Popular Tags |