View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cli;
5   
6   import java.io.File;
7   import java.io.FileReader;
8   import java.util.Iterator;
9   
10  import net.sourceforge.pmd.PMD;
11  import net.sourceforge.pmd.PMDConfiguration;
12  import net.sourceforge.pmd.Rule;
13  import net.sourceforge.pmd.RuleContext;
14  import net.sourceforge.pmd.RuleSet;
15  import net.sourceforge.pmd.RuleSets;
16  import net.sourceforge.pmd.RuleViolation;
17  import net.sourceforge.pmd.SourceCodeProcessor;
18  import net.sourceforge.pmd.lang.Language;
19  import net.sourceforge.pmd.lang.rule.XPathRule;
20  import net.sourceforge.pmd.util.StringUtil;
21  
22  /**
23   * To use this, do this:
24   *
25   * $ cat ~/tmp/Test.java
26   * package foo;
27   * public class Test {
28   *  private int x;
29   * }
30   * $ java net.sourceforge.pmd.util.XPathTest -xpath "//FieldDeclaration" -filename "/home/tom/tmp/Test.java"
31   * Match at line 3 column 11; package name 'foo'; variable name 'x'
32   */
33  public class XPathCLI {
34  
35      private static final Language LANGUAGE = Language.JAVA;
36  
37      public static void main(String[] args) throws Exception {
38          if (args.length != 4) {
39              System.err.println("Wrong arguments.\n");
40              System.err.println("Example:");
41              System.err.println("java " + XPathCLI.class.getName() + " -xpath \"//FieldDeclaration\" -filename \"/home/user/Test.java\"");
42              System.exit(1);
43          }
44  
45          String xpath = args[0].equals("-xpath") ? args[1] : args[3];
46          String filename = args[0].equals("-file") ? args[1] : args[3];
47          
48          Rule rule = new XPathRule(xpath);
49          rule.setMessage("Got one!");
50          rule.setLanguage(LANGUAGE);
51          RuleSet ruleSet = RuleSet.createFor("", rule);
52  
53          RuleContext ctx = PMD.newRuleContext(filename, new File(filename));
54          ctx.setLanguageVersion(LANGUAGE.getDefaultVersion());
55  
56          PMDConfiguration config = new PMDConfiguration();
57          config.setDefaultLanguageVersion(LANGUAGE.getDefaultVersion());
58          
59          new SourceCodeProcessor(config).processSourceCode(new FileReader(filename), new RuleSets(ruleSet), ctx);
60  
61          for (Iterator<RuleViolation> i = ctx.getReport().iterator(); i.hasNext();) {
62              RuleViolation rv = i.next();
63              StringBuilder sb = new StringBuilder("Match at line " + rv.getBeginLine() + " column " + rv.getBeginColumn());
64              if (StringUtil.isNotEmpty(rv.getPackageName())) {
65                  sb.append("; package name '" + rv.getPackageName() + "'");
66              }
67              if (StringUtil.isNotEmpty(rv.getMethodName())) {
68                  sb.append("; method name '" + rv.getMethodName() + "'");
69              }
70              if (StringUtil.isNotEmpty(rv.getVariableName())) {
71                  sb.append("; variable name '" + rv.getVariableName() + "'");
72              }
73              System.out.println(sb.toString());
74          }
75      }
76  }