KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > TreeWalker2Result


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TreeWalker2Result.java,v 1.18 2004/02/16 20:41:29 minchau Exp $
18  */

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 /**
28  * Handle a walk of a tree, but screen out attributes for
29  * the result tree.
30  * @xsl.usage internal
31  */

32 public class TreeWalker2Result extends DTMTreeWalker
33 {
34
35   /** The transformer instance */
36   TransformerImpl m_transformer;
37
38   /** The result tree handler */
39   SerializationHandler m_handler;
40
41   /** Node where to start the tree walk */
42   int m_startNode;
43
44   /**
45    * Constructor.
46    *
47    * @param transformer Non-null transformer instance
48    * @param handler The Result tree handler to use
49    */

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   /**
61    * Perform a pre-order traversal non-recursive style.
62    *
63    * @param pos Start node for traversal
64    *
65    * @throws TransformerException
66    */

67   public void traverse(int pos) throws org.xml.sax.SAXException JavaDoc
68   {
69     m_dtm = m_transformer.getXPathContext().getDTM(pos);
70     m_startNode = pos;
71
72     super.traverse(pos);
73   }
74         
75         /**
76    * End processing of given node
77    *
78    *
79    * @param node Node we just finished processing
80    *
81    * @throws org.xml.sax.SAXException
82    */

83   protected void endNode(int node) throws org.xml.sax.SAXException JavaDoc
84   {
85     super.endNode(node);
86     if(DTM.ELEMENT_NODE == m_dtm.getNodeType(node))
87     {
88       m_transformer.getXPathContext().popCurrentNode();
89     }
90   }
91
92   /**
93    * Start traversal of the tree at the given node
94    *
95    *
96    * @param node Starting node for traversal
97    *
98    * @throws TransformerException
99    */

100   protected void startNode(int node) throws org.xml.sax.SAXException JavaDoc
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 JavaDoc elemName = m_dtm.getNodeName(node);
118           String JavaDoc localName = m_dtm.getLocalName(node);
119           String JavaDoc namespace = m_dtm.getNamespaceURI(node);
120                                         
121           //xcntxt.pushCurrentNode(node);
122
// SAX-like call to allow adding attributes afterwards
123
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 JavaDoc te)
149     {
150       throw new org.xml.sax.SAXException JavaDoc(te);
151     }
152   }
153 }
154
Popular Tags