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.ParserOptions;
    11  import net.sourceforge.pmd.lang.ast.Node;
    12  import net.sourceforge.pmd.lang.rule.AbstractRule;
    13  import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
    14  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
    15  import net.sourceforge.pmd.lang.xml.XmlParserOptions;
    16  import net.sourceforge.pmd.lang.xml.ast.XmlNode;
    17
    18  /**
    19   * This is a base class for XML Java bases rules. Subclasses should override
    20   * {@link #visit(XmlNode, RuleContext)} and can call <code>super</code>
    21   * to visit children.
    22   */
    23  public class AbstractXmlRule extends AbstractRule implements ImmutableLanguage {
    24
    25      public static final BooleanProperty COALESCING_DESCRIPTOR = XmlParserOptions.COALESCING_DESCRIPTOR;
    26      public static final BooleanProperty EXPAND_ENTITY_REFERENCES_DESCRIPTOR = XmlParserOptions.EXPAND_ENTITY_REFERENCES_DESCRIPTOR;
    27      public static final BooleanProperty IGNORING_COMMENTS_DESCRIPTOR = XmlParserOptions.IGNORING_COMMENTS_DESCRIPTOR;
    28      public static final BooleanProperty IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR = XmlParserOptions.IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR;
    29      public static final BooleanProperty NAMESPACE_AWARE_DESCRIPTOR = XmlParserOptions.NAMESPACE_AWARE_DESCRIPTOR;
    30      public static final BooleanProperty VALIDATING_DESCRIPTOR = XmlParserOptions.VALIDATING_DESCRIPTOR;
    31      public static final BooleanProperty XINCLUDE_AWARE_DESCRIPTOR = XmlParserOptions.XINCLUDE_AWARE_DESCRIPTOR;
    32
    33      public AbstractXmlRule() {
    34  	super.setLanguage(Language.XML);
    35  	definePropertyDescriptor(COALESCING_DESCRIPTOR);
    36  	definePropertyDescriptor(EXPAND_ENTITY_REFERENCES_DESCRIPTOR);
    37  	definePropertyDescriptor(IGNORING_COMMENTS_DESCRIPTOR);
    38  	definePropertyDescriptor(IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR);
    39  	definePropertyDescriptor(NAMESPACE_AWARE_DESCRIPTOR);
    40  	definePropertyDescriptor(VALIDATING_DESCRIPTOR);
    41  	definePropertyDescriptor(XINCLUDE_AWARE_DESCRIPTOR);
    42      }
    43
    44      @Override
    45      public ParserOptions getParserOptions() {
    46  	return new XmlParserOptions(this);
    47      }
    48
    49      public void apply(List<? extends Node> nodes, RuleContext ctx) {
    50  	visitAll(nodes, ctx);
    51      }
    52
    53      protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
    54  	for (Object element : nodes) {
    55  	    XmlNode node = (XmlNode) element;
    56  	    visit(node, ctx);
    57  	}
    58      }
    59
    60      protected void visit(XmlNode node, RuleContext ctx) {
    61  	final int numChildren = node.jjtGetNumChildren();
    62  	for (int i = 0; i < numChildren; i++) {
    63  	    XmlNode child = (XmlNode) node.jjtGetChild(i);
    64  	    visit(child, ctx);
    65  	}
    66      }
    67  }