KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > trax > DOM2TO


1 /*
2  * Copyright 2001-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: DOM2TO.java,v 1.9 2004/02/20 19:30:40 bhakti Exp $
18  */

19
20 package org.apache.xalan.xsltc.trax;
21
22 import java.io.IOException JavaDoc;
23
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.apache.xml.serializer.SerializationHandler;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.DTDHandler JavaDoc;
29 import org.xml.sax.EntityResolver JavaDoc;
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.SAXNotRecognizedException JavaDoc;
35 import org.xml.sax.SAXNotSupportedException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.apache.xml.serializer.NamespaceMappings;
38
39 /**
40  * @author Santiago Pericas-Geertsen
41  */

42 public class DOM2TO implements XMLReader JavaDoc, Locator JavaDoc {
43
44     private final static String JavaDoc EMPTYSTRING = "";
45     private static final String JavaDoc XMLNS_PREFIX = "xmlns";
46
47     /**
48      * A reference to the DOM to be traversed.
49      */

50     private Node JavaDoc _dom;
51
52     /**
53      * A reference to the output handler receiving the events.
54      */

55     private SerializationHandler _handler;
56
57     public DOM2TO(Node JavaDoc root, SerializationHandler handler) {
58     _dom = root;
59     _handler = handler;
60     }
61
62     public ContentHandler JavaDoc getContentHandler() {
63     return null;
64     }
65
66     public void setContentHandler(ContentHandler JavaDoc handler) {
67     // Empty
68
}
69
70     public void parse(InputSource JavaDoc unused) throws IOException JavaDoc, SAXException JavaDoc {
71         parse(_dom);
72     }
73
74     public void parse() throws IOException JavaDoc, SAXException JavaDoc {
75     if (_dom != null) {
76         boolean isIncomplete =
77         (_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE);
78
79         if (isIncomplete) {
80         _handler.startDocument();
81         parse(_dom);
82         _handler.endDocument();
83         }
84         else {
85         parse(_dom);
86         }
87     }
88     }
89
90     /**
91      * Traverse the DOM and generate TO events for a handler. Notice that
92      * we need to handle implicit namespace declarations too.
93      */

94     private void parse(Node JavaDoc node)
95     throws IOException JavaDoc, SAXException JavaDoc
96     {
97     if (node == null) return;
98
99         switch (node.getNodeType()) {
100     case Node.ATTRIBUTE_NODE: // handled by ELEMENT_NODE
101
case Node.DOCUMENT_TYPE_NODE :
102     case Node.ENTITY_NODE :
103     case Node.ENTITY_REFERENCE_NODE:
104     case Node.NOTATION_NODE :
105         // These node types are ignored!!!
106
break;
107     case Node.CDATA_SECTION_NODE:
108         _handler.startCDATA();
109         _handler.characters(node.getNodeValue());
110         _handler.endCDATA();
111         break;
112
113     case Node.COMMENT_NODE: // should be handled!!!
114
_handler.comment(node.getNodeValue());
115         break;
116
117     case Node.DOCUMENT_NODE:
118         _handler.startDocument();
119         Node JavaDoc next = node.getFirstChild();
120         while (next != null) {
121         parse(next);
122         next = next.getNextSibling();
123         }
124         _handler.endDocument();
125         break;
126
127     case Node.DOCUMENT_FRAGMENT_NODE:
128         next = node.getFirstChild();
129         while (next != null) {
130         parse(next);
131         next = next.getNextSibling();
132         }
133         break;
134
135     case Node.ELEMENT_NODE:
136         // Generate SAX event to start element
137
final String JavaDoc qname = node.getNodeName();
138         _handler.startElement(null, null, qname);
139
140         String JavaDoc prefix;
141         final NamedNodeMap JavaDoc map = node.getAttributes();
142         final int length = map.getLength();
143
144         // Process all other attributes
145
NamespaceMappings nm = new NamespaceMappings();
146         for (int i = 0; i < length; i++) {
147         int colon;
148         final Node JavaDoc attr = map.item(i);
149         final String JavaDoc qnameAttr = attr.getNodeName();
150
151         if (qnameAttr.startsWith(XMLNS_PREFIX)) {
152             final String JavaDoc uriAttr = attr.getNodeValue();
153             colon = qnameAttr.lastIndexOf(':');
154             prefix = (colon > 0) ? qnameAttr.substring(colon + 1)
155             : EMPTYSTRING;
156             _handler.namespaceAfterStartElement(prefix, uriAttr);
157         }
158         else {
159             final String JavaDoc uriAttr = attr.getNamespaceURI();
160             // Uri may be implicitly declared
161
if (uriAttr != null && !uriAttr.equals(EMPTYSTRING) ) {
162             colon = qnameAttr.lastIndexOf(':');
163                            
164                         // Fix for bug 26319
165
// For attributes not given an prefix explictly
166
// but having a namespace uri we need
167
// to explicitly generate the prefix
168
String JavaDoc newPrefix = nm.lookupPrefix(uriAttr);
169                         if (newPrefix == null)
170                             newPrefix = nm.generateNextPrefix();
171             prefix = (colon > 0) ? qnameAttr.substring(0, colon)
172                 : newPrefix;
173             _handler.namespaceAfterStartElement(prefix, uriAttr);
174                 _handler.addAttribute((prefix + ":" + qnameAttr)
175                             , attr.getNodeValue());
176             } else {
177                          _handler.addAttribute(qnameAttr, attr.getNodeValue());
178                     }
179                 }
180         }
181
182         // Now element namespace and children
183
final String JavaDoc uri = node.getNamespaceURI();
184             final String JavaDoc localName = node.getLocalName();
185
186         // Uri may be implicitly declared
187
if (uri != null) {
188         final int colon = qname.lastIndexOf(':');
189         prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
190         _handler.namespaceAfterStartElement(prefix, uri);
191         }else {
192                   // Fix for bug 26319
193
// If an element foo is created using
194
// createElementNS(null,locName)
195
// then the element should be serialized
196
// <foo xmlns=" "/>
197
if (uri == null && localName != null) {
198              prefix = EMPTYSTRING;
199              _handler.namespaceAfterStartElement(prefix, EMPTYSTRING);
200                  }
201             }
202
203         // Traverse all child nodes of the element (if any)
204
next = node.getFirstChild();
205         while (next != null) {
206         parse(next);
207         next = next.getNextSibling();
208         }
209
210         // Generate SAX event to close element
211
_handler.endElement(qname);
212         break;
213
214     case Node.PROCESSING_INSTRUCTION_NODE:
215         _handler.processingInstruction(node.getNodeName(),
216                        node.getNodeValue());
217         break;
218
219     case Node.TEXT_NODE:
220         _handler.characters(node.getNodeValue());
221         break;
222     }
223     }
224
225     /**
226      * This class is only used internally so this method should never
227      * be called.
228      */

229     public DTDHandler JavaDoc getDTDHandler() {
230     return null;
231     }
232
233     /**
234      * This class is only used internally so this method should never
235      * be called.
236      */

237     public ErrorHandler JavaDoc getErrorHandler() {
238     return null;
239     }
240
241     /**
242      * This class is only used internally so this method should never
243      * be called.
244      */

245     public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
246     SAXNotSupportedException JavaDoc
247     {
248     return false;
249     }
250
251     /**
252      * This class is only used internally so this method should never
253      * be called.
254      */

255     public void setFeature(String JavaDoc name, boolean value) throws
256     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
257     {
258     }
259
260     /**
261      * This class is only used internally so this method should never
262      * be called.
263      */

264     public void parse(String JavaDoc sysId) throws IOException JavaDoc, SAXException JavaDoc {
265     throw new IOException JavaDoc("This method is not yet implemented.");
266     }
267
268     /**
269      * This class is only used internally so this method should never
270      * be called.
271      */

272     public void setDTDHandler(DTDHandler JavaDoc handler) throws NullPointerException JavaDoc {
273     }
274
275     /**
276      * This class is only used internally so this method should never
277      * be called.
278      */

279     public void setEntityResolver(EntityResolver JavaDoc resolver) throws
280     NullPointerException JavaDoc
281     {
282     }
283
284     /**
285      * This class is only used internally so this method should never
286      * be called.
287      */

288     public EntityResolver JavaDoc getEntityResolver() {
289     return null;
290     }
291
292     /**
293      * This class is only used internally so this method should never
294      * be called.
295      */

296     public void setErrorHandler(ErrorHandler JavaDoc handler) throws
297     NullPointerException JavaDoc
298     {
299     }
300
301     /**
302      * This class is only used internally so this method should never
303      * be called.
304      */

305     public void setProperty(String JavaDoc name, Object JavaDoc value) throws
306     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
307     }
308
309     /**
310      * This class is only used internally so this method should never
311      * be called.
312      */

313     public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
314     SAXNotSupportedException JavaDoc
315     {
316     return null;
317     }
318
319     /**
320      * This class is only used internally so this method should never
321      * be called.
322      */

323     public int getColumnNumber() {
324     return 0;
325     }
326     
327     /**
328      * This class is only used internally so this method should never
329      * be called.
330      */

331     public int getLineNumber() {
332     return 0;
333     }
334
335     /**
336      * This class is only used internally so this method should never
337      * be called.
338      */

339     public String JavaDoc getPublicId() {
340     return null;
341     }
342
343     /**
344      * This class is only used internally so this method should never
345      * be called.
346      */

347     public String JavaDoc getSystemId() {
348     return null;
349     }
350
351     // Debugging
352
private String JavaDoc getNodeTypeFromCode(short code) {
353     String JavaDoc retval = null;
354     switch (code) {
355     case Node.ATTRIBUTE_NODE :
356         retval = "ATTRIBUTE_NODE"; break;
357     case Node.CDATA_SECTION_NODE :
358         retval = "CDATA_SECTION_NODE"; break;
359     case Node.COMMENT_NODE :
360         retval = "COMMENT_NODE"; break;
361     case Node.DOCUMENT_FRAGMENT_NODE :
362         retval = "DOCUMENT_FRAGMENT_NODE"; break;
363     case Node.DOCUMENT_NODE :
364         retval = "DOCUMENT_NODE"; break;
365     case Node.DOCUMENT_TYPE_NODE :
366         retval = "DOCUMENT_TYPE_NODE"; break;
367     case Node.ELEMENT_NODE :
368         retval = "ELEMENT_NODE"; break;
369     case Node.ENTITY_NODE :
370         retval = "ENTITY_NODE"; break;
371     case Node.ENTITY_REFERENCE_NODE :
372         retval = "ENTITY_REFERENCE_NODE"; break;
373     case Node.NOTATION_NODE :
374         retval = "NOTATION_NODE"; break;
375     case Node.PROCESSING_INSTRUCTION_NODE :
376         retval = "PROCESSING_INSTRUCTION_NODE"; break;
377     case Node.TEXT_NODE:
378         retval = "TEXT_NODE"; break;
379         }
380     return retval;
381     }
382 }
383
Popular Tags