KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > JavaWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
37 import java.io.Writer JavaDoc;
38
39 /**
40  * Writing class for generated Java code.
41  */

42 public class JavaWriter extends Writer JavaDoc {
43   // Write stream for generating the code
44
private WriteStream _os;
45   
46   // Indentation depth
47
private int _indentDepth;
48   
49   // True at the start of a line
50
private boolean _startLine = true;
51
52   // The line mapping
53
private LineMap _lineMap;
54
55   // The current output line
56
private int _destLine = 1;
57   private boolean _lastCr;
58
59   // Generates a unique string.
60
private int _uniqueId;
61
62   public JavaWriter(WriteStream os)
63   {
64     _os = os;
65   }
66
67   /**
68    * Returns the underlying stream.
69    */

70   public WriteStream getWriteStream()
71   {
72     return _os;
73   }
74
75   /**
76    * Returns the destination line.
77    */

78   public int getDestLine()
79   {
80     return _destLine;
81   }
82
83   /**
84    * Sets the line map
85    */

86   public void setLineMap(LineMap lineMap)
87   {
88     _lineMap = lineMap;
89   }
90
91   /**
92    * Gets the line map
93    */

94   public LineMap getLineMap()
95   {
96     return _lineMap;
97   }
98
99   /**
100    * Sets the source filename and line.
101    *
102    * @param filename the filename of the source file.
103    * @param line the line of the source file.
104    */

105   public void setLocation(String JavaDoc filename, int line)
106     throws IOException JavaDoc
107   {
108     if (_lineMap != null && filename != null && line >= 0) {
109       _lineMap.add(filename, line, _destLine);
110     }
111   }
112
113   /**
114    * Generates a unique id.
115    */

116   public int generateId()
117   {
118     return _uniqueId++;
119   }
120
121   /**
122    * Prints a Java escaped string
123    */

124   public void printJavaString(String JavaDoc s)
125     throws IOException JavaDoc
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   /**
150    * Prints a Java escaped string
151    */

152   public void printJavaChar(char ch)
153     throws IOException JavaDoc
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   /**
174    * Pushes an indentation depth.
175    */

176   public void pushDepth()
177     throws IOException JavaDoc
178   {
179     _indentDepth += 2;
180   }
181
182   /**
183    * Pops an indentation depth.
184    */

185   public void popDepth()
186     throws IOException JavaDoc
187   {
188     _indentDepth -= 2;
189   }
190
191   /**
192    * Prints a string
193    */

194   public void print(String JavaDoc s)
195     throws IOException JavaDoc
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 JavaDoc
224   {
225     print(new String JavaDoc(buffer, offset, length));
226   }
227
228   /**
229    * Prints a character.
230    */

231   public void print(char ch)
232     throws IOException JavaDoc
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   /**
249    * Prints a boolean.
250    */

251   public void print(boolean b)
252     throws IOException JavaDoc
253   {
254     if (_startLine)
255       printIndent();
256     
257     _os.print(b);
258     _lastCr = false;
259   }
260
261   /**
262    * Prints an integer.
263    */

264   public void print(int i)
265     throws IOException JavaDoc
266   {
267     if (_startLine)
268       printIndent();
269     
270     _os.print(i);
271     _lastCr = false;
272   }
273
274   /**
275    * Prints an long
276    */

277   public void print(long l)
278     throws IOException JavaDoc
279   {
280     if (_startLine)
281       printIndent();
282     
283     _os.print(l);
284     _lastCr = false;
285   }
286
287   /**
288    * Prints an object.
289    */

290   public void print(Object JavaDoc o)
291     throws IOException JavaDoc
292   {
293     if (_startLine)
294       printIndent();
295     
296     _os.print(o);
297     _lastCr = false;
298   }
299
300   /**
301    * Prints a string with a new line
302    */

303   public void println(String JavaDoc s)
304     throws IOException JavaDoc
305   {
306     print(s);
307     println();
308   }
309
310   /**
311    * Prints a boolean with a new line
312    */

313   public void println(boolean v)
314     throws IOException JavaDoc
315   {
316     print(v);
317     println();
318   }
319
320   /**
321    * Prints a character.
322    */

323   public void println(char ch)
324     throws IOException JavaDoc
325   {
326     print(ch);
327     println();
328   }
329
330   /**
331    * Prints an integer with a new line
332    */

333   public void println(int v)
334     throws IOException JavaDoc
335   {
336     print(v);
337     println();
338   }
339
340   /**
341    * Prints an long with a new line
342    */

343   public void println(long v)
344     throws IOException JavaDoc
345   {
346     print(v);
347     println();
348   }
349
350   /**
351    * Prints an object with a new line
352    */

353   public void println(Object JavaDoc v)
354     throws IOException JavaDoc
355   {
356     print(v);
357     println();
358   }
359
360   /**
361    * Prints a newline
362    */

363   public void println()
364     throws IOException JavaDoc
365   {
366     _os.println();
367     if (! _lastCr)
368       _destLine++;
369     _lastCr = false;
370     _startLine = true;
371   }
372   
373   /**
374    * Prints the Java represention of the class
375    */

376   public void printClass(Class JavaDoc cl)
377     throws IOException JavaDoc
378   {
379     if (! cl.isArray())
380       print(cl.getName().replace('$', '.'));
381     else {
382       printClass(cl.getComponentType());
383       print("[]");
384     }
385   }
386   
387   /**
388    * Converts a java primitive type to a Java object.
389    *
390    * @param value the java expression to be converted
391    * @param javaType the type of the converted expression.
392    */

393   public void printJavaTypeToObject(String JavaDoc value, Class JavaDoc javaType)
394     throws IOException JavaDoc
395   {
396     if (Object JavaDoc.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   /**
419    * Converts a java primitive type to a Java object.
420    *
421    * @param value the java expression to be converted
422    * @param javaType the type of the converted expression.
423    */

424   public void printJavaTypeToObject(String JavaDoc value, JClass javaType)
425     throws IOException JavaDoc
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   /**
448    * Prints the indentation at the beginning of a line.
449    */

450   public void printIndent()
451     throws IOException JavaDoc
452   {
453     _startLine = false;
454     
455     for (int i = 0; i < _indentDepth; i++)
456       _os.print(' ');
457
458     _lastCr = false;
459   }
460
461   /**
462    * Generates the smap file.
463    */

464   public void generateSmap()
465     throws IOException JavaDoc
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 JavaDoc 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   /**
489    * Returns the error message with proper line number.
490    */

491   public String JavaDoc errorMessage(String JavaDoc message)
492   {
493     /*
494     if (_srcFilename == null)
495       return message;
496     else
497       return _srcFilename + ':' + _srcLine + ": " + message;
498     */

499     return message;
500   }
501
502   public void flush()
503   {
504   }
505
506   public void close()
507   {
508   }
509 }
510
Popular Tags