| 1 16 17 22 23 package org.ditchnet.jsp.util; 24 25 import java.io.Writer ; 26 import java.io.StringWriter ; 27 import org.ditchnet.xml.Xml; 28 29 41 public final class JspResponseWriter { 42 43 private static final String LT = "<"; 44 private static final String GT = ">"; 45 private static final String COLON = ":"; 46 private static final String SPACE = " "; 47 private static final String EQUALS = "="; 48 private static final String TAB_STOP = "\t"; 49 private static final String LINE_BREAK = "\n"; 50 private static final String SINGLE_QUOTE = "'"; 51 private static final String DOUBLE_QUOTE = "\""; 52 private static final String OPEN_COMMENT = "<!-- "; 53 private static final String CLOSE_COMMENT = " -->"; 54 private static final String FORWARD_SLASH = "/"; 55 56 private StringWriter out = new StringWriter (); 57 58 private boolean prettyPrinting = true; 59 60 private int tagDepth; 61 private volatile boolean elementTagIsOpen; 62 private volatile boolean elementIsEmpty; 63 64 68 public JspResponseWriter() { 69 this(true); 70 } 71 72 75 public JspResponseWriter(final boolean prettyPrinting) { 76 setPrettyPrinting(prettyPrinting); 77 } 78 79 82 public StringBuffer getBuffer() { 83 return out.getBuffer(); 84 } 85 86 public Writer getWriter() { 87 return out; 88 } 89 90 94 public void setPrettyPrinting(final boolean prettyPrinting) { 95 this.prettyPrinting = prettyPrinting; 96 } 97 98 101 public boolean isPrettyPrinting() { 102 return prettyPrinting; 103 } 104 105 private void prettyPrint() { 106 if (!isPrettyPrinting()) { 107 return; 108 } 109 lineBreak(); 110 for (int i = 0; i < tagDepth; i++) { 111 tabStop(); 112 } 113 tagDepth++; 114 } 115 116 public void tabStop() { 117 closeOpenTagIfNecessary(); 118 out.write(TAB_STOP); 119 elementIsEmpty = false; 120 } 121 122 133 public void startElement(Xml.Tag qName) { 134 _startElement(qName.toString()); 135 } 136 137 140 public void startElement(String prefix, Xml.Tag localName) { 141 _startElement(prefix + COLON + localName); 142 } 143 144 private void _startElement(final String name) { 145 closeOpenTagIfNecessary(); 146 prettyPrint(); 147 out.write(LT); 148 out.write(name); 149 elementTagIsOpen = true; 150 elementIsEmpty = true; 151 } 152 153 156 public void endElement(Xml.Tag qName) { 157 _endElement(qName.toString()); 158 } 159 160 163 public void endElement(String prefix, Xml.Tag localName) { 164 _endElement(prefix + COLON + localName); 165 } 166 167 private void _endElement(final String name) { 168 if (elementIsEmpty) { 169 out.write(SPACE); 170 out.write(FORWARD_SLASH); 171 out.write(GT); 172 elementIsEmpty = false; 173 } else { 174 if (isPrettyPrinting()) { 175 lineBreak(); 176 } 177 out.write(LT); 178 out.write(FORWARD_SLASH); 179 out.write(name); 180 out.write(GT); 181 } 182 elementTagIsOpen = false; 183 tagDepth--; 184 } 185 186 190 public void lineBreak() { 191 closeOpenTagIfNecessary(); 192 out.write(LINE_BREAK); 193 elementIsEmpty = false; 194 } 195 196 206 public void attribute(Xml.Attr qName, String value) { 207 _attribute(qName.toString(),value); 208 } 209 210 220 public void attribute(String prefix, Xml.Attr localName, String value) { 221 _attribute(prefix + localName.toString(), value); 222 } 223 224 private void _attribute(String qName, String value) { 225 if (!elementTagIsOpen) { 226 throw new IllegalStateException ("Attempting to write attribute " + 227 " with no open element tag"); 228 } 229 if (null == qName) { 230 throw new NullPointerException ("Attempting to write attribute " + 231 " with no name"); 232 } 233 out.write(SPACE); 234 out.write(qName); 235 out.write(EQUALS); 236 out.write(DOUBLE_QUOTE); 237 out.write(value); 238 out.write(DOUBLE_QUOTE); 239 } 240 241 253 public void text(Object text) { 254 if (null == text) { 255 throw new NullPointerException ("Attempting to write null text"); 256 } 257 closeOpenTagIfNecessary(); 258 out.write(text.toString()); 259 elementIsEmpty = false; 260 } 261 262 274 public void comment(Object comment) { 275 if (null == comment) { 276 throw new NullPointerException ("Attempting to write null comment"); 277 } 278 closeOpenTagIfNecessary(); 279 out.write(OPEN_COMMENT); 280 out.write(comment.toString()); 281 out.write(CLOSE_COMMENT); 282 } 283 284 private void closeOpenTagIfNecessary() { 285 if (elementTagIsOpen) { 286 out.write(GT); 287 elementTagIsOpen = false; 288 } 289 } 290 291 } 292 293 | Popular Tags |