1 package org.codehaus.groovy.sandbox.markup;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
53 private final Writer bodyWriter = new Writer() {
54
55
56
57 public void close() throws IOException {
58 StreamingMarkupWriter.this.close();
59 }
60
61
62
63
64 public void flush() throws IOException {
65 StreamingMarkupWriter.this.flush();
66 }
67
68
69
70
71 public void write(int c) throws IOException {
72 if (c > StreamingMarkupWriter.this.encodingLimit) {
73 StreamingMarkupWriter.this.delegate.write("&#x");
74 StreamingMarkupWriter.this.delegate.write(Integer.toHexString(c));
75 StreamingMarkupWriter.this.delegate.write(';');
76 } else if (c == '<') {
77 StreamingMarkupWriter.this.delegate.write("<");
78 } else if (c == '>') {
79 StreamingMarkupWriter.this.delegate.write(">");
80 } else if (c == '&') {
81 StreamingMarkupWriter.this.delegate.write("&");
82 } else {
83 StreamingMarkupWriter.this.delegate.write(c);
84 }
85 }
86
87
88
89
90 public void write(final char[] cbuf, int off, int len) throws IOException {
91 while (len-- > 0){
92 write(cbuf[off++]);
93 }
94 }
95
96 public Writer attributeValue() {
97 return StreamingMarkupWriter.this.attributeWriter;
98 }
99
100 public Writer bodyText() {
101 return bodyWriter;
102 }
103
104 public Writer unescaped() {
105 return StreamingMarkupWriter.this;
106 }
107 };
108
109 private final Writer attributeWriter = new Writer() {
110
111
112
113 public void close() throws IOException {
114 StreamingMarkupWriter.this.close();
115 }
116
117
118
119
120 public void flush() throws IOException {
121 StreamingMarkupWriter.this.flush();
122 }
123
124
125
126
127 public void write(int c) throws IOException {
128 if (c == '\'') {
129 StreamingMarkupWriter.this.delegate.write("'");
130 } else {
131 StreamingMarkupWriter.this.bodyWriter.write(c);
132 }
133 }
134
135
136
137
138 public void write(final char[] cbuf, int off, int len) throws IOException {
139 while (len-- > 0){
140 write(cbuf[off++]);
141 }
142 }
143
144 public Writer attributeValue() {
145 return attributeWriter;
146 }
147
148 public Writer bodyText() {
149 return StreamingMarkupWriter.this.bodyWriter;
150 }
151
152 public Writer unescaped() {
153 return StreamingMarkupWriter.this;
154 }
155 };
156
157 public StreamingMarkupWriter(final Writer delegate) {
158 this.delegate = delegate;
159 }
160
161
162
163
164 public void close() throws IOException {
165 this.delegate.close();
166 }
167
168
169
170
171 public void flush() throws IOException {
172 this.delegate.flush();
173 }
174
175
176
177
178 public void write(final char[] cbuf, int off, int len) throws IOException {
179 this.delegate.write(cbuf, off, len);
180 }
181
182 public Writer attributeValue() {
183 return this.attributeWriter;
184 }
185
186 public Writer bodyText() {
187 return this.bodyWriter;
188 }
189
190 public Writer unescaped() {
191 return this;
192 }
193 }