001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: *
025: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.sax;
057:
058: import com.sun.xml.internal.fastinfoset.Encoder;
059: import com.sun.xml.internal.fastinfoset.EncodingConstants;
060: import com.sun.xml.internal.fastinfoset.QualifiedName;
061: import com.sun.xml.internal.org.jvnet.fastinfoset.sax.FastInfosetWriter;
062: import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
063: import java.io.IOException;
064: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
065: import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetException;
066: import com.sun.xml.internal.org.jvnet.fastinfoset.RestrictedAlphabet;
067: import com.sun.xml.internal.org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
068: import org.xml.sax.Attributes;
069: import org.xml.sax.SAXException;
070: import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
071:
072: /**
073: * The Fast Infoset SAX serializer.
074: * <p>
075: * Instantiate this serializer to serialize a fast infoset document in accordance
076: * with the SAX API.
077: * <p>
078: * This utilizes the SAX API in a reverse manner to that of parsing. It is the
079: * responsibility of the client to call the appropriate event methods on the
080: * SAX handlers, and to ensure that such a sequence of methods calls results
081: * in the production well-formed fast infoset documents. The
082: * SAXDocumentSerializer performs no well-formed checks.
083: *
084: * <p>
085: * More than one fast infoset document may be encoded to the
086: * {@link java.io.OutputStream}.
087: */
088: public class SAXDocumentSerializer extends Encoder implements
089: FastInfosetWriter {
090: protected boolean _elementHasNamespaces = false;
091:
092: protected boolean _charactersAsCDATA = false;
093:
094: public SAXDocumentSerializer() {
095: }
096:
097: public void reset() {
098: super .reset();
099:
100: _elementHasNamespaces = false;
101: _charactersAsCDATA = false;
102: }
103:
104: // ContentHandler
105:
106: public final void startDocument() throws SAXException {
107: try {
108: reset();
109: encodeHeader(false);
110: encodeInitialVocabulary();
111: } catch (IOException e) {
112: throw new SAXException("startDocument", e);
113: }
114: }
115:
116: public final void endDocument() throws SAXException {
117: try {
118: encodeDocumentTermination();
119: } catch (IOException e) {
120: throw new SAXException("endDocument", e);
121: }
122: }
123:
124: public final void startPrefixMapping(String prefix, String uri)
125: throws SAXException {
126: try {
127: if (_elementHasNamespaces == false) {
128: encodeTermination();
129:
130: // Mark the current buffer position to flag attributes if necessary
131: mark();
132: _elementHasNamespaces = true;
133:
134: // Write out Element byte with namespaces
135: write(EncodingConstants.ELEMENT
136: | EncodingConstants.ELEMENT_NAMESPACES_FLAG);
137: }
138:
139: encodeNamespaceAttribute(prefix, uri);
140: } catch (IOException e) {
141: throw new SAXException("startElement", e);
142: }
143: }
144:
145: public final void startElement(String namespaceURI,
146: String localName, String qName, Attributes atts)
147: throws SAXException {
148: // TODO consider using buffer for encoding of attributes, then pre-counting is not necessary
149: final int attributeCount = (atts != null && atts.getLength() > 0) ? countAttributes(atts)
150: : 0;
151: try {
152: if (_elementHasNamespaces) {
153: _elementHasNamespaces = false;
154:
155: if (attributeCount > 0) {
156: // Flag the marked byte with attributes
157: _octetBuffer[_markIndex] |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG;
158: }
159: resetMark();
160:
161: write(EncodingConstants.TERMINATOR);
162:
163: _b = 0;
164: } else {
165: encodeTermination();
166:
167: _b = EncodingConstants.ELEMENT;
168: if (attributeCount > 0) {
169: _b |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG;
170: }
171: }
172:
173: encodeElement(namespaceURI, qName, localName);
174:
175: if (attributeCount > 0) {
176: boolean addToTable;
177: String value;
178: if (atts instanceof EncodingAlgorithmAttributes) {
179: final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes) atts;
180: for (int i = 0; i < eAtts.getLength(); i++) {
181: if (encodeAttribute(atts.getURI(i), atts
182: .getQName(i), atts.getLocalName(i))) {
183: final Object data = eAtts
184: .getAlgorithmData(i);
185: // If data is null then there is no algorithm data
186: if (data == null) {
187: value = eAtts.getValue(i);
188: addToTable = (value.length() < attributeValueSizeConstraint) ? true
189: : false;
190: encodeNonIdentifyingStringOnFirstBit(
191: value, _v.attributeValue,
192: addToTable);
193: } else {
194: encodeNonIdentifyingStringOnFirstBit(
195: eAtts.getAlgorithmURI(i), eAtts
196: .getAlgorithmIndex(i),
197: data);
198: }
199: }
200: }
201: } else {
202: for (int i = 0; i < atts.getLength(); i++) {
203: if (encodeAttribute(atts.getURI(i), atts
204: .getQName(i), atts.getLocalName(i))) {
205: value = atts.getValue(i);
206: addToTable = (value.length() < attributeValueSizeConstraint) ? true
207: : false;
208: encodeNonIdentifyingStringOnFirstBit(value,
209: _v.attributeValue, addToTable);
210: }
211: }
212: }
213: _b = EncodingConstants.TERMINATOR;
214: _terminate = true;
215: }
216: } catch (IOException e) {
217: throw new SAXException("startElement", e);
218: } catch (FastInfosetException e) {
219: throw new SAXException("startElement", e);
220: }
221: }
222:
223: public final int countAttributes(Attributes atts) {
224: // Count attributes ignoring any in the XMLNS namespace
225: // Note, such attributes may be produced when transforming from a DOM node
226: int count = 0;
227: for (int i = 0; i < atts.getLength(); i++) {
228: final String uri = atts.getURI(i);
229: if (uri == "http://www.w3.org/2000/xmlns/"
230: || uri.equals("http://www.w3.org/2000/xmlns/")) {
231: continue;
232: }
233: count++;
234: }
235: return count;
236: }
237:
238: public final void endElement(String namespaceURI, String localName,
239: String qName) throws SAXException {
240: try {
241: encodeElementTermination();
242: } catch (IOException e) {
243: throw new SAXException("startElement", e);
244: }
245: }
246:
247: public final void characters(char[] ch, int start, int length)
248: throws SAXException {
249: if (length <= 0) {
250: return;
251: }
252:
253: if (getIgnoreWhiteSpaceTextContent()
254: && isWhiteSpace(ch, start, length))
255: return;
256:
257: try {
258: encodeTermination();
259:
260: if (!_charactersAsCDATA) {
261: encodeCharacters(ch, start, length);
262: } else {
263: encodeCIIBuiltInAlgorithmDataAsCDATA(ch, start, length);
264: }
265: } catch (IOException e) {
266: throw new SAXException(e);
267: } catch (FastInfosetException e) {
268: throw new SAXException(e);
269: }
270: }
271:
272: public final void ignorableWhitespace(char[] ch, int start,
273: int length) throws SAXException {
274: if (getIgnoreWhiteSpaceTextContent())
275: return;
276:
277: characters(ch, start, length);
278: }
279:
280: public final void processingInstruction(String target, String data)
281: throws SAXException {
282: try {
283: if (getIgnoreProcesingInstructions())
284: return;
285:
286: if (target == "") {
287: throw new SAXException(
288: CommonResourceBundle
289: .getInstance()
290: .getString(
291: "message.processingInstructionTargetIsEmpty"));
292: }
293: encodeTermination();
294:
295: encodeProcessingInstruction(target, data);
296: } catch (IOException e) {
297: throw new SAXException("processingInstruction", e);
298: }
299: }
300:
301: public final void setDocumentLocator(org.xml.sax.Locator locator) {
302: }
303:
304: public final void skippedEntity(String name) throws SAXException {
305: }
306:
307: // LexicalHandler
308:
309: public final void comment(char[] ch, int start, int length)
310: throws SAXException {
311: try {
312: if (getIgnoreComments())
313: return;
314:
315: encodeTermination();
316:
317: encodeComment(ch, start, length);
318: } catch (IOException e) {
319: throw new SAXException("startElement", e);
320: }
321: }
322:
323: public final void startCDATA() throws SAXException {
324: _charactersAsCDATA = true;
325: }
326:
327: public final void endCDATA() throws SAXException {
328: _charactersAsCDATA = false;
329: }
330:
331: public final void startDTD(String name, String publicId,
332: String systemId) throws SAXException {
333: }
334:
335: public final void endDTD() throws SAXException {
336: }
337:
338: public final void startEntity(String name) throws SAXException {
339: }
340:
341: public final void endEntity(String name) throws SAXException {
342: }
343:
344: // EncodingAlgorithmContentHandler
345:
346: public final void octets(String URI, int id, byte[] b, int start,
347: int length) throws SAXException {
348: if (length <= 0) {
349: return;
350: }
351:
352: try {
353: encodeTermination();
354:
355: encodeNonIdentifyingStringOnThirdBit(URI, id, b, start,
356: length);
357: } catch (IOException e) {
358: throw new SAXException(e);
359: } catch (FastInfosetException e) {
360: throw new SAXException(e);
361: }
362: }
363:
364: public final void object(String URI, int id, Object data)
365: throws SAXException {
366: try {
367: encodeTermination();
368:
369: encodeNonIdentifyingStringOnThirdBit(URI, id, data);
370: } catch (IOException e) {
371: throw new SAXException(e);
372: } catch (FastInfosetException e) {
373: throw new SAXException(e);
374: }
375: }
376:
377: // PrimitiveTypeContentHandler
378:
379: public final void bytes(byte[] b, int start, int length)
380: throws SAXException {
381: if (length <= 0) {
382: return;
383: }
384:
385: try {
386: encodeTermination();
387:
388: encodeCIIOctetAlgorithmData(
389: EncodingAlgorithmIndexes.BASE64, b, start, length);
390: } catch (IOException e) {
391: throw new SAXException(e);
392: }
393: }
394:
395: public final void shorts(short[] s, int start, int length)
396: throws SAXException {
397: if (length <= 0) {
398: return;
399: }
400:
401: try {
402: encodeTermination();
403:
404: encodeCIIBuiltInAlgorithmData(
405: EncodingAlgorithmIndexes.SHORT, s, start, length);
406: } catch (IOException e) {
407: throw new SAXException(e);
408: } catch (FastInfosetException e) {
409: throw new SAXException(e);
410: }
411: }
412:
413: public final void ints(int[] i, int start, int length)
414: throws SAXException {
415: if (length <= 0) {
416: return;
417: }
418:
419: try {
420: encodeTermination();
421:
422: encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.INT,
423: i, start, length);
424: } catch (IOException e) {
425: throw new SAXException(e);
426: } catch (FastInfosetException e) {
427: throw new SAXException(e);
428: }
429: }
430:
431: public final void longs(long[] l, int start, int length)
432: throws SAXException {
433: if (length <= 0) {
434: return;
435: }
436:
437: try {
438: encodeTermination();
439:
440: encodeCIIBuiltInAlgorithmData(
441: EncodingAlgorithmIndexes.LONG, l, start, length);
442: } catch (IOException e) {
443: throw new SAXException(e);
444: } catch (FastInfosetException e) {
445: throw new SAXException(e);
446: }
447: }
448:
449: public final void booleans(boolean[] b, int start, int length)
450: throws SAXException {
451: if (length <= 0) {
452: return;
453: }
454:
455: try {
456: encodeTermination();
457:
458: encodeCIIBuiltInAlgorithmData(
459: EncodingAlgorithmIndexes.BOOLEAN, b, start, length);
460: } catch (IOException e) {
461: throw new SAXException(e);
462: } catch (FastInfosetException e) {
463: throw new SAXException(e);
464: }
465: }
466:
467: public final void floats(float[] f, int start, int length)
468: throws SAXException {
469: if (length <= 0) {
470: return;
471: }
472:
473: try {
474: encodeTermination();
475:
476: encodeCIIBuiltInAlgorithmData(
477: EncodingAlgorithmIndexes.FLOAT, f, start, length);
478: } catch (IOException e) {
479: throw new SAXException(e);
480: } catch (FastInfosetException e) {
481: throw new SAXException(e);
482: }
483: }
484:
485: public final void doubles(double[] d, int start, int length)
486: throws SAXException {
487: if (length <= 0) {
488: return;
489: }
490:
491: try {
492: encodeTermination();
493:
494: encodeCIIBuiltInAlgorithmData(
495: EncodingAlgorithmIndexes.DOUBLE, d, start, length);
496: } catch (IOException e) {
497: throw new SAXException(e);
498: } catch (FastInfosetException e) {
499: throw new SAXException(e);
500: }
501: }
502:
503: public void uuids(long[] msblsb, int start, int length)
504: throws SAXException {
505: if (length <= 0) {
506: return;
507: }
508:
509: try {
510: encodeTermination();
511:
512: encodeCIIBuiltInAlgorithmData(
513: EncodingAlgorithmIndexes.UUID, msblsb, start,
514: length);
515: } catch (IOException e) {
516: throw new SAXException(e);
517: } catch (FastInfosetException e) {
518: throw new SAXException(e);
519: }
520: }
521:
522: // RestrictedAlphabetContentHandler
523:
524: public void numericCharacters(char ch[], int start, int length)
525: throws SAXException {
526: if (length <= 0) {
527: return;
528: }
529:
530: try {
531: encodeTermination();
532:
533: encodeFourBitCharacters(
534: RestrictedAlphabet.NUMERIC_CHARACTERS_INDEX,
535: EncodingConstants.NUMERIC_CHARACTERS_TABLE, ch,
536: start, length);
537: } catch (IOException e) {
538: throw new SAXException(e);
539: } catch (FastInfosetException e) {
540: throw new SAXException(e);
541: }
542: }
543:
544: public void dateTimeCharacters(char ch[], int start, int length)
545: throws SAXException {
546: if (length <= 0) {
547: return;
548: }
549:
550: try {
551: encodeTermination();
552:
553: encodeFourBitCharacters(
554: RestrictedAlphabet.DATE_TIME_CHARACTERS_INDEX,
555: EncodingConstants.DATE_TIME_CHARACTERS_TABLE, ch,
556: start, length);
557: } catch (IOException e) {
558: throw new SAXException(e);
559: } catch (FastInfosetException e) {
560: throw new SAXException(e);
561: }
562: }
563:
564: public void alphabetCharacters(String alphabet, char ch[],
565: int start, int length) throws SAXException {
566: if (length <= 0) {
567: return;
568: }
569:
570: try {
571: encodeTermination();
572:
573: encodeAlphabetCharacters(alphabet, ch, start, length);
574: } catch (IOException e) {
575: throw new SAXException(e);
576: } catch (FastInfosetException e) {
577: throw new SAXException(e);
578: }
579: }
580:
581: protected final void encodeElement(String namespaceURI,
582: String qName, String localName) throws IOException {
583: LocalNameQualifiedNamesMap.Entry entry = _v.elementName
584: .obtainEntry(qName);
585: if (entry._valueIndex > 0) {
586: QualifiedName[] names = entry._value;
587: for (int i = 0; i < entry._valueIndex; i++) {
588: if ((namespaceURI == names[i].namespaceName || namespaceURI
589: .equals(names[i].namespaceName))) {
590: encodeNonZeroIntegerOnThirdBit(names[i].index);
591: return;
592: }
593: }
594: }
595:
596: encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI,
597: getPrefixFromQualifiedName(qName), localName, entry);
598: }
599:
600: protected final boolean encodeAttribute(String namespaceURI,
601: String qName, String localName) throws IOException {
602: LocalNameQualifiedNamesMap.Entry entry = _v.attributeName
603: .obtainEntry(qName);
604: if (entry._valueIndex > 0) {
605: QualifiedName[] names = entry._value;
606: for (int i = 0; i < entry._valueIndex; i++) {
607: if ((namespaceURI == names[i].namespaceName || namespaceURI
608: .equals(names[i].namespaceName))) {
609: encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index);
610: return true;
611: }
612: }
613: }
614:
615: return encodeLiteralAttributeQualifiedNameOnSecondBit(
616: namespaceURI, getPrefixFromQualifiedName(qName),
617: localName, entry);
618: }
619: }
|