author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.txw2.output; |
|
27 |
||
28 |
import com.sun.xml.internal.txw2.TxwException; |
|
29 |
||
30 |
import javax.xml.stream.XMLStreamException; |
|
31 |
import javax.xml.stream.XMLStreamWriter; |
|
32 |
||
33 |
/** |
|
34 |
* XML serializer for StAX XMLStreamWriter. |
|
35 |
* |
|
36 |
* TODO: add support for XMLEventWriter (if it makes sense) |
|
37 |
* |
|
38 |
* @author Ryan.Shoemaker@Sun.COM |
|
39 |
*/ |
|
40 |
||
41 |
public class StaxSerializer implements XmlSerializer { |
|
42 |
private final XMLStreamWriter out; |
|
43 |
||
44 |
public StaxSerializer(XMLStreamWriter writer) { |
|
45 |
this(writer,true); |
|
46 |
} |
|
47 |
||
48 |
public StaxSerializer(XMLStreamWriter writer, boolean indenting) { |
|
49 |
if(indenting) |
|
50 |
writer = new IndentingXMLStreamWriter(writer); |
|
51 |
this.out = writer; |
|
52 |
} |
|
53 |
||
54 |
public void startDocument() { |
|
55 |
try { |
|
56 |
out.writeStartDocument(); |
|
57 |
} catch (XMLStreamException e) { |
|
58 |
throw new TxwException(e); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
public void beginStartTag(String uri, String localName, String prefix) { |
|
63 |
try { |
|
64 |
out.writeStartElement(prefix, localName, uri); |
|
65 |
} catch (XMLStreamException e) { |
|
66 |
throw new TxwException(e); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
public void writeAttribute(String uri, String localName, String prefix, StringBuilder value) { |
|
71 |
try { |
|
72 |
out.writeAttribute(prefix, uri, localName, value.toString()); |
|
73 |
} catch (XMLStreamException e) { |
|
74 |
throw new TxwException(e); |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
public void writeXmlns(String prefix, String uri) { |
|
79 |
try { |
|
80 |
if (prefix.length() == 0) { |
|
81 |
out.setDefaultNamespace(uri); |
|
82 |
} else { |
|
83 |
out.setPrefix(prefix, uri); |
|
84 |
} |
|
85 |
||
86 |
// this method handles "", null, and "xmlns" prefixes properly |
|
87 |
out.writeNamespace(prefix, uri); |
|
88 |
} catch (XMLStreamException e) { |
|
89 |
throw new TxwException(e); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
public void endStartTag(String uri, String localName, String prefix) { |
|
94 |
// NO-OP |
|
95 |
} |
|
96 |
||
97 |
public void endTag() { |
|
98 |
try { |
|
99 |
out.writeEndElement(); |
|
100 |
} catch (XMLStreamException e) { |
|
101 |
throw new TxwException(e); |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
public void text(StringBuilder text) { |
|
106 |
try { |
|
107 |
out.writeCharacters(text.toString()); |
|
108 |
} catch (XMLStreamException e) { |
|
109 |
throw new TxwException(e); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
public void cdata(StringBuilder text) { |
|
114 |
try { |
|
115 |
out.writeCData(text.toString()); |
|
116 |
} catch (XMLStreamException e) { |
|
117 |
throw new TxwException(e); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
public void comment(StringBuilder comment) { |
|
122 |
try { |
|
123 |
out.writeComment(comment.toString()); |
|
124 |
} catch (XMLStreamException e) { |
|
125 |
throw new TxwException(e); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
public void endDocument() { |
|
130 |
try { |
|
131 |
out.writeEndDocument(); |
|
132 |
out.flush(); |
|
133 |
} catch (XMLStreamException e) { |
|
134 |
throw new TxwException(e); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
public void flush() { |
|
139 |
try { |
|
140 |
out.flush(); |
|
141 |
} catch (XMLStreamException e) { |
|
142 |
throw new TxwException(e); |
|
143 |
} |
|
144 |
} |
|
145 |
} |