KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > 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.xs.impl;
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. This code
29  * is duplicated and copied from
30  * {@link org.apache.ws.jaxme.util.DOMSerializer}. The reason for
31  * duplicating the code is, that the JaxMe runtime should not depend
32  * on JaxMeXS. On the other hand, JaxMeXS should not depend on
33  * JaxMe.</p>
34  *
35  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
36  * @version $Id: DOMSerializer.java,v 1.7 2004/08/27 01:02:42 jochen Exp $
37  */

38 public class DOMSerializer {
39     private boolean namespaceDeclarationAttribute;
40     private boolean parentsNamespaceDeclarationDisabled;
41     
42     /** <p>Sets whether XML declarations are being serialized as
43      * attributes or as SAX events (default).</p>
44      */

45     public void setNamespaceDeclarationAttribute(boolean pXmlDeclarationAttribute) {
46         namespaceDeclarationAttribute = pXmlDeclarationAttribute;
47     }
48     
49     /** <p>Returns whether XML declarations are being serialized as
50      * attributes or as SAX events (default).</p>
51      */

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

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

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

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