1
2
3
4 package net.sourceforge.pmd.lang.xml.rule;
5
6 import net.sourceforge.pmd.RuleContext;
7 import net.sourceforge.pmd.lang.xml.ast.XmlNode;
8
9 import org.w3c.dom.Attr;
10 import org.w3c.dom.CharacterData;
11 import org.w3c.dom.Comment;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.DocumentType;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Entity;
16 import org.w3c.dom.EntityReference;
17 import org.w3c.dom.NamedNodeMap;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.Notation;
20 import org.w3c.dom.ProcessingInstruction;
21 import org.w3c.dom.Text;
22
23
24
25
26
27
28 public class AbstractDomXmlRule extends AbstractXmlRule {
29
30 @Override
31 protected void visit(XmlNode node, RuleContext ctx) {
32 final Node domNode = node.getNode();
33
34
35 visitDomNode(node, domNode, ctx);
36
37
38 visitAttributeNodes(node, domNode, ctx);
39 }
40
41 protected void visitDomNode(XmlNode node, Node domNode, RuleContext ctx) {
42 switch (domNode.getNodeType()) {
43 case Node.CDATA_SECTION_NODE:
44 visit(node, (CharacterData) domNode, ctx);
45 break;
46 case Node.COMMENT_NODE:
47 visit(node, (Comment) domNode, ctx);
48 break;
49 case Node.DOCUMENT_NODE:
50 visit(node, (Document) domNode, ctx);
51 break;
52 case Node.DOCUMENT_TYPE_NODE:
53 visit(node, (DocumentType) domNode, ctx);
54 break;
55 case Node.ELEMENT_NODE:
56 visit(node, (Element) domNode, ctx);
57 break;
58 case Node.ENTITY_NODE:
59 visit(node, (Entity) domNode, ctx);
60 break;
61 case Node.ENTITY_REFERENCE_NODE:
62 visit(node, (EntityReference) domNode, ctx);
63 break;
64 case Node.NOTATION_NODE:
65 visit(node, (Notation) domNode, ctx);
66 break;
67 case Node.PROCESSING_INSTRUCTION_NODE:
68 visit(node, (ProcessingInstruction) domNode, ctx);
69 break;
70 case Node.TEXT_NODE:
71 visit(node, (Text) domNode, ctx);
72 break;
73 default:
74 throw new RuntimeException("Unexpected node type: " + domNode.getNodeType() + " on node: " + domNode);
75 }
76 }
77
78 protected void visitAttributeNodes(XmlNode node, Node domNode, RuleContext ctx) {
79 NamedNodeMap attributes = domNode.getAttributes();
80 if (attributes != null) {
81 for (int i = 0; i < attributes.getLength(); i++) {
82 visit(node, (Attr) attributes.item(i), ctx);
83 }
84 }
85 }
86
87 protected void visit(XmlNode node, Attr attr, RuleContext ctx) {
88 }
89
90 protected void visit(XmlNode node, CharacterData characterData, RuleContext ctx) {
91 super.visit(node, ctx);
92 }
93
94 protected void visit(XmlNode node, Comment comment, RuleContext ctx) {
95 super.visit(node, ctx);
96 }
97
98 protected void visit(XmlNode node, Document document, RuleContext ctx) {
99 super.visit(node, ctx);
100 }
101
102 protected void visit(XmlNode node, DocumentType documentType, RuleContext ctx) {
103 super.visit(node, ctx);
104 }
105
106 protected void visit(XmlNode node, Element element, RuleContext ctx) {
107 super.visit(node, ctx);
108 }
109
110 protected void visit(XmlNode node, Entity entity, RuleContext ctx) {
111 super.visit(node, ctx);
112 }
113
114 protected void visit(XmlNode node, EntityReference entityReference, RuleContext ctx) {
115 super.visit(node, ctx);
116 }
117
118 protected void visit(XmlNode node, Notation notation, RuleContext ctx) {
119 super.visit(node, ctx);
120 }
121
122 protected void visit(XmlNode node, ProcessingInstruction processingInstruction, RuleContext ctx) {
123 super.visit(node, ctx);
124 }
125
126 protected void visit(XmlNode node, Text text, RuleContext ctx) {
127 super.visit(node, ctx);
128 }
129 }