1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd; |
5 |
| |
6 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
7 |
| import net.sourceforge.pmd.ast.JavaParser; |
8 |
| import net.sourceforge.pmd.ast.ParseException; |
9 |
| import net.sourceforge.pmd.cpd.FileFinder; |
10 |
| import net.sourceforge.pmd.cpd.JavaLanguage; |
11 |
| import net.sourceforge.pmd.dfa.DataFlowFacade; |
12 |
| import net.sourceforge.pmd.renderers.Renderer; |
13 |
| import net.sourceforge.pmd.symboltable.SymbolFacade; |
14 |
| import net.sourceforge.pmd.jaxen.AttributeAxisIterator; |
15 |
| |
16 |
| import java.io.BufferedInputStream; |
17 |
| import java.io.File; |
18 |
| import java.io.FileNotFoundException; |
19 |
| import java.io.IOException; |
20 |
| import java.io.InputStream; |
21 |
| import java.io.InputStreamReader; |
22 |
| import java.io.Reader; |
23 |
| import java.io.UnsupportedEncodingException; |
24 |
| import java.util.ArrayList; |
25 |
| import java.util.Enumeration; |
26 |
| import java.util.Iterator; |
27 |
| import java.util.List; |
28 |
| import java.util.StringTokenizer; |
29 |
| import java.util.zip.ZipEntry; |
30 |
| import java.util.zip.ZipFile; |
31 |
| |
32 |
| public class PMD { |
33 |
| |
34 |
| public static final String EOL = System.getProperty("line.separator", "\n"); |
35 |
| public static final String VERSION = "3.3"; |
36 |
| |
37 |
| private TargetJDKVersion targetJDKVersion; |
38 |
| private String excludeMarker = ExcludeLines.EXCLUDE_MARKER; |
39 |
| |
40 |
2
| public PMD() {
|
41 |
2
| this(new TargetJDK1_4());
|
42 |
| } |
43 |
| |
44 |
753
| public PMD(TargetJDKVersion targetJDKVersion) {
|
45 |
753
| this.targetJDKVersion = targetJDKVersion;
|
46 |
| } |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
753
| public void processFile(Reader reader, RuleSet ruleSet, RuleContext ctx) throws PMDException {
|
57 |
753
| try {
|
58 |
753
| ExcludeLines excluder = new ExcludeLines(reader, excludeMarker);
|
59 |
753
| ctx.excludeLines(excluder.getLinesToExclude());
|
60 |
| |
61 |
753
| JavaParser parser = targetJDKVersion.createParser(excluder.getCopyReader());
|
62 |
753
| ASTCompilationUnit c = parser.CompilationUnit();
|
63 |
753
| Thread.yield();
|
64 |
| |
65 |
| |
66 |
753
| SymbolFacade stb = new SymbolFacade();
|
67 |
753
| stb.initializeWith(c);
|
68 |
| |
69 |
753
| if (ruleSet.usesDFA()) {
|
70 |
0
| DataFlowFacade dff = new DataFlowFacade();
|
71 |
0
| dff.initializeWith(c);
|
72 |
| } |
73 |
| |
74 |
753
| List acus = new ArrayList();
|
75 |
753
| acus.add(c);
|
76 |
753
| ruleSet.apply(acus, ctx);
|
77 |
753
| reader.close();
|
78 |
| } catch (ParseException pe) { |
79 |
0
| throw new PMDException("Error while parsing " + ctx.getSourceCodeFilename(), pe);
|
80 |
| } catch (Exception e) { |
81 |
0
| throw new PMDException("Error while processing " + ctx.getSourceCodeFilename(), e);
|
82 |
| } |
83 |
| } |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public void processFile(InputStream fileContents, String encoding, RuleSet ruleSet, RuleContext ctx) throws PMDException {
|
98 |
0
| try {
|
99 |
0
| processFile(new InputStreamReader(fileContents, encoding), ruleSet, ctx);
|
100 |
| } catch (UnsupportedEncodingException uee) { |
101 |
0
| throw new PMDException("Unsupported encoding exception: " + uee.getMessage());
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
0
| public void processFile(InputStream fileContents, RuleSet ruleSet, RuleContext ctx) throws PMDException {
|
117 |
0
| processFile(fileContents, System.getProperty("file.encoding"), ruleSet, ctx);
|
118 |
| } |
119 |
| |
120 |
0
| public void setExcludeMarker(String marker) {
|
121 |
0
| this.excludeMarker = marker;
|
122 |
| } |
123 |
| |
124 |
| |
125 |
0
| public static void main(String[] args) {
|
126 |
0
| CommandLineOptions opts = new CommandLineOptions(args);
|
127 |
| |
128 |
0
| List files;
|
129 |
0
| if (opts.containsCommaSeparatedFileList()) {
|
130 |
0
| files = collectFromCommaDelimitedString(opts.getInputPath());
|
131 |
| } else { |
132 |
0
| files = collectFilesFromOneName(opts.getInputPath());
|
133 |
| } |
134 |
| |
135 |
0
| PMD pmd;
|
136 |
0
| if (opts.getTargetJDK().equals("1.3")) {
|
137 |
0
| if (opts.debugEnabled()) System.out.println("In JDK 1.3 mode");
|
138 |
0
| pmd = new PMD(new TargetJDK1_3());
|
139 |
0
| } else if (opts.getTargetJDK().equals("1.5")) {
|
140 |
0
| if (opts.debugEnabled()) System.out.println("In JDK 1.5 mode");
|
141 |
0
| pmd = new PMD(new TargetJDK1_5());
|
142 |
| } else { |
143 |
0
| if (opts.debugEnabled()) System.out.println("In JDK 1.4 mode");
|
144 |
0
| pmd = new PMD();
|
145 |
| } |
146 |
0
| pmd.setExcludeMarker(opts.getExcludeMarker());
|
147 |
| |
148 |
0
| RuleContext ctx = new RuleContext();
|
149 |
0
| Report report = new Report();
|
150 |
0
| ctx.setReport(report);
|
151 |
0
| report.start();
|
152 |
| |
153 |
0
| try {
|
154 |
0
| RuleSetFactory ruleSetFactory = new RuleSetFactory();
|
155 |
0
| RuleSet rules = ruleSetFactory.createRuleSet(opts.getRulesets());
|
156 |
0
| if (opts.debugEnabled()) {
|
157 |
0
| for (Iterator i = rules.getRules().iterator(); i.hasNext();) {
|
158 |
0
| Rule r = (Rule)i.next();
|
159 |
0
| System.out.println("Loaded rule " + r.getName());
|
160 |
| } |
161 |
| } |
162 |
| |
163 |
0
| for (Iterator i = files.iterator(); i.hasNext();) {
|
164 |
0
| DataSource dataSource = (DataSource) i.next();
|
165 |
0
| String niceFileName = dataSource.getNiceFileName(opts.shortNamesEnabled(), opts.getInputPath());
|
166 |
0
| ctx.setSourceCodeFilename(niceFileName);
|
167 |
0
| if (opts.debugEnabled()) {
|
168 |
0
| System.out.println("Processing " + ctx.getSourceCodeFilename());
|
169 |
| } |
170 |
0
| try {
|
171 |
0
| pmd.processFile(new BufferedInputStream(dataSource.getInputStream()), opts.getEncoding(), rules, ctx);
|
172 |
| } catch (PMDException pmde) { |
173 |
0
| if (opts.debugEnabled()) {
|
174 |
0
| pmde.getReason().printStackTrace();
|
175 |
| } |
176 |
0
| ctx.getReport().addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
|
177 |
| } |
178 |
| } |
179 |
| } catch (FileNotFoundException fnfe) { |
180 |
0
| System.out.println(opts.usage());
|
181 |
0
| fnfe.printStackTrace();
|
182 |
| } catch (RuleSetNotFoundException rsnfe) { |
183 |
0
| System.out.println(opts.usage());
|
184 |
0
| rsnfe.printStackTrace();
|
185 |
| } catch (IOException ioe) { |
186 |
0
| System.out.println(opts.usage());
|
187 |
0
| ioe.printStackTrace();
|
188 |
| } |
189 |
0
| report.end();
|
190 |
| |
191 |
0
| try {
|
192 |
0
| Renderer r = opts.createRenderer();
|
193 |
0
| System.out.println(r.render(ctx.getReport()));
|
194 |
| } catch (Exception e) { |
195 |
0
| System.out.println(e.getMessage());
|
196 |
0
| System.out.println(opts.usage());
|
197 |
0
| if (opts.debugEnabled()) {
|
198 |
0
| e.printStackTrace();
|
199 |
| } |
200 |
| } |
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
0
| private static List collectFilesFromOneName(String inputFileName) {
|
211 |
0
| return collect(inputFileName);
|
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
0
| private static List collectFromCommaDelimitedString(String fileList) {
|
221 |
0
| List files = new ArrayList();
|
222 |
0
| for (StringTokenizer st = new StringTokenizer(fileList, ","); st.hasMoreTokens();) {
|
223 |
0
| files.addAll(collect(st.nextToken()));
|
224 |
| } |
225 |
0
| return files;
|
226 |
| } |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
0
| private static List collect(String filename) {
|
236 |
0
| File inputFile = new File(filename);
|
237 |
0
| if (!inputFile.exists()) {
|
238 |
0
| throw new RuntimeException("File " + inputFile.getName() + " doesn't exist");
|
239 |
| } |
240 |
0
| List dataSources = new ArrayList();
|
241 |
0
| if (!inputFile.isDirectory()) {
|
242 |
0
| if (filename.endsWith(".zip") || filename.endsWith(".jar")) {
|
243 |
0
| ZipFile zipFile;
|
244 |
0
| try {
|
245 |
0
| zipFile = new ZipFile(inputFile);
|
246 |
0
| Enumeration e = zipFile.entries();
|
247 |
0
| while (e.hasMoreElements()) {
|
248 |
0
| ZipEntry zipEntry = (ZipEntry) e.nextElement();
|
249 |
0
| if (zipEntry.getName().endsWith(".java")) {
|
250 |
0
| dataSources.add(new ZipDataSource(zipFile, zipEntry));
|
251 |
| } |
252 |
| } |
253 |
| } catch (IOException ze) { |
254 |
0
| throw new RuntimeException("Zip file " + inputFile.getName() + " can't be opened");
|
255 |
| } |
256 |
| } else { |
257 |
0
| dataSources.add(new FileDataSource(inputFile));
|
258 |
| } |
259 |
| } else { |
260 |
0
| FileFinder finder = new FileFinder();
|
261 |
0
| List files = finder.findFilesFrom(inputFile.getAbsolutePath(), new JavaLanguage.JavaFileOrDirectoryFilter(), true);
|
262 |
0
| for (Iterator i = files.iterator(); i.hasNext();) {
|
263 |
0
| dataSources.add(new FileDataSource((File) i.next()));
|
264 |
| } |
265 |
| } |
266 |
0
| return dataSources;
|
267 |
| } |
268 |
| |
269 |
| } |