View Javadoc

1   package net.sourceforge.pmd.lang.xml;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.IOException;
5   
6   import org.xml.sax.EntityResolver;
7   import org.xml.sax.InputSource;
8   import org.xml.sax.SAXException;
9   
10  import net.sourceforge.pmd.Rule;
11  import net.sourceforge.pmd.lang.ParserOptions;
12  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
13  import net.sourceforge.pmd.util.StringUtil;
14  
15  public class XmlParserOptions extends ParserOptions {
16  
17      // Note: The UI order values are chosen to be larger than those built into XPathRule.
18      public static final BooleanProperty COALESCING_DESCRIPTOR = new BooleanProperty(
19  	    "coalescing",
20  	    "Specifies that the XML parser convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node.",
21  	    Boolean.FALSE, 3.0f);
22      public static final BooleanProperty EXPAND_ENTITY_REFERENCES_DESCRIPTOR = new BooleanProperty(
23  	    "expandEntityReferences", "Specifies that the XML parser expand entity reference nodes.", Boolean.TRUE,
24  	    4.0f);
25      public static final BooleanProperty IGNORING_COMMENTS_DESCRIPTOR = new BooleanProperty("ignoringComments",
26  	    "Specifies that the XML parser ignore comments.", Boolean.FALSE, 5.0f);
27      public static final BooleanProperty IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR = new BooleanProperty(
28  	    "ignoringElementContentWhitespace",
29  	    "Specifies that the XML parser eliminate whitespace in element content.  Setting this to 'true' will force validating.",
30  	    Boolean.FALSE, 6.0f);
31      public static final BooleanProperty NAMESPACE_AWARE_DESCRIPTOR = new BooleanProperty("namespaceAware",
32  	    "Specifies that the XML parser will provide support for XML namespaces.", Boolean.TRUE, 7.0f);
33      public static final BooleanProperty VALIDATING_DESCRIPTOR = new BooleanProperty("validating",
34  	    "Specifies that the XML parser will validate documents as they are parsed.  This only works for DTDs.",
35  	    Boolean.FALSE, 8.0f);
36      public static final BooleanProperty XINCLUDE_AWARE_DESCRIPTOR = new BooleanProperty("xincludeAware",
37  	    "Specifies that the XML parser will process XInclude markup.", Boolean.FALSE, 9.0f);
38      public static final BooleanProperty LOOKUP_DESCRIPTOR_DTD = new BooleanProperty("xincludeAware",
39      	    "Specifies whether XML parser will attempt to lookup the DTD.", Boolean.FALSE, 10.0f);
40      
41      public static final EntityResolver SILENT_ENTITY_RESOLVER = new EntityResolver() {
42  		public InputSource resolveEntity(String publicId, String systemId)
43  				throws SAXException, IOException {
44  			return new InputSource( new ByteArrayInputStream( "".getBytes() ) );
45  		}
46  	};
47  
48      private boolean coalescing;
49      private boolean expandEntityReferences;
50      private boolean ignoringComments;
51      private boolean ignoringElementContentWhitespace;
52      private boolean namespaceAware;
53      private boolean validating;
54      private boolean xincludeAware;
55      private boolean lookupDescriptorDoc;
56  
57      public XmlParserOptions() {
58  	this.coalescing = COALESCING_DESCRIPTOR.defaultValue().booleanValue();
59  	this.expandEntityReferences = EXPAND_ENTITY_REFERENCES_DESCRIPTOR.defaultValue().booleanValue();
60  	this.ignoringComments = IGNORING_COMMENTS_DESCRIPTOR.defaultValue().booleanValue();
61  	this.ignoringElementContentWhitespace = IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR.defaultValue()
62  		.booleanValue();
63  	this.namespaceAware = NAMESPACE_AWARE_DESCRIPTOR.defaultValue().booleanValue();
64  	this.validating = VALIDATING_DESCRIPTOR.defaultValue().booleanValue();
65  	this.xincludeAware = XINCLUDE_AWARE_DESCRIPTOR.defaultValue().booleanValue();
66  	this.lookupDescriptorDoc = LOOKUP_DESCRIPTOR_DTD.defaultValue().booleanValue();
67      }
68  
69      public XmlParserOptions(Rule rule) {
70  	this.coalescing = rule.getProperty(COALESCING_DESCRIPTOR);
71  	this.expandEntityReferences = rule.getProperty(EXPAND_ENTITY_REFERENCES_DESCRIPTOR);
72  	this.ignoringComments = rule.getProperty(IGNORING_COMMENTS_DESCRIPTOR);
73  	this.ignoringElementContentWhitespace = rule.getProperty(IGNORING_ELEMENT_CONTENT_WHITESPACE_DESCRIPTOR);
74  	this.namespaceAware = rule.getProperty(NAMESPACE_AWARE_DESCRIPTOR);
75  	this.validating = rule.getProperty(VALIDATING_DESCRIPTOR);
76  	this.xincludeAware = rule.getProperty(XINCLUDE_AWARE_DESCRIPTOR);
77  	this.lookupDescriptorDoc = rule.getProperty(LOOKUP_DESCRIPTOR_DTD);
78      }
79      
80      /**
81       * 
82       * @return the configured entity resolver. If {@link #lookupDescriptorDoc} is false
83       * it would normally force the XML parser to use its own resolver
84       */
85      public EntityResolver getEntityResolver(){
86      	if (!lookupDescriptorDoc)
87      		return SILENT_ENTITY_RESOLVER;
88      	else 
89      		return null;
90      }
91  
92      public boolean isLookupDescriptorDoc() {
93  		return lookupDescriptorDoc;
94  	}
95  
96  	public void setLookupDescriptorDoc(boolean lookupDescriptorDoc) {
97  		this.lookupDescriptorDoc = lookupDescriptorDoc;
98  	}
99  
100 	public boolean isCoalescing() {
101 	return this.coalescing;
102     }
103 
104     public void setCoalescing(boolean coalescing) {
105         this.coalescing = coalescing;
106     }
107 
108     public boolean isExpandEntityReferences() {
109 	return this.expandEntityReferences;
110     }
111 
112     public void setExpandEntityReferences(boolean expandEntityReferences) {
113         this.expandEntityReferences = expandEntityReferences;
114     }
115 
116     public boolean isIgnoringComments() {
117 	return this.ignoringComments;
118     }
119 
120     public void setIgnoringComments(boolean ignoringComments) {
121         this.ignoringComments = ignoringComments;
122     }
123 
124     public boolean isIgnoringElementContentWhitespace() {
125 	return this.ignoringElementContentWhitespace;
126     }
127 
128     public void setIgnoringElementContentWhitespace(boolean ignoringElementContentWhitespace) {
129         this.ignoringElementContentWhitespace = ignoringElementContentWhitespace;
130     }
131 
132     public boolean isNamespaceAware() {
133 	return this.namespaceAware;
134     }
135 
136     public void setNamespaceAware(boolean namespaceAware) {
137         this.namespaceAware = namespaceAware;
138     }
139 
140     public boolean isValidating() {
141 	return this.validating;
142     }
143 
144     public void setValidating(boolean validating) {
145         this.validating = validating;
146     }
147 
148     public boolean isXincludeAware() {
149 	return this.xincludeAware;
150     }
151 
152     public void setXincludeAware(boolean xincludeAware) {
153         this.xincludeAware = xincludeAware;
154     }
155 
156     @Override
157     public int hashCode() {
158 	final int prime = 31;
159 	int result = super.hashCode();
160 	result = prime * result + (coalescing ? 1231 : 1237);
161 	result = prime * result + (expandEntityReferences ? 1231 : 1237);
162 	result = prime * result + (ignoringComments ? 1231 : 1237);
163 	result = prime * result + (ignoringElementContentWhitespace ? 1231 : 1237);
164 	result = prime * result + (namespaceAware ? 1231 : 1237);
165 	result = prime * result + (validating ? 1231 : 1237);
166 	result = prime * result + (xincludeAware ? 1231 : 1237);
167 	return result;
168     }
169 
170     @Override
171     public boolean equals(Object obj) {
172 	if (this == obj) {
173 	    return true;
174 	}
175 	if (obj == null || getClass() != obj.getClass()) {
176 	    return false;
177 	}
178 	final XmlParserOptions that = (XmlParserOptions) obj;
179 	return StringUtil.isSame(this.suppressMarker, that.suppressMarker, false, false, false)
180 		&& this.coalescing == that.coalescing && this.expandEntityReferences == that.expandEntityReferences
181 		&& this.ignoringComments == that.ignoringComments
182 		&& this.ignoringElementContentWhitespace == that.ignoringElementContentWhitespace
183 		&& this.namespaceAware == that.namespaceAware && this.validating == that.validating
184 		&& this.xincludeAware == that.xincludeAware;
185     }
186 }