|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StreamingMarkupWriter.java | 0% | 0% | 0% | 0% |
|
1 |
package org.codehaus.groovy.sandbox.markup;
|
|
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.Writer;
|
|
49 |
|
|
50 |
public class StreamingMarkupWriter extends Writer { |
|
51 |
protected final Writer delegate;
|
|
52 |
private final int encodingLimit = 127; // initally encode everything that's not US ASCII |
|
53 |
private final Writer bodyWriter = new Writer() { |
|
54 |
/* (non-Javadoc)
|
|
55 |
* @see java.io.Writer#close()
|
|
56 |
*/
|
|
57 | 0 |
public void close() throws IOException { |
58 | 0 |
StreamingMarkupWriter.this.close();
|
59 |
} |
|
60 |
|
|
61 |
/* (non-Javadoc)
|
|
62 |
* @see java.io.Writer#flush()
|
|
63 |
*/
|
|
64 | 0 |
public void flush() throws IOException { |
65 | 0 |
StreamingMarkupWriter.this.flush();
|
66 |
} |
|
67 |
|
|
68 |
/* (non-Javadoc)
|
|
69 |
* @see java.io.Writer#write(int)
|
|
70 |
*/
|
|
71 | 0 |
public void write(int c) throws IOException { |
72 | 0 |
if (c > StreamingMarkupWriter.this.encodingLimit) { |
73 | 0 |
StreamingMarkupWriter.this.delegate.write("&#x"); |
74 | 0 |
StreamingMarkupWriter.this.delegate.write(Integer.toHexString(c));
|
75 | 0 |
StreamingMarkupWriter.this.delegate.write(';');
|
76 | 0 |
} else if (c == '<') { |
77 | 0 |
StreamingMarkupWriter.this.delegate.write("<"); |
78 | 0 |
} else if (c == '>') { |
79 | 0 |
StreamingMarkupWriter.this.delegate.write(">"); |
80 | 0 |
} else if (c == '&') { |
81 | 0 |
StreamingMarkupWriter.this.delegate.write("&"); |
82 |
} else {
|
|
83 | 0 |
StreamingMarkupWriter.this.delegate.write(c);
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
/* (non-Javadoc)
|
|
88 |
* @see java.io.Writer#write(char[], int, int)
|
|
89 |
*/
|
|
90 | 0 |
public void write(final char[] cbuf, int off, int len) throws IOException { |
91 | 0 |
while (len-- > 0){
|
92 | 0 |
write(cbuf[off++]); |
93 |
} |
|
94 |
} |
|
95 |
|
|
96 | 0 |
public Writer attributeValue() {
|
97 | 0 |
return StreamingMarkupWriter.this.attributeWriter; |
98 |
} |
|
99 |
|
|
100 | 0 |
public Writer bodyText() {
|
101 | 0 |
return bodyWriter;
|
102 |
} |
|
103 |
|
|
104 | 0 |
public Writer unescaped() {
|
105 | 0 |
return StreamingMarkupWriter.this; |
106 |
} |
|
107 |
}; |
|
108 |
|
|
109 |
private final Writer attributeWriter = new Writer() { |
|
110 |
/* (non-Javadoc)
|
|
111 |
* @see java.io.Writer#close()
|
|
112 |
*/
|
|
113 | 0 |
public void close() throws IOException { |
114 | 0 |
StreamingMarkupWriter.this.close();
|
115 |
} |
|
116 |
|
|
117 |
/* (non-Javadoc)
|
|
118 |
* @see java.io.Writer#flush()
|
|
119 |
*/
|
|
120 | 0 |
public void flush() throws IOException { |
121 | 0 |
StreamingMarkupWriter.this.flush();
|
122 |
} |
|
123 |
|
|
124 |
/* (non-Javadoc)
|
|
125 |
* @see java.io.Writer#write(int)
|
|
126 |
*/
|
|
127 | 0 |
public void write(int c) throws IOException { |
128 | 0 |
if (c == '\'') {
|
129 | 0 |
StreamingMarkupWriter.this.delegate.write("'"); |
130 |
} else {
|
|
131 | 0 |
StreamingMarkupWriter.this.bodyWriter.write(c);
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
/* (non-Javadoc)
|
|
136 |
* @see java.io.Writer#write(char[], int, int)
|
|
137 |
*/
|
|
138 | 0 |
public void write(final char[] cbuf, int off, int len) throws IOException { |
139 | 0 |
while (len-- > 0){
|
140 | 0 |
write(cbuf[off++]); |
141 |
} |
|
142 |
} |
|
143 |
|
|
144 | 0 |
public Writer attributeValue() {
|
145 | 0 |
return attributeWriter;
|
146 |
} |
|
147 |
|
|
148 | 0 |
public Writer bodyText() {
|
149 | 0 |
return StreamingMarkupWriter.this.bodyWriter; |
150 |
} |
|
151 |
|
|
152 | 0 |
public Writer unescaped() {
|
153 | 0 |
return StreamingMarkupWriter.this; |
154 |
} |
|
155 |
}; |
|
156 |
|
|
157 | 0 |
public StreamingMarkupWriter(final Writer delegate) {
|
158 | 0 |
this.delegate = delegate;
|
159 |
} |
|
160 |
|
|
161 |
/* (non-Javadoc)
|
|
162 |
* @see java.io.Writer#close()
|
|
163 |
*/
|
|
164 | 0 |
public void close() throws IOException { |
165 | 0 |
this.delegate.close();
|
166 |
} |
|
167 |
|
|
168 |
/* (non-Javadoc)
|
|
169 |
* @see java.io.Writer#flush()
|
|
170 |
*/
|
|
171 | 0 |
public void flush() throws IOException { |
172 | 0 |
this.delegate.flush();
|
173 |
} |
|
174 |
|
|
175 |
/* (non-Javadoc)
|
|
176 |
* @see java.io.Writer#write(char[], int, int)
|
|
177 |
*/
|
|
178 | 0 |
public void write(final char[] cbuf, int off, int len) throws IOException { |
179 | 0 |
this.delegate.write(cbuf, off, len);
|
180 |
} |
|
181 |
|
|
182 | 0 |
public Writer attributeValue() {
|
183 | 0 |
return this.attributeWriter; |
184 |
} |
|
185 |
|
|
186 | 0 |
public Writer bodyText() {
|
187 | 0 |
return this.bodyWriter; |
188 |
} |
|
189 |
|
|
190 | 0 |
public Writer unescaped() {
|
191 | 0 |
return this; |
192 |
} |
|
193 |
} |
|
194 |
|
|