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 java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.Reader;
28 import java.nio.file.Files;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.TreeSet;
33
34
35
36
37
38
39 public class FileReport implements Comparable<FileReport> {
40
41
42
43
44 private final File file;
45
46
47
48
49 private final String encoding;
50
51
52
53
54 private String className;
55
56
57
58
59 private final Map<Integer, String> tagListing;
60
61
62
63
64 private static final String PACKAGE_STR = "package";
65
66
67
68
69
70
71
72 public FileReport(File file, String encoding) {
73 this.file = file;
74 this.encoding = encoding;
75 this.tagListing = new HashMap<>();
76 }
77
78
79
80
81
82
83
84 public void addComment(String comment, int lineIndex) {
85 tagListing.put(lineIndex, comment);
86 }
87
88
89
90
91
92
93
94 public String getClassNameWithSlash() {
95 return className.replace('.', '/');
96 }
97
98
99
100
101
102
103
104
105 private Reader getReader(File fileToRead) throws IOException {
106 InputStream in = Files.newInputStream(fileToRead.toPath());
107 return (encoding == null) ? new InputStreamReader(in) : new InputStreamReader(in, encoding);
108 }
109
110
111
112
113
114
115 public String getClassName() {
116 if (className != null) {
117 return className;
118 }
119
120 String packageName = null;
121 try (BufferedReader reader = new BufferedReader(getReader(file))) {
122 String currentLine = reader.readLine();
123 if (currentLine != null) {
124 currentLine = currentLine.trim();
125 }
126 while (currentLine != null) {
127 if (currentLine.startsWith(PACKAGE_STR)) {
128 packageName = currentLine
129 .substring(PACKAGE_STR.length())
130 .trim()
131 .replaceAll(";", "")
132 .trim();
133 break;
134 }
135 String nextLine = reader.readLine();
136 if (nextLine == null) {
137 currentLine = nextLine;
138 } else {
139 currentLine = nextLine.trim();
140 }
141 }
142 } catch (IOException e) {
143 packageName = "unknown";
144 }
145
146 className = packageName + "." + file.getName().replaceAll("\\.java$", "");
147
148 return className;
149 }
150
151
152
153
154
155
156 public Collection<Integer> getLineIndexes() {
157 return new TreeSet<>(tagListing.keySet());
158 }
159
160
161
162
163
164
165
166 public String getComment(Integer lineIndex) {
167 return tagListing.get(lineIndex);
168 }
169
170
171
172
173
174
175 public int compareTo(FileReport o) {
176 if (o != null) {
177 return this.getClassName().compareTo(o.getClassName());
178 } else {
179 return 0;
180 }
181 }
182
183
184
185
186
187
188 public boolean equals(Object r) {
189
190
191 if (!(r instanceof FileReport)) {
192 return false;
193 }
194 return (this.compareTo((FileReport) r) == 0);
195 }
196
197
198
199
200
201
202 public int hashCode() {
203 assert false : "hashCode not designed";
204 return 1;
205 }
206 }