1 package org.codehaus.groovy.sandbox.markup; 2 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; private final Writer bodyWriter = new Writer () { 54 57 public void close() throws IOException { 58 StreamingMarkupWriter.this.close(); 59 } 60 61 64 public void flush() throws IOException { 65 StreamingMarkupWriter.this.flush(); 66 } 67 68 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 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 113 public void close() throws IOException { 114 StreamingMarkupWriter.this.close(); 115 } 116 117 120 public void flush() throws IOException { 121 StreamingMarkupWriter.this.flush(); 122 } 123 124 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 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 164 public void close() throws IOException { 165 this.delegate.close(); 166 } 167 168 171 public void flush() throws IOException { 172 this.delegate.flush(); 173 } 174 175 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 } 194 | Popular Tags |