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 while ( stok.nextToken() != StreamTokenizer.TT_EOF )
88 {
89
90 if ( stok.sval == null )
91 {
92 if ( stok.ttype == '{' )
93 {
94 openBracesCount++;
95 }
96 else if ( stok.ttype == '}' )
97 {
98 if ( --openBracesCount == 0 )
99 {
100
101 return;
102 }
103 }
104 continue;
105 }
106
107
108 if ( "package".equals( stok.sval ) && stok.ttype != '\"' )
109 {
110 stok.nextToken();
111 this.setPackageType( new PackageType( stok.sval ) );
112 }
113
114
115 if ( "import".equals( stok.sval ) && stok.ttype != '\"' )
116 {
117 stok.nextToken();
118
119 String name = stok.sval;
120
121
122
123
124
125
126
127
128 if ( name.charAt( name.length() - 1 ) == '.' )
129 {
130 name = name + '*';
131 }
132
133 this.addImportType( new ImportType( name ) );
134 }
135
136
137
138 if ( ( "class".equals( stok.sval ) || "interface".equals( stok.sval ) || "enum".equals( stok.sval ) )
139 && stok.ttype != '\"' )
140 {
141 stok.nextToken();
142 this.addClassType( new ClassType( nestedPrefix + stok.sval,
143 getFilenameWithoutPathOrExtension( this.getPath() ) ) );
144 parseRecursive( nestedPrefix + stok.sval + ".", stok );
145 }
146
147 }
148 }
149
150
151
152
153 private static String getFilenameWithoutPathOrExtension( Path path )
154 {
155 String newFilename = path.getFileName().toString();
156
157 int extensionIndex = newFilename.lastIndexOf( ".java" );
158 if ( extensionIndex >= 0 )
159 {
160 newFilename = newFilename.substring( 0, extensionIndex );
161 }
162 return newFilename;
163 }
164
165
166
167
168 private StreamTokenizer getTokenizer( Reader reader )
169 {
170 StreamTokenizer stok = new StreamTokenizer( reader );
171
172
173 stok.commentChar( '*' );
174 stok.wordChars( '_', '_' );
175
176
177 stok.slashStarComments( true );
178 stok.slashSlashComments( true );
179
180 return stok;
181 }
182
183 private Reader getReader()
184 throws IOException
185 {
186 if ( Files.notExists( this.getPath() ) )
187 {
188 throw new IOException( this.getPath() + " does not exist!" );
189 }
190
191 if ( this.getEncoding() != null )
192 {
193 return new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
194 }
195 else
196 {
197 return new FileReader( this.getPath().toFile() );
198 }
199 }
200 }