1 29 30 package com.caucho.java; 31 32 import com.caucho.bytecode.JClass; 33 import com.caucho.vfs.Path; 34 import com.caucho.vfs.WriteStream; 35 36 import java.io.IOException ; 37 import java.io.Writer ; 38 39 42 public class JavaWriter extends Writer { 43 private WriteStream _os; 45 46 private int _indentDepth; 48 49 private boolean _startLine = true; 51 52 private LineMap _lineMap; 54 55 private int _destLine = 1; 57 private boolean _lastCr; 58 59 private int _uniqueId; 61 62 public JavaWriter(WriteStream os) 63 { 64 _os = os; 65 } 66 67 70 public WriteStream getWriteStream() 71 { 72 return _os; 73 } 74 75 78 public int getDestLine() 79 { 80 return _destLine; 81 } 82 83 86 public void setLineMap(LineMap lineMap) 87 { 88 _lineMap = lineMap; 89 } 90 91 94 public LineMap getLineMap() 95 { 96 return _lineMap; 97 } 98 99 105 public void setLocation(String filename, int line) 106 throws IOException 107 { 108 if (_lineMap != null && filename != null && line >= 0) { 109 _lineMap.add(filename, line, _destLine); 110 } 111 } 112 113 116 public int generateId() 117 { 118 return _uniqueId++; 119 } 120 121 124 public void printJavaString(String s) 125 throws IOException 126 { 127 for (int i = 0; i < s.length(); i++) { 128 char ch = s.charAt(i); 129 130 switch (ch) { 131 case '\\': 132 _os.print("\\\\"); 133 break; 134 case '\n': 135 _os.print("\\n"); 136 break; 137 case '\r': 138 _os.print("\\r"); 139 break; 140 case '"': 141 _os.print("\\\""); 142 break; 143 default: 144 _os.print(ch); 145 } 146 } 147 } 148 149 152 public void printJavaChar(char ch) 153 throws IOException 154 { 155 switch (ch) { 156 case '\\': 157 _os.print("\\\\"); 158 break; 159 case '\n': 160 _os.print("\\n"); 161 break; 162 case '\r': 163 _os.print("\\r"); 164 break; 165 case '\'': 166 _os.print("\\'"); 167 break; 168 default: 169 _os.print(ch); 170 } 171 } 172 173 176 public void pushDepth() 177 throws IOException 178 { 179 _indentDepth += 2; 180 } 181 182 185 public void popDepth() 186 throws IOException 187 { 188 _indentDepth -= 2; 189 } 190 191 194 public void print(String s) 195 throws IOException 196 { 197 if (_startLine) 198 printIndent(); 199 200 if (s == null) { 201 _lastCr = false; 202 _os.print("null"); 203 204 return; 205 } 206 207 int len = s.length(); 208 for (int i = 0; i < len; i++) { 209 int ch = s.charAt(i); 210 211 if (ch == '\n' && ! _lastCr) 212 _destLine++; 213 else if (ch == '\r') 214 _destLine++; 215 216 _lastCr = ch == '\r'; 217 218 _os.print((char) ch); 219 } 220 } 221 222 public void write(char []buffer, int offset, int length) 223 throws IOException 224 { 225 print(new String (buffer, offset, length)); 226 } 227 228 231 public void print(char ch) 232 throws IOException 233 { 234 if (_startLine) 235 printIndent(); 236 237 if (ch == '\r') { 238 _destLine++; 239 } 240 else if (ch == '\n' && ! _lastCr) 241 _destLine++; 242 243 _lastCr = ch == '\r'; 244 245 _os.print(ch); 246 } 247 248 251 public void print(boolean b) 252 throws IOException 253 { 254 if (_startLine) 255 printIndent(); 256 257 _os.print(b); 258 _lastCr = false; 259 } 260 261 264 public void print(int i) 265 throws IOException 266 { 267 if (_startLine) 268 printIndent(); 269 270 _os.print(i); 271 _lastCr = false; 272 } 273 274 277 public void print(long l) 278 throws IOException 279 { 280 if (_startLine) 281 printIndent(); 282 283 _os.print(l); 284 _lastCr = false; 285 } 286 287 290 public void print(Object o) 291 throws IOException 292 { 293 if (_startLine) 294 printIndent(); 295 296 _os.print(o); 297 _lastCr = false; 298 } 299 300 303 public void println(String s) 304 throws IOException 305 { 306 print(s); 307 println(); 308 } 309 310 313 public void println(boolean v) 314 throws IOException 315 { 316 print(v); 317 println(); 318 } 319 320 323 public void println(char ch) 324 throws IOException 325 { 326 print(ch); 327 println(); 328 } 329 330 333 public void println(int v) 334 throws IOException 335 { 336 print(v); 337 println(); 338 } 339 340 343 public void println(long v) 344 throws IOException 345 { 346 print(v); 347 println(); 348 } 349 350 353 public void println(Object v) 354 throws IOException 355 { 356 print(v); 357 println(); 358 } 359 360 363 public void println() 364 throws IOException 365 { 366 _os.println(); 367 if (! _lastCr) 368 _destLine++; 369 _lastCr = false; 370 _startLine = true; 371 } 372 373 376 public void printClass(Class cl) 377 throws IOException 378 { 379 if (! cl.isArray()) 380 print(cl.getName().replace('$', '.')); 381 else { 382 printClass(cl.getComponentType()); 383 print("[]"); 384 } 385 } 386 387 393 public void printJavaTypeToObject(String value, Class javaType) 394 throws IOException 395 { 396 if (Object .class.isAssignableFrom(javaType)) 397 print(value); 398 else if (javaType.equals(boolean.class)) 399 print("new Boolean(" + value + ")"); 400 else if (javaType.equals(byte.class)) 401 print("new Byte(" + value + ")"); 402 else if (javaType.equals(short.class)) 403 print("new Short(" + value + ")"); 404 else if (javaType.equals(int.class)) 405 print("new Integer(" + value + ")"); 406 else if (javaType.equals(long.class)) 407 print("new Long(" + value + ")"); 408 else if (javaType.equals(char.class)) 409 print("String.valueOf(" + value + ")"); 410 else if (javaType.equals(float.class)) 411 print("new Float(" + value + ")"); 412 else if (javaType.equals(double.class)) 413 print("new Double(" + value + ")"); 414 else 415 print(value); 416 } 417 418 424 public void printJavaTypeToObject(String value, JClass javaType) 425 throws IOException 426 { 427 if (javaType.getName().equals("boolean")) 428 print("new Boolean(" + value + ")"); 429 else if (javaType.getName().equals("byte")) 430 print("new Byte(" + value + ")"); 431 else if (javaType.getName().equals("short")) 432 print("new Short(" + value + ")"); 433 else if (javaType.getName().equals("int")) 434 print("new Integer(" + value + ")"); 435 else if (javaType.getName().equals("long")) 436 print("new Long(" + value + ")"); 437 else if (javaType.getName().equals("char")) 438 print("String.valueOf(" + value + ")"); 439 else if (javaType.getName().equals("float")) 440 print("new Float(" + value + ")"); 441 else if (javaType.getName().equals("double")) 442 print("new Double(" + value + ")"); 443 else 444 print(value); 445 } 446 447 450 public void printIndent() 451 throws IOException 452 { 453 _startLine = false; 454 455 for (int i = 0; i < _indentDepth; i++) 456 _os.print(' '); 457 458 _lastCr = false; 459 } 460 461 464 public void generateSmap() 465 throws IOException 466 { 467 if (_lineMap != null) { 468 Path dstPath = getWriteStream().getPath(); 469 Path smap = dstPath.getParent().lookup(dstPath.getTail() + ".smap"); 470 471 WriteStream out = smap.openWrite(); 472 try { 473 String srcName = _lineMap.getLastSourceFilename(); 474 475 LineMapWriter writer = new LineMapWriter(out); 476 477 if (_lineMap.getSourceType() != null) 478 writer.setSourceType(_lineMap.getSourceType()); 479 480 writer.write(_lineMap); 481 } finally { 482 out.close(); 483 } 484 } 485 486 } 487 488 491 public String errorMessage(String message) 492 { 493 499 return message; 500 } 501 502 public void flush() 503 { 504 } 505 506 public void close() 507 { 508 } 509 } 510 | Popular Tags |