1 package groovy.xml.streamingmarkupsupport;
2 /*
3
4 Copyright 2004 (C) John Wilson. All Rights Reserved.
5
6 Redistribution and use of this software and associated documentation
7 ("Software"), with or without modification, are permitted provided
8 that the following conditions are met:
9
10 1. Redistributions of source code must retain copyright
11 statements and notices. Redistributions must also contain a
12 copy of this document.
13
14 2. Redistributions in binary form must reproduce the
15 above copyright notice, this list of conditions and the
16 following disclaimer in the documentation and/or other
17 materials provided with the distribution.
18
19 3. The name "groovy" must not be used to endorse or promote
20 products derived from this Software without prior written
21 permission of The Codehaus. For written permission,
22 please contact info@codehaus.org.
23
24 4. Products derived from this Software may not be called "groovy"
25 nor may "groovy" appear in their names without prior written
26 permission of The Codehaus. "groovy" is a registered
27 trademark of The Codehaus.
28
29 5. Due credit should be given to The Codehaus -
30 http://groovy.codehaus.org/
31
32 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43 OF THE POSSIBILITY OF SUCH DAMAGE.
44
45 */
46
47 import java.io.IOException;
48 import java.io.OutputStreamWriter;
49 import java.io.Writer;
50 import java.nio.charset.Charset;
51 import java.nio.charset.CharsetEncoder;
52
53 public class StreamingMarkupWriter extends Writer {
54 protected final Writer writer;
55 protected final String encoding;
56 protected final CharsetEncoder encoder;
57 private final Writer bodyWriter = new Writer() {
58 /* (non-Javadoc)
59 * @see java.io.Writer#close()
60 */
61 public void close() throws IOException {
62 StreamingMarkupWriter.this.close();
63 }
64
65 /* (non-Javadoc)
66 * @see java.io.Writer#flush()
67 */
68 public void flush() throws IOException {
69 StreamingMarkupWriter.this.flush();
70 }
71
72 /* (non-Javadoc)
73 * @see java.io.Writer#write(int)
74 */
75 public void write(final int c) throws IOException {
76 if (!StreamingMarkupWriter.this.encoder.canEncode((char)c)) {
77 StreamingMarkupWriter.this.writer.write("&#x");
78 StreamingMarkupWriter.this.writer.write(Integer.toHexString(c));
79 StreamingMarkupWriter.this.writer.write(';');
80 } else if (c == '<') {
81 StreamingMarkupWriter.this.writer.write("<");
82 } else if (c == '>') {
83 StreamingMarkupWriter.this.writer.write(">");
84 } else if (c == '&') {
85 StreamingMarkupWriter.this.writer.write("&");
86 } else {
87 StreamingMarkupWriter.this.writer.write(c);
88 }
89 }
90
91 /* (non-Javadoc)
92 * @see java.io.Writer#write(char[], int, int)
93 */
94 public void write(final char[] cbuf, int off, int len) throws IOException {
95 while (len-- > 0){
96 write(cbuf[off++]);
97 }
98 }
99
100 public Writer attributeValue() {
101 return StreamingMarkupWriter.this.attributeWriter;
102 }
103
104 public Writer bodyText() {
105 return bodyWriter;
106 }
107
108 public Writer unescaped() {
109 return StreamingMarkupWriter.this;
110 }
111 };
112
113 private final Writer attributeWriter = new Writer() {
114 /* (non-Javadoc)
115 * @see java.io.Writer#close()
116 */
117 public void close() throws IOException {
118 StreamingMarkupWriter.this.close();
119 }
120
121 /* (non-Javadoc)
122 * @see java.io.Writer#flush()
123 */
124 public void flush() throws IOException {
125 StreamingMarkupWriter.this.flush();
126 }
127
128 /* (non-Javadoc)
129 * @see java.io.Writer#write(int)
130 */
131 public void write(final int c) throws IOException {
132 if (c == '\'') {
133 StreamingMarkupWriter.this.writer.write("'");
134 } else {
135 StreamingMarkupWriter.this.bodyWriter.write(c);
136 }
137 }
138
139 /* (non-Javadoc)
140 * @see java.io.Writer#write(char[], int, int)
141 */
142 public void write(final char[] cbuf, int off, int len) throws IOException {
143 while (len-- > 0){
144 write(cbuf[off++]);
145 }
146 }
147
148 public Writer attributeValue() {
149 return attributeWriter;
150 }
151
152 public Writer bodyText() {
153 return StreamingMarkupWriter.this.bodyWriter;
154 }
155
156 public Writer unescaped() {
157 return StreamingMarkupWriter.this;
158 }
159 };
160
161 public StreamingMarkupWriter(final Writer writer, final String encoding) {
162 this.writer = writer;
163
164 if (encoding != null) {
165 this.encoding = encoding;
166 } else if (writer instanceof OutputStreamWriter) {
167 this.encoding = ((OutputStreamWriter)writer).getEncoding();
168 } else {
169 this.encoding = "US-ASCII";
170 }
171
172 this.encoder = Charset.forName(this.encoding).newEncoder();
173 }
174
175 public StreamingMarkupWriter(final Writer writer) {
176 this(writer, null);
177 }
178
179 /* (non-Javadoc)
180 * @see java.io.Writer#close()
181 */
182 public void close() throws IOException {
183 this.writer.close();
184 }
185
186 /* (non-Javadoc)
187 * @see java.io.Writer#flush()
188 */
189 public void flush() throws IOException {
190 this.writer.flush();
191 }
192
193 /* (non-Javadoc)
194 * @see java.io.Writer#write(char[], int, int)
195 */
196 public void write(final char[] cbuf, int off, int len) throws IOException {
197 this.writer.write(cbuf, off, len);
198 }
199
200 public Writer attributeValue() {
201 return this.attributeWriter;
202 }
203
204 public Writer bodyText() {
205 return this.bodyWriter;
206 }
207
208 public Writer unescaped() {
209 return this;
210 }
211 }