View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.comments;
5   
6   
7   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
8   import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
9   import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty;
10  
11  /**
12   * Restrictions regarding the legal placement and content of the file header.
13   * 
14   * @author Brian Remedios
15   */
16  public class HeaderCommentsRule extends AbstractCommentRule {
17  	
18  	private static final String[] requiredWords = new String[] { "copyright" };
19  	private static final String[] requiredTags = new String[] { "author", "version" };
20  	
21  	public static final StringMultiProperty REQUIRED_TERMS_DESCRIPTOR = new StringMultiProperty("requiredTerms",
22  	    	"Expected terms or phrases in the code header", requiredWords, 1.0f, '|');
23  	   
24  	public static final StringMultiProperty REQUIRED_TAGS_DESCRIPTOR = new StringMultiProperty("requiredTags",
25      		"Expected tags in the header", requiredTags, 2.0f, '|');
26     
27  	enum RequiredHeaderPlacement {
28  		BeforePackageDeclaration("Before package"), 
29  		BeforeImportStatements("Before imports"), 
30  		BeforeTypeDeclaration("Before types"), 
31  		Anywhere("Anywhere"); 
32  		
33  		private final String label;
34  		
35  		RequiredHeaderPlacement(String theLabel) {
36  			label = theLabel;
37  		}
38  		
39  		public static String[] labels() {
40  			String[] labels = new String[values().length];
41  			int i=0;
42  			for (RequiredHeaderPlacement placement : values()) {
43  				labels[i++] = placement.label;
44  			}
45  			return labels;
46  		}
47  	}
48  	
49      public static final EnumeratedProperty<RequiredHeaderPlacement> HEADER_PLACEMENT_DESCRIPTOR = new EnumeratedProperty<RequiredHeaderPlacement>(
50      	    "headerPlacement",
51      	    "Placement of the header comment", 
52      	    RequiredHeaderPlacement.labels(), 
53      	    RequiredHeaderPlacement.values(), 
54      	    0, 3.0f
55      	    );
56  		
57  	public HeaderCommentsRule() {
58  		definePropertyDescriptor(REQUIRED_TERMS_DESCRIPTOR);
59  		definePropertyDescriptor(REQUIRED_TAGS_DESCRIPTOR);
60  		definePropertyDescriptor(HEADER_PLACEMENT_DESCRIPTOR);
61  	}
62  
63  	@Override
64      public Object visit(ASTCompilationUnit cUnit, Object data) {
65    
66  //		SortedMap<Integer, Object> itemsByLineNumber = orderedCommentsAndDeclarations(cUnit);
67  
68          return super.visit(cUnit, data);
69      }
70  }