KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > dom > DOMDocumentSerializer


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.dom;
40
41 import com.sun.xml.fastinfoset.Encoder;
42 import com.sun.xml.fastinfoset.EncodingConstants;
43 import com.sun.xml.fastinfoset.QualifiedName;
44 import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap;
45 import java.io.IOException JavaDoc;
46 import org.jvnet.fastinfoset.FastInfosetException;
47 import org.w3c.dom.Document JavaDoc;
48 import org.w3c.dom.NamedNodeMap JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 public class DOMDocumentSerializer extends Encoder {
53     
54     public DOMDocumentSerializer() {
55     }
56     
57     public final void serialize(Node JavaDoc n) throws IOException JavaDoc {
58         switch (n.getNodeType()) {
59             case Node.DOCUMENT_NODE:
60                 serialize((Document JavaDoc)n);
61             case Node.ELEMENT_NODE:
62                 serializeElementAsDocument(n);
63                 break;
64             case Node.COMMENT_NODE:
65                 serializeComment(n);
66                 break;
67             case Node.PROCESSING_INSTRUCTION_NODE:
68                 serializeProcessingInstruction(n);
69                 break;
70         }
71     }
72     
73     public final void serialize(Document JavaDoc d) throws IOException JavaDoc {
74         reset();
75         encodeHeader(false);
76         encodeInitialVocabulary();
77         
78         final NodeList JavaDoc nl = d.getChildNodes();
79         for (int i = 0; i < nl.getLength(); i++) {
80             final Node JavaDoc n = nl.item(i);
81             switch (n.getNodeType()) {
82                 case Node.ELEMENT_NODE:
83                     serializeElement(n);
84                     break;
85                 case Node.COMMENT_NODE:
86                     serializeComment(n);
87                     break;
88                 case Node.PROCESSING_INSTRUCTION_NODE:
89                     serializeProcessingInstruction(n);
90                     break;
91             }
92         }
93         encodeDocumentTermination();
94     }
95
96     protected final void serializeElementAsDocument(Node JavaDoc e) throws IOException JavaDoc {
97         reset();
98         encodeHeader(false);
99         encodeInitialVocabulary();
100
101         serializeElement(e);
102         
103         encodeDocumentTermination();
104     }
105     
106
107     protected Node JavaDoc[] _namespaceAttributes = new Node JavaDoc[4];
108     protected Node JavaDoc[] _attributes = new Node JavaDoc[32];
109     
110     protected final void serializeElement(Node JavaDoc e) throws IOException JavaDoc {
111         encodeTermination();
112         
113         int namespaceAttributesSize = 0;
114         int attributesSize = 0;
115         if (e.hasAttributes()) {
116             /*
117              * Split the attribute nodes into namespace attributes
118              * or normal attributes.
119              */

120             final NamedNodeMap JavaDoc nnm = e.getAttributes();
121             for (int i = 0; i < nnm.getLength(); i++) {
122                 final Node JavaDoc a = nnm.item(i);
123                 final String JavaDoc namespaceURI = a.getNamespaceURI();
124                 if (namespaceURI != null && namespaceURI.equals("http://www.w3.org/2000/xmlns/")) {
125                     if (namespaceAttributesSize == _namespaceAttributes.length) {
126                         final Node JavaDoc[] attributes = new Node JavaDoc[namespaceAttributesSize * 3 / 2 + 1];
127                         System.arraycopy(_namespaceAttributes, 0, attributes, 0, namespaceAttributesSize);
128                         _namespaceAttributes = attributes;
129                     }
130                     _namespaceAttributes[namespaceAttributesSize++] = a;
131                 } else {
132                     if (attributesSize == _attributes.length) {
133                         final Node JavaDoc[] attributes = new Node JavaDoc[attributesSize * 3 / 2 + 1];
134                         System.arraycopy(_attributes, 0, attributes, 0, attributesSize);
135                         _attributes = attributes;
136                     }
137                     _attributes[attributesSize++] = a;
138                 }
139             }
140         }
141         
142         if (namespaceAttributesSize > 0) {
143             if (attributesSize > 0) {
144                 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG |
145                         EncodingConstants.ELEMENT_ATTRIBUTE_FLAG);
146             } else {
147                 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
148             }
149
150             // Serialize the namespace attributes
151
for (int i = 0; i < namespaceAttributesSize; i++) {
152                 final Node JavaDoc a = _namespaceAttributes[i];
153                 _namespaceAttributes[i] = null;
154                 String JavaDoc prefix = a.getLocalName();
155                 if (prefix == "xmlns" || prefix.equals("xmlns")) {
156                     prefix = "";
157                 }
158                 final String JavaDoc uri = a.getNodeValue();
159                 encodeNamespaceAttribute(prefix, uri);
160             }
161             
162             write(EncodingConstants.TERMINATOR);
163             _b = 0;
164         } else {
165             _b = (attributesSize > 0) ? EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_ATTRIBUTE_FLAG :
166                 EncodingConstants.ELEMENT;
167         }
168         
169         String JavaDoc namespaceURI = e.getNamespaceURI();
170         namespaceURI = (namespaceURI == null) ? "" : namespaceURI;
171         encodeElement(namespaceURI, e.getNodeName(), e.getLocalName());
172
173         if (attributesSize > 0) {
174             // Serialize the attributes
175
for (int i = 0; i < attributesSize; i++) {
176                 final Node JavaDoc a = _attributes[i];
177                 _attributes[i] = null;
178                 namespaceURI = a.getNamespaceURI();
179                 namespaceURI = (namespaceURI == null) ? "" : namespaceURI;
180                 encodeAttribute(namespaceURI, a.getNodeName(), a.getLocalName());
181                 
182                 final String JavaDoc value = a.getNodeValue();
183                 final boolean addToTable = (value.length() < attributeValueSizeConstraint) ? true : false;
184                 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable);
185             }
186             
187             _b = EncodingConstants.TERMINATOR;
188             _terminate = true;
189         }
190         
191         if (e.hasChildNodes()) {
192             // Serialize the children
193
final NodeList JavaDoc nl = e.getChildNodes();
194             for (int i = 0; i < nl.getLength(); i++) {
195                 final Node JavaDoc n = nl.item(i);
196                 switch (n.getNodeType()) {
197                     case Node.ELEMENT_NODE:
198                         serializeElement(n);
199                         break;
200                     case Node.TEXT_NODE:
201                         serializeText(n);
202                         break;
203                     case Node.CDATA_SECTION_NODE:
204                         serializeCDATA(n);
205                         break;
206                     case Node.COMMENT_NODE:
207                         serializeComment(n);
208                         break;
209                     case Node.PROCESSING_INSTRUCTION_NODE:
210                         serializeProcessingInstruction(n);
211                         break;
212                 }
213             }
214         }
215         encodeElementTermination();
216     }
217     
218     protected final void serializeText(Node JavaDoc t) throws IOException JavaDoc {
219         final String JavaDoc text = t.getNodeValue();
220         
221         final int length = (text != null) ? text.length() : 0;
222         if (length == 0) {
223             return;
224         } else if (length < _charBuffer.length) {
225             encodeTermination();
226             text.getChars(0, length, _charBuffer, 0);
227             encodeCharacters(_charBuffer, 0, length);
228         } else {
229             encodeTermination();
230             final char ch[] = text.toCharArray();
231             encodeCharactersNoClone(ch, 0, length);
232         }
233     }
234
235     protected final void serializeCDATA(Node JavaDoc t) throws IOException JavaDoc {
236         final String JavaDoc text = t.getNodeValue();
237         
238         final int length = (text != null) ? text.length() : 0;
239         if (length == 0) {
240             return;
241         } else {
242             encodeTermination();
243             final char ch[] = text.toCharArray();
244             try {
245                 encodeCIIBuiltInAlgorithmDataAsCDATA(ch, 0, length);
246             } catch (FastInfosetException e) {
247                 throw new IOException JavaDoc("");
248             }
249         }
250     }
251     
252     protected final void serializeComment(Node JavaDoc c) throws IOException JavaDoc {
253         encodeTermination();
254         
255         final String JavaDoc comment = c.getNodeValue();
256         
257         final int length = (comment != null) ? comment.length() : 0;
258         if (length == 0) {
259             encodeComment(_charBuffer, 0, 0);
260         } else if (length < _charBuffer.length) {
261             comment.getChars(0, length, _charBuffer, 0);
262             encodeComment(_charBuffer, 0, length);
263         } else {
264             final char ch[] = comment.toCharArray();
265             encodeCommentNoClone(ch, 0, length);
266         }
267     }
268     
269     protected final void serializeProcessingInstruction(Node JavaDoc pi) throws IOException JavaDoc {
270         encodeTermination();
271         
272         final String JavaDoc target = pi.getNodeName();
273         final String JavaDoc data = pi.getNodeValue();
274         encodeProcessingInstruction(target, data);
275     }
276     
277     protected final void encodeElement(String JavaDoc namespaceURI, String JavaDoc qName, String JavaDoc localName) throws IOException JavaDoc {
278         LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(qName);
279         if (entry._valueIndex > 0) {
280             final QualifiedName[] names = entry._value;
281             for (int i = 0; i < entry._valueIndex; i++) {
282                 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
283                     encodeNonZeroIntegerOnThirdBit(names[i].index);
284                     return;
285                 }
286             }
287         }
288         
289         // Was DOM node created using an NS-aware call?
290
if (localName != null) {
291             encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefixFromQualifiedName(qName),
292                     localName, entry);
293         }
294         else {
295             encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, "", qName, entry);
296         }
297     }
298
299     protected final void encodeAttribute(String JavaDoc namespaceURI, String JavaDoc qName, String JavaDoc localName) throws IOException JavaDoc {
300         LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(qName);
301         if (entry._valueIndex > 0) {
302             final QualifiedName[] names = entry._value;
303             for (int i = 0; i < entry._valueIndex; i++) {
304                 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
305                     encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
306                     return;
307                 }
308             }
309         }
310         
311         // Was DOM node created using an NS-aware call?
312
if (localName != null) {
313             encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefixFromQualifiedName(qName),
314                     localName, entry);
315         }
316         else {
317             encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, "", qName, entry);
318         }
319     }
320 }
321
Popular Tags