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 StreamTokenizer getTokenizer( Reader reader )
154 {
155 StreamTokenizer stok = new StreamTokenizer( reader );
156
157
158 stok.commentChar( '*' );
159 stok.wordChars( '_', '_' );
160
161
162 stok.slashStarComments( true );
163 stok.slashSlashComments( true );
164
165 return stok;
166 }
167
168 private Reader getReader()
169 throws IOException
170 {
171 if ( Files.notExists( this.getPath() ) )
172 {
173 throw new IOException( this.getPath() + " does not exist!" );
174 }
175
176 if ( this.getEncoding() != null )
177 {
178 return new InputStreamReader( new FileInputStream( this.getPath().toFile() ), this.getEncoding() );
179 }
180 else
181 {
182 return new FileReader( this.getPath().toFile() );
183 }
184 }
185 }