View Javadoc

1   /*
2    * Copyright (c) 2001-2003 The XDoclet team
3    * All rights reserved.
4    */
5   package xjavadoc;
6   
7   import java.io.PrintWriter;
8   import java.io.Writer;
9   
10  /***
11   * This visitor prints a node and all its children to a PrintWriter. The
12   * PrintWriter must be passed to the data variable in the jjtAccept method of
13   * the node from which printing should start. T print a whole class, call
14   * compilationUnit.jjtAccept( new PrintVisitor(), System.out );
15   *
16   * @author    Aslak Hellesøy
17   * @created   8. januar 2002
18   * @todo      replace PrintWriter with PrintWriter and remove the
19   *      addUnicodeEscapes()
20   */
21  public final class NodePrinter
22  {
23  
24  	/***
25  	 * Describe what the method does
26  	 *
27  	 * @param node     Describe what the parameter does
28  	 * @param o
29  	 * @todo-javadoc   Write javadocs for method parameter
30  	 * @todo-javadoc   Write javadocs for method
31  	 * @todo-javadoc   Write javadocs for method parameter
32  	 * @todo-javadoc   Write javadocs for method parameter
33  	 * @todo-javadoc   Write javadocs for return value
34  	 */
35  	public static void print( SimpleNode node, Writer o )
36  	{
37  		PrintWriter out = new PrintWriter( o );
38  		Token t1 = node.getFirstToken();
39  		Token t = new Token();
40  
41  		t.next = t1;
42  
43  		SimpleNode n;
44  
45  		for( int ord = 0; ord < node.jjtGetNumChildren(); ord++ )
46  		{
47  			n = ( SimpleNode ) node.jjtGetChild( ord );
48  			while( true )
49  			{
50  				t = t.next;
51  				if( t == n.getFirstToken() )
52  				{
53  					break;
54  				}
55  				print( t, out );
56  			}
57  			print( n, out );
58  			t = n.getLastToken();
59  		}
60  
61  		while( t != node.getLastToken() && t != null )
62  		{
63  			t = t.next;
64  			if( t != null )
65  			{
66  				print( t, out );
67  			}
68  		}
69  	}
70  
71  	/***
72  	 * Describe what the method does
73  	 *
74  	 * @param t        Describe what the parameter does
75  	 * @param out      Describe what the parameter does
76  	 * @todo-javadoc   Write javadocs for method
77  	 * @todo-javadoc   Write javadocs for method parameter
78  	 * @todo-javadoc   Write javadocs for method parameter
79  	 */
80  	private final static void print( Token t, PrintWriter out )
81  	{
82  		Token tt = t.specialToken;
83  
84  		if( tt != null )
85  		{
86  			while( tt.specialToken != null )
87  			{
88  				tt = tt.specialToken;
89  			}
90  			while( tt != null )
91  			{
92  				out.print( addUnicodeEscapes( tt.image ) );
93  //				out.print(tt.image);
94  				tt = tt.next;
95  			}
96  		}
97  		out.print( addUnicodeEscapes( t.image ) );
98  //		out.print(t.image);
99  	}
100 
101 	/***
102 	 * Describe the method
103 	 *
104 	 * @param str      Describe the method parameter
105 	 * @return         Describe the return value
106 	 * @todo-javadoc   Describe the method
107 	 * @todo-javadoc   Describe the method parameter
108 	 * @todo-javadoc   Write javadocs for return value
109 	 */
110 	private final static String addUnicodeEscapes( final String str )
111 	{
112 		return str;
113 	}
114 
115 	/***
116 	 * Describe the method
117 	 *
118 	 * @param str      Describe the method parameter
119 	 * @return         Describe the return value
120 	 * @todo-javadoc   Describe the method
121 	 * @todo-javadoc   Describe the method parameter
122 	 * @todo-javadoc   Write javadocs for return value
123 	 */
124 	protected String addUnicodeEscapesOld( String str )
125 	{
126 		String retval = "";
127 		char ch;
128 
129 		for( int i = 0; i < str.length(); i++ )
130 		{
131 			ch = str.charAt( i );
132 			if( ( ch < 0x20 || ch > 0x7e ) &&
133 				ch != '\t' && ch != '\n' && ch != '\r' && ch != '\f' )
134 			{
135 				String s = "0000" + Integer.toString( ch, 16 );
136 
137 				retval += "//u" + s.substring( s.length() - 4, s.length() );
138 			}
139 			else
140 			{
141 				retval += ch;
142 			}
143 		}
144 		return retval;
145 	}
146 
147 }