1 package org.codehaus.mojo.taglist.beans;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.IOUtil;
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.Reader;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37
38
39
40
41 public class FileReport
42 implements Comparable
43 {
44
45
46
47
48 private final File file;
49
50
51
52
53 private final String encoding;
54
55
56
57
58 private String className;
59
60
61
62
63 private final Map tagListing;
64
65
66
67
68 private static final String PACKAGE_STR = "package";
69
70
71
72
73
74
75
76 public FileReport( File file, String encoding )
77 {
78 this.file = file;
79 this.encoding = encoding;
80 this.tagListing = new HashMap();
81 }
82
83
84
85
86
87
88
89 public void addComment( String comment, int lineIndex )
90 {
91 tagListing.put( new Integer( lineIndex ), comment );
92 }
93
94
95
96
97
98
99
100 public String getClassNameWithSlash()
101 {
102 return className.replace( '.', '/' );
103 }
104
105
106
107
108
109
110
111
112 private Reader getReader( File fileToRead ) throws IOException
113 {
114 InputStream in = new FileInputStream( fileToRead );
115 return ( encoding == null ) ? new InputStreamReader( in ) : new InputStreamReader( in, encoding );
116 }
117
118
119
120
121
122
123 public String getClassName()
124 {
125 if ( className != null )
126 {
127 return className;
128 }
129
130 BufferedReader reader = null;
131 String packageName = null;
132 try
133 {
134 reader = new BufferedReader( getReader( file ) );
135 String currentLine = reader.readLine();
136 if ( currentLine != null )
137 {
138 currentLine = currentLine.trim();
139 }
140 while ( currentLine != null )
141 {
142 if ( currentLine.startsWith( PACKAGE_STR ) )
143 {
144 packageName = currentLine.substring( PACKAGE_STR.length() ).trim().replaceAll( ";", "" ).trim();
145 break;
146 }
147 String nextLine = reader.readLine();
148 if ( nextLine == null )
149 {
150 currentLine = nextLine;
151 }
152 else
153 {
154 currentLine = nextLine.trim();
155 }
156 }
157 }
158 catch ( IOException e )
159 {
160 packageName = "unknown";
161 }
162 finally
163 {
164 IOUtil.close( reader );
165 }
166
167 className = packageName + "." + file.getName().replaceAll( "\\.java$", "" );
168
169 return className;
170 }
171
172
173
174
175
176
177 public Collection getLineIndexes()
178 {
179 ArrayList list = new ArrayList();
180 list.addAll( tagListing.keySet() );
181 Collections.sort( list );
182 return list;
183 }
184
185
186
187
188
189
190
191 public String getComment( Integer lineIndex )
192 {
193 return (String) tagListing.get( lineIndex );
194 }
195
196
197
198
199
200
201 public int compareTo( Object o )
202 {
203 if ( o instanceof FileReport )
204 {
205 FileReport fileReport = (FileReport) o;
206 return this.getClassName().compareTo( fileReport.getClassName() );
207 }
208 else
209 {
210 return 0;
211 }
212 }
213
214
215
216
217
218
219 public boolean equals( Object r )
220 {
221
222
223 return ( this.compareTo( r ) == 0 );
224 }
225
226
227
228
229
230
231 public int hashCode()
232 {
233 assert false : "hashCode not designed";
234 return 1;
235 }
236
237 }