View Javadoc

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  											public void close() throws IOException {
58  												StreamingMarkupWriter.this.close();
59  											}
60  											
61  											/* (non-Javadoc)
62  											 * @see java.io.Writer#flush()
63  											 */
64  											public void flush() throws IOException {
65  												StreamingMarkupWriter.this.flush();
66  											}
67  		
68  											/* (non-Javadoc)
69  											 * @see java.io.Writer#write(int)
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("&lt;");
78  												} else if (c == '>') {
79  													StreamingMarkupWriter.this.delegate.write("&gt;");
80  												} else if (c == '&') {
81  													StreamingMarkupWriter.this.delegate.write("&amp;");
82  												} else {
83  													StreamingMarkupWriter.this.delegate.write(c);
84  												}
85  											}
86  											
87  											/* (non-Javadoc)
88  											 * @see java.io.Writer#write(char[], int, int)
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 												/* (non-Javadoc)
111 												 * @see java.io.Writer#close()
112 												 */
113 												public void close() throws IOException {
114 													StreamingMarkupWriter.this.close();
115 												}
116 										
117 												/* (non-Javadoc)
118 												 * @see java.io.Writer#flush()
119 												 */
120 												public void flush() throws IOException {
121 													StreamingMarkupWriter.this.flush();
122 												}
123 		
124 												/* (non-Javadoc)
125 												 * @see java.io.Writer#write(int)
126 												 */
127 												public void write(int c) throws IOException {
128 													if (c == '\'') {
129 														StreamingMarkupWriter.this.delegate.write("&apos;");
130 													} else {
131 														StreamingMarkupWriter.this.bodyWriter.write(c);
132 													}
133 												}
134 												
135 												/* (non-Javadoc)
136 												 * @see java.io.Writer#write(char[], int, int)
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 		/* (non-Javadoc)
162 		 * @see java.io.Writer#close()
163 		 */
164 		public void close() throws IOException {
165 			this.delegate.close();
166 		}
167 
168 		/* (non-Javadoc)
169 		 * @see java.io.Writer#flush()
170 		 */
171 		public void flush() throws IOException {
172 			this.delegate.flush();
173 		}
174 		
175 		/* (non-Javadoc)
176 		 * @see java.io.Writer#write(char[], int, int)
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 	}