KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > 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.10 2004/12/10 18:39:15 santiagopg Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.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 com.sun.org.apache.xml.internal.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 com.sun.org.apache.xml.internal.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             int colon;
141         String JavaDoc prefix;
142         final NamedNodeMap JavaDoc map = node.getAttributes();
143         final int length = map.getLength();
144
145         // Process all namespace attributes first
146
for (int i = 0; i < length; i++) {
147         final Node JavaDoc attr = map.item(i);
148         final String JavaDoc qnameAttr = attr.getNodeName();
149
150                 // Is this a namespace declaration?
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         }
159             
160         // Process all non-namespace attributes next
161
NamespaceMappings nm = new NamespaceMappings();
162         for (int i = 0; i < length; i++) {
163         final Node JavaDoc attr = map.item(i);
164         final String JavaDoc qnameAttr = attr.getNodeName();
165
166                 // Is this a regular attribute?
167
if (!qnameAttr.startsWith(XMLNS_PREFIX)) {
168             final String JavaDoc uriAttr = attr.getNamespaceURI();
169             // Uri may be implicitly declared
170
if (uriAttr != null && !uriAttr.equals(EMPTYSTRING) ) {
171             colon = qnameAttr.lastIndexOf(':');
172                            
173                         // Fix for bug 26319
174
// For attributes not given an prefix explictly
175
// but having a namespace uri we need
176
// to explicitly generate the prefix
177
String JavaDoc newPrefix = nm.lookupPrefix(uriAttr);
178                         if (newPrefix == null)
179                             newPrefix = nm.generateNextPrefix();
180             prefix = (colon > 0) ? qnameAttr.substring(0, colon)
181                 : newPrefix;
182             _handler.namespaceAfterStartElement(prefix, uriAttr);
183                 _handler.addAttribute((prefix + ":" + qnameAttr),
184                             attr.getNodeValue());
185             } else {
186                          _handler.addAttribute(qnameAttr, attr.getNodeValue());
187                     }
188                 }
189         }
190
191         // Now element namespace and children
192
final String JavaDoc uri = node.getNamespaceURI();
193             final String JavaDoc localName = node.getLocalName();
194
195         // Uri may be implicitly declared
196
if (uri != null) {
197         colon = qname.lastIndexOf(':');
198         prefix = (colon > 0) ? qname.substring(0, colon) : EMPTYSTRING;
199         _handler.namespaceAfterStartElement(prefix, uri);
200         }else {
201                   // Fix for bug 26319
202
// If an element foo is created using
203
// createElementNS(null,locName)
204
// then the element should be serialized
205
// <foo xmlns=" "/>
206
if (uri == null && localName != null) {
207              prefix = EMPTYSTRING;
208              _handler.namespaceAfterStartElement(prefix, EMPTYSTRING);
209                  }
210             }
211
212         // Traverse all child nodes of the element (if any)
213
next = node.getFirstChild();
214         while (next != null) {
215         parse(next);
216         next = next.getNextSibling();
217         }
218
219         // Generate SAX event to close element
220
_handler.endElement(qname);
221         break;
222
223     case Node.PROCESSING_INSTRUCTION_NODE:
224         _handler.processingInstruction(node.getNodeName(),
225                        node.getNodeValue());
226         break;
227
228     case Node.TEXT_NODE:
229         _handler.characters(node.getNodeValue());
230         break;
231     }
232     }
233
234     /**
235      * This class is only used internally so this method should never
236      * be called.
237      */

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

246     public ErrorHandler JavaDoc getErrorHandler() {
247     return null;
248     }
249
250     /**
251      * This class is only used internally so this method should never
252      * be called.
253      */

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

264     public void setFeature(String JavaDoc name, boolean value) throws
265     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
266     {
267     }
268
269     /**
270      * This class is only used internally so this method should never
271      * be called.
272      */

273     public void parse(String JavaDoc sysId) throws IOException JavaDoc, SAXException JavaDoc {
274     throw new IOException JavaDoc("This method is not yet implemented.");
275     }
276
277     /**
278      * This class is only used internally so this method should never
279      * be called.
280      */

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

288     public void setEntityResolver(EntityResolver JavaDoc resolver) throws
289     NullPointerException JavaDoc
290     {
291     }
292
293     /**
294      * This class is only used internally so this method should never
295      * be called.
296      */

297     public EntityResolver JavaDoc getEntityResolver() {
298     return null;
299     }
300
301     /**
302      * This class is only used internally so this method should never
303      * be called.
304      */

305     public void setErrorHandler(ErrorHandler JavaDoc handler) throws
306     NullPointerException JavaDoc
307     {
308     }
309
310     /**
311      * This class is only used internally so this method should never
312      * be called.
313      */

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

322     public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
323     SAXNotSupportedException JavaDoc
324     {
325     return null;
326     }
327
328     /**
329      * This class is only used internally so this method should never
330      * be called.
331      */

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

340     public int getLineNumber() {
341     return 0;
342     }
343
344     /**
345      * This class is only used internally so this method should never
346      * be called.
347      */

348     public String JavaDoc getPublicId() {
349     return null;
350     }
351
352     /**
353      * This class is only used internally so this method should never
354      * be called.
355      */

356     public String JavaDoc getSystemId() {
357     return null;
358     }
359
360     // Debugging
361
private String JavaDoc getNodeTypeFromCode(short code) {
362     String JavaDoc retval = null;
363     switch (code) {
364     case Node.ATTRIBUTE_NODE :
365         retval = "ATTRIBUTE_NODE"; break;
366     case Node.CDATA_SECTION_NODE :
367         retval = "CDATA_SECTION_NODE"; break;
368     case Node.COMMENT_NODE :
369         retval = "COMMENT_NODE"; break;
370     case Node.DOCUMENT_FRAGMENT_NODE :
371         retval = "DOCUMENT_FRAGMENT_NODE"; break;
372     case Node.DOCUMENT_NODE :
373         retval = "DOCUMENT_NODE"; break;
374     case Node.DOCUMENT_TYPE_NODE :
375         retval = "DOCUMENT_TYPE_NODE"; break;
376     case Node.ELEMENT_NODE :
377         retval = "ELEMENT_NODE"; break;
378     case Node.ENTITY_NODE :
379         retval = "ENTITY_NODE"; break;
380     case Node.ENTITY_REFERENCE_NODE :
381         retval = "ENTITY_REFERENCE_NODE"; break;
382     case Node.NOTATION_NODE :
383         retval = "NOTATION_NODE"; break;
384     case Node.PROCESSING_INSTRUCTION_NODE :
385         retval = "PROCESSING_INSTRUCTION_NODE"; break;
386     case Node.TEXT_NODE:
387         retval = "TEXT_NODE"; break;
388         }
389     return retval;
390     }
391 }
392
Popular Tags