1 29 30 package com.caucho.jsp.java; 31 32 import com.caucho.java.JavaWriter; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.vfs.WriteStream; 35 36 import java.io.IOException ; 37 38 41 public class JspJavaWriter extends JavaWriter { 42 private JavaJspGenerator _gen; 44 45 private String _filename; 46 private int _line = -1; 47 48 private CharBuffer _cb = CharBuffer.allocate(); 50 51 public JspJavaWriter(WriteStream os, JavaJspGenerator gen) 52 { 53 super(os); 54 55 _gen = gen; 56 } 57 58 61 public void addText(String text) 62 throws IOException 63 { 64 if (_filename != null && _cb.length() == 0) { 65 super.setLocation(_filename, _line); 66 } 67 68 _cb.append(text); 69 } 70 71 77 public void setLocation(String filename, int line) 78 throws IOException 79 { 80 _filename = filename; 81 _line = line; 82 } 83 84 87 88 93 protected void flushText() 94 throws IOException 95 { 96 String filename = _filename; 97 int line = _line; 98 _filename = null; 99 100 if (_cb.length() > 0) { 101 int length = _cb.length(); 102 _cb.clear(); 103 generateText(_cb.getBuffer(), 0, length); 104 } 105 106 if (filename != null) 107 super.setLocation(filename, line); 108 } 109 110 118 private void generateText(char []text, int offset, int length) 119 throws IOException 120 { 121 if (length > 32000) { 122 generateText(text, offset, 16 * 1024); 123 generateText(text, offset + 16 * 1024, length - 16 * 1024); 124 return; 125 } 126 127 if (length == 1) { 128 int ch = text[offset]; 129 130 print("out.write('"); 131 switch (ch) { 132 case '\\': 133 print("\\\\"); 134 break; 135 case '\'': 136 print("\\'"); 137 break; 138 case '\n': 139 print("\\n"); 140 break; 141 case '\r': 142 print("\\r"); 143 break; 144 default: 145 print((char) ch); 146 break; 147 } 148 149 println("');"); 150 } 151 else { 152 int index = _gen.addString(new String (text, offset, length)); 153 154 print("out.write(_jsp_string" + index + ", 0, "); 155 println("_jsp_string" + index + ".length);"); 156 } 157 } 158 159 162 public void printJavaString(String s) 163 throws IOException 164 { 165 flushText(); 166 167 super.printJavaString(s); 168 } 169 170 173 public void pushDepth() 174 throws IOException 175 { 176 flushText(); 177 178 super.pushDepth(); 179 } 180 181 184 public void popDepth() 185 throws IOException 186 { 187 flushText(); 188 189 super.popDepth(); 190 } 191 192 195 public void print(String s) 196 throws IOException 197 { 198 flushText(); 199 200 super.print(s); 201 } 202 203 206 public void print(char ch) 207 throws IOException 208 { 209 flushText(); 210 211 super.print(ch); 212 } 213 214 217 public void print(boolean b) 218 throws IOException 219 { 220 flushText(); 221 222 super.print(b); 223 } 224 225 228 public void print(int i) 229 throws IOException 230 { 231 flushText(); 232 233 super.print(i); 234 } 235 236 239 public void print(long l) 240 throws IOException 241 { 242 flushText(); 243 244 super.print(l); 245 } 246 247 250 public void print(Object o) 251 throws IOException 252 { 253 flushText(); 254 255 super.print(o); 256 } 257 258 261 public void println(String s) 262 throws IOException 263 { 264 flushText(); 265 266 super.println(s); 267 } 268 269 272 public void println(boolean v) 273 throws IOException 274 { 275 flushText(); 276 277 super.println(v); 278 } 279 280 283 public void println(char ch) 284 throws IOException 285 { 286 flushText(); 287 288 super.println(ch); 289 } 290 291 294 public void println(int v) 295 throws IOException 296 { 297 flushText(); 298 299 super.println(v); 300 } 301 302 305 public void println(long v) 306 throws IOException 307 { 308 flushText(); 309 310 super.println(v); 311 } 312 313 316 public void println(Object v) 317 throws IOException 318 { 319 flushText(); 320 321 super.println(v); 322 } 323 324 327 public void println() 328 throws IOException 329 { 330 flushText(); 331 332 super.println(); 333 } 334 } 335 | Popular Tags |