001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.xml.txw2.output;
022:
023: import com.sun.xml.txw2.TxwException;
024:
025: import javax.xml.stream.XMLStreamException;
026: import javax.xml.stream.XMLStreamWriter;
027:
028: /**
029: * XML serializer for StAX XMLStreamWriter.
030: *
031: * TODO: add support for XMLEventWriter (if it makes sense)
032: *
033: * @author Ryan.Shoemaker@Sun.COM
034: */
035:
036: public class StaxSerializer implements XmlSerializer {
037: private final XMLStreamWriter out;
038:
039: public StaxSerializer(XMLStreamWriter writer) {
040: this (writer, true);
041: }
042:
043: public StaxSerializer(XMLStreamWriter writer, boolean indenting) {
044: if (indenting)
045: writer = new IndentingXMLStreamWriter(writer);
046: this .out = writer;
047: }
048:
049: public void startDocument() {
050: try {
051: out.writeStartDocument();
052: } catch (XMLStreamException e) {
053: throw new TxwException(e);
054: }
055: }
056:
057: public void beginStartTag(String uri, String localName,
058: String prefix) {
059: try {
060: out.writeStartElement(prefix, localName, uri);
061: } catch (XMLStreamException e) {
062: throw new TxwException(e);
063: }
064: }
065:
066: public void writeAttribute(String uri, String localName,
067: String prefix, StringBuilder value) {
068: try {
069: out
070: .writeAttribute(prefix, uri, localName, value
071: .toString());
072: } catch (XMLStreamException e) {
073: throw new TxwException(e);
074: }
075: }
076:
077: public void writeXmlns(String prefix, String uri) {
078: try {
079: if (prefix.length() == 0) {
080: out.setDefaultNamespace(uri);
081: } else {
082: out.setPrefix(prefix, uri);
083: }
084:
085: // this method handles "", null, and "xmlns" prefixes properly
086: out.writeNamespace(prefix, uri);
087: } catch (XMLStreamException e) {
088: throw new TxwException(e);
089: }
090: }
091:
092: public void endStartTag(String uri, String localName, String prefix) {
093: // NO-OP
094: }
095:
096: public void endTag() {
097: try {
098: out.writeEndElement();
099: } catch (XMLStreamException e) {
100: throw new TxwException(e);
101: }
102: }
103:
104: public void text(StringBuilder text) {
105: try {
106: out.writeCharacters(text.toString());
107: } catch (XMLStreamException e) {
108: throw new TxwException(e);
109: }
110: }
111:
112: public void cdata(StringBuilder text) {
113: try {
114: out.writeCData(text.toString());
115: } catch (XMLStreamException e) {
116: throw new TxwException(e);
117: }
118: }
119:
120: public void comment(StringBuilder comment) {
121: try {
122: out.writeComment(comment.toString());
123: } catch (XMLStreamException e) {
124: throw new TxwException(e);
125: }
126: }
127:
128: public void endDocument() {
129: try {
130: out.writeEndDocument();
131: out.flush();
132: } catch (XMLStreamException e) {
133: throw new TxwException(e);
134: }
135: }
136:
137: public void flush() {
138: try {
139: out.flush();
140: } catch (XMLStreamException e) {
141: throw new TxwException(e);
142: }
143: }
144: }
|