View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.xml.rule;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.RuleContext;
9   import net.sourceforge.pmd.lang.Language;
10  import net.sourceforge.pmd.lang.LanguageRegistry;
11  import net.sourceforge.pmd.lang.ParserOptions;
12  import net.sourceforge.pmd.lang.ast.Node;
13  import net.sourceforge.pmd.lang.rule.AbstractRule;
14  import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
15  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
16  import net.sourceforge.pmd.lang.xml.XmlLanguageModule;
17  import net.sourceforge.pmd.lang.xml.XmlParserOptions;
18  import net.sourceforge.pmd.lang.xml.ast.XmlNode;
19  
20  /**
21   * This is a base class for XML Java bases rules. Subclasses should override
22   * {@link #visit(XmlNode, RuleContext)} and can call <code>super</code>
23   * to visit children.
24   */
25  public class AbstractXmlRule extends AbstractRule implements ImmutableLanguage {
26  
27      public static final BooleanProperty COALESCING_DESCRIPTOR = XmlParserOptions.COALESCING_DESCRIPTOR;
28      public static final BooleanProperty EXPAND_ENTITY_REFERENCES_DESCRIPTOR = XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR;
29      public static final BooleanProperty IGNORING_COMMENTS_DESCRIPTOR = XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR;
30      public static final BooleanProperty IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR = XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR;
31      public static final BooleanProperty NAMESPACE_AWARE_DESCRIPTOR = XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR;
32      public static final BooleanProperty VALIDATING_DESCRIPTOR = XmlParserOptions.VALIDATING_DESCRIPTOR;
33      public static final BooleanProperty XINCLUDE_AWARE_DESCRIPTOR = XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR;
34  
35      public AbstractXmlRule() {
36  	super.setLanguage(LanguageRegistry.getLanguage(XmlLanguageModule.NAME));
37  	defineProperties();
38      }
39      
40      protected AbstractXmlRule(Language language) {
41  	super.setLanguage(language);
42  	defineProperties();
43      }
44      
45      private void defineProperties() {
46  	definePropertyDescriptor(COALESCING_DESCRIPTOR);
47  	definePropertyDescriptor(EXPAND_ENTITY_REFERENCES_DESCRIPTOR);
48  	definePropertyDescriptor(IGNORING_COMMENTS_DESCRIPTOR);
49  	definePropertyDescriptor(IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR);
50  	definePropertyDescriptor(NAMESPACE_AWARE_DESCRIPTOR);
51  	definePropertyDescriptor(VALIDATING_DESCRIPTOR);
52  	definePropertyDescriptor(XINCLUDE_AWARE_DESCRIPTOR);
53      }
54  
55      @Override
56      public ParserOptions getParserOptions() {
57  	return new XmlParserOptions(this);
58      }
59  
60      public void apply(List<? extends Node> nodes, RuleContext ctx) {
61  	visitAll(nodes, ctx);
62      }
63  
64      protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
65  	for (Object element : nodes) {
66  	    XmlNode node = (XmlNode) element;
67  	    visit(node, ctx);
68  	}
69      }
70  
71      protected void visit(XmlNode node, RuleContext ctx) {
72  	final int numChildren = node.jjtGetNumChildren();
73  	for (int i = 0; i < numChildren; i++) {
74  	    XmlNode child = (XmlNode) node.jjtGetChild(i);
75  	    visit(child, ctx);
76  	}
77      }
78  }