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