KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > sax > SAXDocumentSerializer


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
40 package com.sun.xml.fastinfoset.sax;
41
42 import com.sun.xml.fastinfoset.Encoder;
43 import com.sun.xml.fastinfoset.EncodingConstants;
44 import com.sun.xml.fastinfoset.QualifiedName;
45 import org.jvnet.fastinfoset.sax.FastInfosetWriter;
46 import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap;
47 import java.io.IOException JavaDoc;
48 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
49 import org.jvnet.fastinfoset.FastInfosetException;
50 import org.jvnet.fastinfoset.RestrictedAlphabet;
51 import org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
52 import org.xml.sax.Attributes JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54 import com.sun.xml.fastinfoset.CommonResourceBundle;
55
56
57 public class SAXDocumentSerializer extends Encoder implements FastInfosetWriter {
58     protected boolean _elementHasNamespaces = false;
59
60     protected boolean _charactersAsCDATA = false;
61     
62     public SAXDocumentSerializer() {
63     }
64
65
66     public void reset() {
67         super.reset();
68         
69         _elementHasNamespaces = false;
70         _charactersAsCDATA = false;
71     }
72     
73     // ContentHandler
74

75     public final void startDocument() throws SAXException JavaDoc {
76         try {
77             reset();
78             encodeHeader(false);
79             encodeInitialVocabulary();
80         } catch (IOException JavaDoc e) {
81             throw new SAXException JavaDoc("startDocument", e);
82         }
83     }
84
85     public final void endDocument() throws SAXException JavaDoc {
86         try {
87             encodeDocumentTermination();
88         } catch (IOException JavaDoc e) {
89             throw new SAXException JavaDoc("endDocument", e);
90         }
91     }
92
93     public final void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
94         try {
95             if (_elementHasNamespaces == false) {
96                 encodeTermination();
97
98                 // Mark the current buffer position to flag attributes if necessary
99
mark();
100                 _elementHasNamespaces = true;
101
102                 // Write out Element byte with namespaces
103
write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
104             }
105
106             encodeNamespaceAttribute(prefix, uri);
107         } catch (IOException JavaDoc e) {
108             throw new SAXException JavaDoc("startElement", e);
109         }
110     }
111
112     public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException JavaDoc {
113         // TODO consider using buffer for encoding of attributes, then pre-counting is not necessary
114
final int attributeCount = (atts.getLength() > 0) ? countAttributes(atts) : 0;
115         try {
116             if (_elementHasNamespaces) {
117                 _elementHasNamespaces = false;
118
119                 if (attributeCount > 0) {
120                     // Flag the marked byte with attributes
121
_octetBuffer[_markIndex] |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG;
122                 }
123                 resetMark();
124
125                 write(EncodingConstants.TERMINATOR);
126
127                 _b = 0;
128             } else {
129                 encodeTermination();
130
131                 _b = EncodingConstants.ELEMENT;
132                 if (attributeCount > 0) {
133                     _b |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG;
134                 }
135             }
136
137             encodeElement(namespaceURI, qName, localName);
138
139             if (attributeCount > 0) {
140                 boolean addToTable;
141                 String JavaDoc value;
142                 if (atts instanceof EncodingAlgorithmAttributes) {
143                     final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes)atts;
144                     for (int i = 0; i < eAtts.getLength(); i++) {
145                         if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) {
146                             value = eAtts.getValue(i);
147                             if (value != null) {
148                                 addToTable = (value.length() < attributeValueSizeConstraint) ? true : false;
149                                 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable);
150                             } else {
151                                 encodeNonIdentifyingStringOnFirstBit(eAtts.getAlgorithmURI(i),
152                                         eAtts.getAlgorithmIndex(i), eAtts.getAlgorithmData(i));
153                             }
154                         }
155                     }
156                 } else {
157                     for (int i = 0; i < atts.getLength(); i++) {
158                         if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) {
159                             value = atts.getValue(i);
160                             addToTable = (value.length() < attributeValueSizeConstraint) ? true : false;
161                             encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable);
162                         }
163                     }
164                 }
165                 _b = EncodingConstants.TERMINATOR;
166                 _terminate = true;
167             }
168         } catch (IOException JavaDoc e) {
169             throw new SAXException JavaDoc("startElement", e);
170         } catch (FastInfosetException e) {
171             throw new SAXException JavaDoc("startElement", e);
172         }
173     }
174
175     public final int countAttributes(Attributes atts) {
176         // Count attributes ignoring any in the XMLNS namespace
177
// Note, such attributes may be produced when transforming from a DOM node
178
int count = 0;
179         for (int i = 0; i < atts.getLength(); i++) {
180             final String JavaDoc uri = atts.getURI(i);
181             if (uri == "http://www.w3.org/2000/xmlns/" || uri.equals("http://www.w3.org/2000/xmlns/")) {
182                 continue;
183             }
184             count++;
185         }
186         return count;
187     }
188
189     public final void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
190         try {
191             encodeElementTermination();
192         } catch (IOException JavaDoc e) {
193             throw new SAXException JavaDoc("startElement", e);
194         }
195     }
196
197     public final void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
198         if (length <= 0) {
199             return;
200         }
201
202         try {
203             encodeTermination();
204
205             if (!_charactersAsCDATA) {
206                 encodeCharacters(ch, start, length);
207             } else {
208                 encodeCIIBuiltInAlgorithmDataAsCDATA(ch, start, length);
209             }
210         } catch (IOException JavaDoc e) {
211             throw new SAXException JavaDoc(e);
212         } catch (FastInfosetException e) {
213             throw new SAXException JavaDoc(e);
214         }
215     }
216
217     public final void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
218         characters(ch, start, length);
219     }
220
221     public final void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
222         try {
223             if (target == "") {
224                 throw new SAXException JavaDoc(CommonResourceBundle.getInstance().getString("message.processingInstructionTargetIsEmpty"));
225             }
226             encodeTermination();
227
228             encodeProcessingInstruction(target, data);
229         } catch (IOException JavaDoc e) {
230             throw new SAXException JavaDoc("processingInstruction", e);
231         }
232     }
233
234     public final void setDocumentLocator(org.xml.sax.Locator JavaDoc locator) {
235     }
236
237     public final void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
238     }
239
240
241
242     // LexicalHandler
243

244     public final void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
245         try {
246             encodeTermination();
247
248             encodeComment(ch, start, length);
249         } catch (IOException JavaDoc e) {
250             throw new SAXException JavaDoc("startElement", e);
251         }
252     }
253
254     public final void startCDATA() throws SAXException JavaDoc {
255         _charactersAsCDATA = true;
256     }
257
258     public final void endCDATA() throws SAXException JavaDoc {
259         _charactersAsCDATA = false;
260     }
261
262     public final void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
263     }
264
265     public final void endDTD() throws SAXException JavaDoc {
266     }
267
268     public final void startEntity(String JavaDoc name) throws SAXException JavaDoc {
269     }
270
271     public final void endEntity(String JavaDoc name) throws SAXException JavaDoc {
272     }
273
274
275     // EncodingAlgorithmContentHandler
276

277     public final void octets(String JavaDoc URI, int id, byte[] b, int start, int length) throws SAXException JavaDoc {
278         if (length <= 0) {
279             return;
280         }
281
282         try {
283             encodeTermination();
284
285             encodeNonIdentifyingStringOnThirdBit(URI, id, b, start, length);
286         } catch (IOException JavaDoc e) {
287             throw new SAXException JavaDoc(e);
288         } catch (FastInfosetException e) {
289             throw new SAXException JavaDoc(e);
290         }
291     }
292
293     public final void object(String JavaDoc URI, int id, Object JavaDoc data) throws SAXException JavaDoc {
294         try {
295             encodeTermination();
296
297             encodeNonIdentifyingStringOnThirdBit(URI, id, data);
298         } catch (IOException JavaDoc e) {
299             throw new SAXException JavaDoc(e);
300         } catch (FastInfosetException e) {
301             throw new SAXException JavaDoc(e);
302         }
303     }
304
305
306     // PrimitiveTypeContentHandler
307

308     public final void bytes(byte[] b, int start, int length) throws SAXException JavaDoc {
309         if (length <= 0) {
310             return;
311         }
312
313         try {
314             encodeTermination();
315
316             encodeCIIOctetAlgorithmData(EncodingAlgorithmIndexes.BASE64, b, start, length);
317         } catch (IOException JavaDoc e) {
318             throw new SAXException JavaDoc(e);
319         }
320     }
321
322     public final void shorts(short[] s, int start, int length) throws SAXException JavaDoc {
323         if (length <= 0) {
324             return;
325         }
326
327         try {
328             encodeTermination();
329
330             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.SHORT, s, start, length);
331         } catch (IOException JavaDoc e) {
332             throw new SAXException JavaDoc(e);
333         } catch (FastInfosetException e) {
334             throw new SAXException JavaDoc(e);
335         }
336     }
337
338     public final void ints(int[] i, int start, int length) throws SAXException JavaDoc {
339         if (length <= 0) {
340             return;
341         }
342
343         try {
344             encodeTermination();
345
346             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.INT, i, start, length);
347         } catch (IOException JavaDoc e) {
348             throw new SAXException JavaDoc(e);
349         } catch (FastInfosetException e) {
350             throw new SAXException JavaDoc(e);
351         }
352     }
353
354     public final void longs(long[] l, int start, int length) throws SAXException JavaDoc {
355         if (length <= 0) {
356             return;
357         }
358
359         try {
360             encodeTermination();
361
362             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.LONG, l, start, length);
363         } catch (IOException JavaDoc e) {
364             throw new SAXException JavaDoc(e);
365         } catch (FastInfosetException e) {
366             throw new SAXException JavaDoc(e);
367         }
368     }
369
370     public final void booleans(boolean[] b, int start, int length) throws SAXException JavaDoc {
371         if (length <= 0) {
372             return;
373         }
374
375         try {
376             encodeTermination();
377
378             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.BOOLEAN, b, start, length);
379         } catch (IOException JavaDoc e) {
380             throw new SAXException JavaDoc(e);
381         } catch (FastInfosetException e) {
382             throw new SAXException JavaDoc(e);
383         }
384     }
385     
386     public final void floats(float[] f, int start, int length) throws SAXException JavaDoc {
387         if (length <= 0) {
388             return;
389         }
390
391         try {
392             encodeTermination();
393
394             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.FLOAT, f, start, length);
395         } catch (IOException JavaDoc e) {
396             throw new SAXException JavaDoc(e);
397         } catch (FastInfosetException e) {
398             throw new SAXException JavaDoc(e);
399         }
400     }
401
402     public final void doubles(double[] d, int start, int length) throws SAXException JavaDoc {
403         if (length <= 0) {
404             return;
405         }
406
407         try {
408             encodeTermination();
409
410             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.DOUBLE, d, start, length);
411         } catch (IOException JavaDoc e) {
412             throw new SAXException JavaDoc(e);
413         } catch (FastInfosetException e) {
414             throw new SAXException JavaDoc(e);
415         }
416     }
417
418     public void uuids(long[] msblsb, int start, int length) throws SAXException JavaDoc {
419         if (length <= 0) {
420             return;
421         }
422
423         try {
424             encodeTermination();
425
426             encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.UUID, msblsb, start, length);
427         } catch (IOException JavaDoc e) {
428             throw new SAXException JavaDoc(e);
429         } catch (FastInfosetException e) {
430             throw new SAXException JavaDoc(e);
431         }
432     }
433
434
435     // RestrictedAlphabetContentHandler
436

437     public void numericCharacters(char ch[], int start, int length) throws SAXException JavaDoc {
438         if (length <= 0) {
439             return;
440         }
441
442         try {
443             encodeTermination();
444
445             encodeFourBitCharacters(RestrictedAlphabet.NUMERIC_CHARACTERS_INDEX, EncodingConstants.NUMERIC_CHARACTERS_TABLE, ch, start, length);
446         } catch (IOException JavaDoc e) {
447             throw new SAXException JavaDoc(e);
448         } catch (FastInfosetException e) {
449             throw new SAXException JavaDoc(e);
450         }
451     }
452     
453     public void dateTimeCharacters(char ch[], int start, int length) throws SAXException JavaDoc {
454         if (length <= 0) {
455             return;
456         }
457
458         try {
459             encodeTermination();
460
461             encodeFourBitCharacters(RestrictedAlphabet.DATE_TIME_CHARACTERS_INDEX, EncodingConstants.DATE_TIME_CHARACTERS_TABLE, ch, start, length);
462         } catch (IOException JavaDoc e) {
463             throw new SAXException JavaDoc(e);
464         } catch (FastInfosetException e) {
465             throw new SAXException JavaDoc(e);
466         }
467     }
468     
469     public void alphabetCharacters(String JavaDoc alphabet, char ch[], int start, int length) throws SAXException JavaDoc {
470         if (length <= 0) {
471             return;
472         }
473
474         try {
475             encodeTermination();
476
477             encodeAlphabetCharacters(alphabet, ch, start, length);
478         } catch (IOException JavaDoc e) {
479             throw new SAXException JavaDoc(e);
480         } catch (FastInfosetException e) {
481             throw new SAXException JavaDoc(e);
482         }
483     }
484
485
486
487     protected final void encodeElement(String JavaDoc namespaceURI, String JavaDoc qName, String JavaDoc localName) throws IOException JavaDoc {
488         LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(qName);
489         if (entry._valueIndex > 0) {
490             QualifiedName[] names = entry._value;
491             for (int i = 0; i < entry._valueIndex; i++) {
492                 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
493                     encodeNonZeroIntegerOnThirdBit(names[i].index);
494                     return;
495                 }
496             }
497         }
498
499         encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefixFromQualifiedName(qName),
500                 localName, entry);
501     }
502
503     protected final boolean encodeAttribute(String JavaDoc namespaceURI, String JavaDoc qName, String JavaDoc localName) throws IOException JavaDoc {
504         LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(qName);
505         if (entry._valueIndex > 0) {
506             QualifiedName[] names = entry._value;
507             for (int i = 0; i < entry._valueIndex; i++) {
508                 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
509                     encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
510                     return true;
511                 }
512             }
513         }
514
515         return encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefixFromQualifiedName(qName),
516                 localName, entry);
517     }
518 }
519
Popular Tags