KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > util > DOMSerializer


1 /*
2  * Copyright 2003, 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 package org.apache.ws.jaxme.util;
18
19 import javax.xml.XMLConstants JavaDoc;
20
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.ext.LexicalHandler JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27
28 /** <p>Serializes a DOM node into a stream of SAX events.</p>
29  *
30  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
31  * @version $Id: DOMSerializer.java,v 1.8 2005/03/10 10:14:02 jochen Exp $
32  */

33 public class DOMSerializer {
34   private boolean namespaceDeclarationAttribute;
35   private boolean parentsNamespaceDeclarationDisabled;
36
37   /** <p>Sets whether XML declarations are being serialized as
38    * attributes or as SAX events (default).</p>
39    */

40   public void setNamespaceDeclarationAttribute(boolean pXmlDeclarationAttribute) {
41     namespaceDeclarationAttribute = pXmlDeclarationAttribute;
42   }
43
44   /** <p>Returns whether XML declarations are being serialized as
45    * attributes or as SAX events (default).</p>
46    */

47   public boolean isNamespaceDeclarationAttribute() {
48     return namespaceDeclarationAttribute;
49   }
50
51   /** <p>Returns whether XML declarations present in the parent nodes
52    * are being serialized (default) or not. This option takes effect
53    * only if the namespace declarations are sent as events. In other
54    * words, if the <code>namespaceDeclarationAttribute</code>
55    * properts is false.</p>
56    */

57   public void setParentsNamespaceDeclarationDisabled(boolean pParentsXmlDeclarationDisabled) {
58     parentsNamespaceDeclarationDisabled = pParentsXmlDeclarationDisabled;
59   }
60
61   /** <p>Sets whether XML declarations present in the parent nodes
62    * are being serialized (default) or not. This option takes effect
63    * only if the namespace declarations are sent as events. In other
64    * words, if the <code>namespaceDeclarationAttribute</code>
65    * properts is false.</p>
66    */

67   public boolean isParentsNamespaceDeclarationDisabled() {
68     return parentsNamespaceDeclarationDisabled;
69   }
70
71   protected void doSerializeChilds(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
72       throws SAXException JavaDoc {
73     for (Node JavaDoc child = pNode.getFirstChild(); child != null;
74           child = child.getNextSibling()) {
75       doSerialize(child, pHandler);
76     }
77   }
78
79   protected void parentsStartPrefixMappingEvents(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
80       throws SAXException JavaDoc {
81     if (pNode != null) {
82       parentsStartPrefixMappingEvents(pNode.getParentNode(), pHandler);
83       if (pNode.getNodeType() == Node.ELEMENT_NODE) {
84         startPrefixMappingEvents(pNode, pHandler);
85       }
86     }
87   }
88
89   protected void parentsEndPrefixMappingEvents(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
90       throws SAXException JavaDoc {
91     if (pNode != null) {
92       if (pNode.getNodeType() == Node.ELEMENT_NODE) {
93         endPrefixMappingEvents(pNode, pHandler);
94       }
95       parentsEndPrefixMappingEvents(pNode.getParentNode(), pHandler);
96     }
97   }
98
99   protected void startPrefixMappingEvents(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
100       throws SAXException JavaDoc {
101     NamedNodeMap JavaDoc nnm = pNode.getAttributes();
102     if (nnm != null) {
103       for (int i = 0; i < nnm.getLength(); i++) {
104         Node JavaDoc attr = nnm.item(i);
105         if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
106           String JavaDoc prefix;
107           if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix())) {
108             prefix = attr.getLocalName();
109           } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getNodeName())) {
110             prefix = "";
111           } else {
112             throw new IllegalStateException JavaDoc("Unable to parse namespace declaration: " + attr.getNodeName());
113           }
114           String JavaDoc uri = attr.getNodeValue();
115           if (uri == null) {
116             uri = "";
117           }
118           pHandler.startPrefixMapping(prefix, uri);
119         }
120       }
121     }
122   }
123
124   protected void endPrefixMappingEvents(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
125       throws SAXException JavaDoc {
126     NamedNodeMap JavaDoc nnm = pNode.getAttributes();
127     if (nnm != null) {
128       for (int i = nnm.getLength()-1; i >= 0; i--) {
129         Node JavaDoc attr = nnm.item(i);
130         if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
131           String JavaDoc prefix = attr.getLocalName();
132           pHandler.endPrefixMapping(prefix);
133         }
134       }
135     }
136   }
137
138   /** Converts the given node <code>pNode</code> into a
139    * stream of SAX events, which are fired into the
140    * content handler <code>pHandler</code>.
141    */

142   public void serialize(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
143       throws SAXException JavaDoc {
144     if (!isNamespaceDeclarationAttribute() &&
145         !isParentsNamespaceDeclarationDisabled()) {
146       parentsStartPrefixMappingEvents(pNode.getParentNode(), pHandler);
147     }
148     doSerialize(pNode, pHandler);
149     if (!isNamespaceDeclarationAttribute() &&
150         !isParentsNamespaceDeclarationDisabled()) {
151       parentsEndPrefixMappingEvents(pNode.getParentNode(), pHandler);
152     }
153   }
154
155   protected void doSerialize(Node JavaDoc pNode, ContentHandler JavaDoc pHandler)
156       throws SAXException JavaDoc {
157     switch (pNode.getNodeType()) {
158       case Node.DOCUMENT_NODE:
159         pHandler.startDocument();
160         doSerializeChilds(pNode, pHandler);
161         pHandler.endDocument();
162         break;
163       case Node.DOCUMENT_FRAGMENT_NODE:
164         doSerializeChilds(pNode, pHandler);
165         break;
166       case Node.ELEMENT_NODE:
167         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
168         boolean isNamespaceDeclarationAttribute = isNamespaceDeclarationAttribute();
169         if (!isNamespaceDeclarationAttribute) {
170           startPrefixMappingEvents(pNode, pHandler);
171         }
172         NamedNodeMap JavaDoc nnm = pNode.getAttributes();
173         if (nnm != null) {
174           for (int i = 0; i < nnm.getLength(); i++) {
175             Node JavaDoc a = nnm.item(i);
176             if (isNamespaceDeclarationAttribute ||
177                 !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(a.getNamespaceURI())) {
178               String JavaDoc aUri = a.getNamespaceURI();
179               String JavaDoc aLocalName = a.getLocalName();
180               String JavaDoc aNodeName = a.getNodeName();
181               if (aLocalName == null) {
182                   if (aUri == null || aUri.length() == 0) {
183                       aLocalName = aNodeName;
184                   } else {
185                       throw new IllegalStateException JavaDoc("aLocalName is null");
186                   }
187               }
188               attr.addAttribute(aUri == null ? "" : aUri, aNodeName,
189                                 aLocalName, "CDATA", a.getNodeValue());
190             }
191           }
192         }
193         String JavaDoc nUri = pNode.getNamespaceURI();
194         if (nUri == null) {
195             nUri = "";
196         }
197         pHandler.startElement(nUri, pNode.getLocalName(),
198                               pNode.getNodeName(), attr);
199         doSerializeChilds(pNode, pHandler);
200         pHandler.endElement(nUri, pNode.getLocalName(),
201                             pNode.getNodeName());
202         if (!isNamespaceDeclarationAttribute) {
203           endPrefixMappingEvents(pNode, pHandler);
204         }
205         break;
206       case Node.TEXT_NODE:
207       case Node.CDATA_SECTION_NODE:
208         {
209           String JavaDoc s = pNode.getNodeValue();
210           pHandler.characters(s.toCharArray(), 0, s.length());
211         }
212         break;
213       case Node.PROCESSING_INSTRUCTION_NODE:
214         pHandler.processingInstruction(pNode.getNodeName(), pNode.getNodeValue());
215         break;
216       case Node.ENTITY_REFERENCE_NODE:
217         pHandler.skippedEntity(pNode.getNodeName());
218         break;
219       case Node.COMMENT_NODE:
220         if (pHandler instanceof LexicalHandler JavaDoc) {
221           String JavaDoc s = pNode.getNodeValue();
222           ((LexicalHandler JavaDoc) pHandler).comment(s.toCharArray(), 0, s.length());
223         }
224         break;
225       default:
226         throw new IllegalStateException JavaDoc("Unknown node type: " + pNode.getNodeType());
227     }
228   }
229 }
230
Popular Tags