1 package org.apache.maven.jxr.pacman;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.FileInputStream;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.StreamTokenizer;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30
31
32
33
34
35
36
37
38 public class JavaFileImpl
39 extends JavaFile
40 {
41
42
43
44
45
46
47
48 public JavaFileImpl( Path path, String encoding )
49 throws IOException
50 {
51 super( path, encoding );
52
53
54
55
56 this.addImportType( new ImportType( "java.lang.*" ) );
57
58
59 this.parse();
60 }
61
62
63
64
65
66 private void parse()
67 throws IOException
68 {
69 StreamTokenizer stok = null;
70 try ( Reader reader = getReader() )
71 {
72 stok = this.getTokenizer( reader );
73
74 parseRecursive( "", stok );
75 }
76 finally
77 {
78 stok = null;
79 }
80 }
81
82 private void parseRecursive( String nestedPrefix, StreamTokenizer stok )
83 throws IOException
84 {
85 int openBracesCount = 0;
86
87 char prevttype = Character.MIN_VALUE;
88 boolean inTripleQuote = false;
89
90 while ( stok.nextToken() != StreamTokenizer.TT_EOF )
91 {
92
93 if ( stok.sval == null )
94 {
95 if ( stok.ttype == '{' )
96 {
97 openBracesCount++;
98 }
99 else if ( stok.ttype == '}' )
100 {
101 if ( --openBracesCount == 0 )
102 {
103
104 return;
105 }
106 }
107 continue;
108 }
109 else
110 {
111 if ( '"' == stok.ttype && '"' == prevttype )
112 {
113 inTripleQuote = !inTripleQuote;
114 }
115 prevttype = (char) stok.ttype;
116 if ( inTripleQuote )
117 {
118
119 continue;
120 }
121 }
122
123
124 if ( "package".equals( stok.sval ) && stok.ttype != '\"' )
125 {
126 stok.nextToken();
127 if ( stok.sval != null )
128 {
129 this.setPackageType( new PackageType( stok.sval ) );
130 }
131 }
132
133
134 if ( "import".equals( stok.sval ) && stok.ttype != '\"' )
135 {
136 stok.nextToken();
137
138 String name = stok.sval;
139
140
141
142
143
144
145
146
147 if ( name != null )
148 {
149 if ( name.charAt( name.length() - 1 ) == '.' )
150 {
151 name = name + '*';
152 }
153 this.addImportType( new ImportType( name ) );
154 }
155 }
156
157
158
159 if ( ( "class".equals( stok.sval ) || "interface".equals( stok.sval ) || "enum".equals( stok.sval ) )
160 && stok.ttype != '"' )
161 {
162 stok.nextToken();
163 if ( stok.sval != null )
164 {
165 this.addClassType( new ClassType( nestedPrefix + stok.sval,
166 getFilenameWithoutPathOrExtension( this.getPath() ) ) );
167 parseRecursive( nestedPrefix + stok.sval + ".", stok );
168 }
169 }
170
171 }
172 }
173
174
175
176
177 private StreamTokenizer getTokenizer( Reader reader )
178 {
179 StreamTokenizer stok = new StreamTokenizer( reader );
180
181
182 stok.commentChar( '*' );
183 stok.wordChars( '_', '_' );
184
185
186 stok.slashStarComments( true );
187 stok.slashSlashComments( true );
188
189 return stok;
190 }
191
192 private Reader getReader()
193 throws IOException
194 {
195 if ( Files.notExists( this.getPath() ) )
196 {
197 throw new IOException( this.getPath() + " does not exist!" );
198 }
199
200 if ( this.getEncoding() != null )
201 {
202 return new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
203 }
204 else
205 {
206 return new FileReader( this.getPath().toFile() );
207 }
208 }
209 }