@@ -7,15 +7,17 @@ import semmle.python.Files
77/** An XML element that has a location. */
88abstract class XMLLocatable extends @xmllocatable {
99 /** The source location for this element. */
10- Location getLocation ( ) { xmllocations ( this , result ) }
10+ Location getLocation ( ) { xmllocations ( this , result ) }
1111
1212 /**
1313 * Whether this element has the specified location information,
1414 * including file path, start line, start column, end line and end column.
1515 */
16- predicate hasLocationInfo ( string filepath , int startline , int startcolumn , int endline , int endcolumn ) {
16+ predicate hasLocationInfo (
17+ string filepath , int startline , int startcolumn , int endline , int endcolumn
18+ ) {
1719 exists ( File f , Location l | l = this .getLocation ( ) |
18- locations_default ( l , f , startline , startcolumn , endline , endcolumn ) and
20+ locations_default ( l , f , startline , startcolumn , endline , endcolumn ) and
1921 filepath = f .getName ( )
2022 )
2123 }
@@ -29,7 +31,6 @@ abstract class XMLLocatable extends @xmllocatable {
2931 * both of which can contain other elements.
3032 */
3133class XMLParent extends @xmlparent {
32-
3334 XMLParent ( ) {
3435 // explicitly restrict `this` to be either an `XMLElement` or an `XMLFile`;
3536 // the type `@xmlparent` currently also includes non-XML files
@@ -43,124 +44,116 @@ class XMLParent extends @xmlparent {
4344 /*abstract*/ string getName ( ) { result = "parent" }
4445
4546 /** The file to which this XML parent belongs. */
46- XMLFile getFile ( ) { result = this or xmlElements ( this , _ , _ , _ , result ) }
47+ XMLFile getFile ( ) { result = this or xmlElements ( this , _ , _ , _ , result ) }
4748
4849 /** The child element at a specified index of this XML parent. */
4950 XMLElement getChild ( int index ) { xmlElements ( result , _, this , index , _) }
5051
5152 /** A child element of this XML parent. */
52- XMLElement getAChild ( ) { xmlElements ( result , _ , this , _ , _) }
53+ XMLElement getAChild ( ) { xmlElements ( result , _ , this , _ , _) }
5354
5455 /** A child element of this XML parent with the given `name`. */
55- XMLElement getAChild ( string name ) { xmlElements ( result , _ , this , _ , _) and result .hasName ( name ) }
56+ XMLElement getAChild ( string name ) { xmlElements ( result , _ , this , _ , _) and result .hasName ( name ) }
5657
5758 /** A comment that is a child of this XML parent. */
58- XMLComment getAComment ( ) { xmlComments ( result , _ , this , _) }
59+ XMLComment getAComment ( ) { xmlComments ( result , _ , this , _) }
5960
6061 /** A character sequence that is a child of this XML parent. */
61- XMLCharacters getACharactersSet ( ) { xmlChars ( result , _ , this , _ , _ , _ ) }
62+ XMLCharacters getACharactersSet ( ) { xmlChars ( result , _ , this , _ , _ , _ ) }
6263
6364 /** The depth in the tree. (Overridden in XMLElement.) */
6465 int getDepth ( ) { result = 0 }
6566
6667 /** The number of child XML elements of this XML parent. */
67- int getNumberOfChildren ( ) {
68- result = count ( XMLElement e | xmlElements ( e , _, this , _, _) )
69- }
68+ int getNumberOfChildren ( ) { result = count ( XMLElement e | xmlElements ( e , _, this , _, _) ) }
7069
7170 /** The number of places in the body of this XML parent where text occurs. */
72- int getNumberOfCharacterSets ( ) {
73- result = count ( int pos | xmlChars ( _, _, this , pos , _, _) )
74- }
71+ int getNumberOfCharacterSets ( ) { result = count ( int pos | xmlChars ( _, _, this , pos , _, _) ) }
7572
7673 /**
7774 * Append the character sequences of this XML parent from left to right, separated by a space,
7875 * up to a specified (zero-based) index.
7976 */
8077 string charsSetUpTo ( int n ) {
81- ( n = 0 and xmlChars ( _, result , this , 0 , _, _) ) or
82- ( n > 0 and exists ( string chars | xmlChars ( _, chars , this , n , _, _) |
83- result = this .charsSetUpTo ( n - 1 ) + " " + chars ) )
78+ n = 0 and xmlChars ( _, result , this , 0 , _, _)
79+ or
80+ n > 0 and
81+ exists ( string chars | xmlChars ( _, chars , this , n , _, _) |
82+ result = this .charsSetUpTo ( n - 1 ) + " " + chars
83+ )
8484 }
8585
8686 /** Append all the character sequences of this XML parent from left to right, separated by a space. */
8787 string allCharactersString ( ) {
8888 exists ( int n | n = this .getNumberOfCharacterSets ( ) |
89- ( n = 0 and result = "" ) or
90- ( n > 0 and result = this .charsSetUpTo ( n - 1 ) )
89+ n = 0 and result = ""
90+ or
91+ n > 0 and result = this .charsSetUpTo ( n - 1 )
9192 )
9293 }
9394
9495 /** The text value contained in this XML parent. */
95- string getTextValue ( ) {
96- result = allCharactersString ( )
97- }
96+ string getTextValue ( ) { result = allCharactersString ( ) }
9897
9998 /** A printable representation of this XML parent. */
10099 string toString ( ) { result = this .getName ( ) }
101100}
102101
103102/** An XML file. */
104103class XMLFile extends XMLParent , File {
105- XMLFile ( ) {
106- xmlEncoding ( this , _)
107- }
104+ XMLFile ( ) { xmlEncoding ( this , _) }
108105
109106 /** A printable representation of this XML file. */
110- override
111- string toString ( ) { result = XMLParent .super .toString ( ) }
107+ override string toString ( ) { result = XMLParent .super .toString ( ) }
112108
113109 /** The name of this XML file. */
114- override
115- string getName ( ) { files ( this , result , _, _, _) }
110+ override string getName ( ) { files ( this , result , _, _, _) }
116111
117112 /** The path of this XML file. */
118- string getPath ( ) { files ( this , _ , result , _ , _) }
113+ string getPath ( ) { files ( this , _ , result , _ , _) }
119114
120115 /** The path of the folder that contains this XML file. */
121116 string getFolder ( ) {
122- result = this .getPath ( ) .substring ( 0 , this .getPath ( ) .length ( ) - this .getName ( ) .length ( ) )
117+ result = this .getPath ( ) .substring ( 0 , this .getPath ( ) .length ( ) - this .getName ( ) .length ( ) )
123118 }
124119
125120 /** The encoding of this XML file. */
126- string getEncoding ( ) { xmlEncoding ( this , result ) }
121+ string getEncoding ( ) { xmlEncoding ( this , result ) }
127122
128123 /** The XML file itself. */
129- override
130- XMLFile getFile ( ) { result = this }
124+ override XMLFile getFile ( ) { result = this }
131125
132126 /** A top-most element in an XML file. */
133127 XMLElement getARootElement ( ) { result = this .getAChild ( ) }
134128
135129 /** A DTD associated with this XML file. */
136- XMLDTD getADTD ( ) { xmlDTDs ( result , _ , _ , _ , this ) }
130+ XMLDTD getADTD ( ) { xmlDTDs ( result , _ , _ , _ , this ) }
137131}
138132
139133/** A "Document Type Definition" of an XML file. */
140134class XMLDTD extends @xmldtd {
141135 /** The name of the root element of this DTD. */
142- string getRoot ( ) { xmlDTDs ( this , result , _ , _ , _) }
136+ string getRoot ( ) { xmlDTDs ( this , result , _ , _ , _) }
143137
144138 /** The public ID of this DTD. */
145- string getPublicId ( ) { xmlDTDs ( this , _ , result , _ , _) }
139+ string getPublicId ( ) { xmlDTDs ( this , _ , result , _ , _) }
146140
147141 /** The system ID of this DTD. */
148- string getSystemId ( ) { xmlDTDs ( this , _ , _ , result , _) }
142+ string getSystemId ( ) { xmlDTDs ( this , _ , _ , result , _) }
149143
150144 /** Whether this DTD is public. */
151- predicate isPublic ( ) { not xmlDTDs ( this , _ , "" , _ , _) }
145+ predicate isPublic ( ) { not xmlDTDs ( this , _ , "" , _ , _) }
152146
153147 /** The parent of this DTD. */
154- XMLParent getParent ( ) { xmlDTDs ( this , _ , _ , _ , result ) }
148+ XMLParent getParent ( ) { xmlDTDs ( this , _ , _ , _ , result ) }
155149
156150 /** A printable representation of this DTD. */
157151 string toString ( ) {
158- ( this .isPublic ( ) and result = this .getRoot ( ) + " PUBLIC '" +
159- this .getPublicId ( ) + "' '" +
160- this .getSystemId ( ) + "'" ) or
161- ( not this .isPublic ( ) and result = this .getRoot ( ) +
162- " SYSTEM '" +
163- this .getSystemId ( ) + "'" )
152+ this .isPublic ( ) and
153+ result = this .getRoot ( ) + " PUBLIC '" + this .getPublicId ( ) + "' '" + this .getSystemId ( ) + "'"
154+ or
155+ not this .isPublic ( ) and
156+ result = this .getRoot ( ) + " SYSTEM '" + this .getSystemId ( ) + "'"
164157 }
165158}
166159
@@ -170,71 +163,61 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
170163 predicate hasName ( string name ) { name = getName ( ) }
171164
172165 /** The name of this XML element. */
173- override
174- string getName ( ) { xmlElements ( this , result , _, _, _) }
166+ override string getName ( ) { xmlElements ( this , result , _, _, _) }
175167
176168 /** The XML file in which this XML element occurs. */
177- override
178- XMLFile getFile ( ) { xmlElements ( this , _, _, _, result ) }
169+ override XMLFile getFile ( ) { xmlElements ( this , _, _, _, result ) }
179170
180171 /** The parent of this XML element. */
181- XMLParent getParent ( ) { xmlElements ( this , _ , result , _ , _) }
172+ XMLParent getParent ( ) { xmlElements ( this , _ , result , _ , _) }
182173
183174 /** The index of this XML element among its parent's children. */
184175 int getIndex ( ) { xmlElements ( this , _, _, result , _) }
185176
186177 /** Whether this XML element has a namespace. */
187- predicate hasNamespace ( ) { xmlHasNs ( this , _ , _) }
178+ predicate hasNamespace ( ) { xmlHasNs ( this , _ , _) }
188179
189180 /** The namespace of this XML element, if any. */
190- XMLNamespace getNamespace ( ) { xmlHasNs ( this , result , _) }
181+ XMLNamespace getNamespace ( ) { xmlHasNs ( this , result , _) }
191182
192183 /** The index of this XML element among its parent's children. */
193- int getElementPositionIndex ( ) { xmlElements ( this , _ , _ , result , _) }
184+ int getElementPositionIndex ( ) { xmlElements ( this , _ , _ , result , _) }
194185
195186 /** The depth of this element within the XML file tree structure. */
196- override
197- int getDepth ( ) { result = this .getParent ( ) .getDepth ( ) + 1 }
187+ override int getDepth ( ) { result = this .getParent ( ) .getDepth ( ) + 1 }
198188
199189 /** An XML attribute of this XML element. */
200190 XMLAttribute getAnAttribute ( ) { result .getElement ( ) = this }
201191
202192 /** The attribute with the specified `name`, if any. */
203- XMLAttribute getAttribute ( string name ) {
204- result .getElement ( ) = this and result .getName ( ) = name
205- }
193+ XMLAttribute getAttribute ( string name ) { result .getElement ( ) = this and result .getName ( ) = name }
206194
207195 /** Whether this XML element has an attribute with the specified `name`. */
208- predicate hasAttribute ( string name ) {
209- exists ( XMLAttribute a | a = this .getAttribute ( name ) )
210- }
196+ predicate hasAttribute ( string name ) { exists ( XMLAttribute a | a = this .getAttribute ( name ) ) }
211197
212198 /** The value of the attribute with the specified `name`, if any. */
213- string getAttributeValue ( string name ) {
214- result = this .getAttribute ( name ) .getValue ( )
215- }
199+ string getAttributeValue ( string name ) { result = this .getAttribute ( name ) .getValue ( ) }
216200
217201 /** A printable representation of this XML element. */
218- override
219- string toString ( ) { result = XMLParent .super .toString ( ) }
202+ override string toString ( ) { result = XMLParent .super .toString ( ) }
220203}
221204
222205/** An attribute that occurs inside an XML element. */
223206class XMLAttribute extends @xmlattribute, XMLLocatable {
224207 /** The name of this attribute. */
225- string getName ( ) { xmlAttrs ( this , _ , result , _ , _ , _) }
208+ string getName ( ) { xmlAttrs ( this , _ , result , _ , _ , _) }
226209
227210 /** The XML element to which this attribute belongs. */
228- XMLElement getElement ( ) { xmlAttrs ( this , result , _ , _ , _ , _) }
211+ XMLElement getElement ( ) { xmlAttrs ( this , result , _ , _ , _ , _) }
229212
230213 /** Whether this attribute has a namespace. */
231- predicate hasNamespace ( ) { xmlHasNs ( this , _ , _) }
214+ predicate hasNamespace ( ) { xmlHasNs ( this , _ , _) }
232215
233216 /** The namespace of this attribute, if any. */
234- XMLNamespace getNamespace ( ) { xmlHasNs ( this , result , _) }
217+ XMLNamespace getNamespace ( ) { xmlHasNs ( this , result , _) }
235218
236219 /** The value of this attribute. */
237- string getValue ( ) { xmlAttrs ( this , _ , _ , result , _ , _) }
220+ string getValue ( ) { xmlAttrs ( this , _ , _ , result , _ , _) }
238221
239222 /** A printable representation of this XML attribute. */
240223 override string toString ( ) { result = this .getName ( ) + "=" + this .getValue ( ) }
@@ -243,28 +226,29 @@ class XMLAttribute extends @xmlattribute, XMLLocatable {
243226/** A namespace used in an XML file */
244227class XMLNamespace extends @xmlnamespace {
245228 /** The prefix of this namespace. */
246- string getPrefix ( ) { xmlNs ( this , result , _ , _) }
229+ string getPrefix ( ) { xmlNs ( this , result , _ , _) }
247230
248231 /** The URI of this namespace. */
249- string getURI ( ) { xmlNs ( this , _ , result , _) }
232+ string getURI ( ) { xmlNs ( this , _ , result , _) }
250233
251234 /** Whether this namespace has no prefix. */
252235 predicate isDefault ( ) { this .getPrefix ( ) = "" }
253236
254237 /** A printable representation of this XML namespace. */
255238 string toString ( ) {
256- ( this .isDefault ( ) and result = this .getURI ( ) ) or
257- ( not this .isDefault ( ) and result = this .getPrefix ( ) + ":" + this .getURI ( ) )
239+ this .isDefault ( ) and result = this .getURI ( )
240+ or
241+ not this .isDefault ( ) and result = this .getPrefix ( ) + ":" + this .getURI ( )
258242 }
259243}
260244
261245/** A comment of the form `<!-- ... -->` is an XML comment. */
262246class XMLComment extends @xmlcomment, XMLLocatable {
263247 /** The text content of this XML comment. */
264- string getText ( ) { xmlComments ( this , result , _ , _) }
248+ string getText ( ) { xmlComments ( this , result , _ , _) }
265249
266250 /** The parent of this XML comment. */
267- XMLParent getParent ( ) { xmlComments ( this , _ , result , _) }
251+ XMLParent getParent ( ) { xmlComments ( this , _ , result , _) }
268252
269253 /** A printable representation of this XML comment. */
270254 override string toString ( ) { result = this .getText ( ) }
@@ -276,13 +260,13 @@ class XMLComment extends @xmlcomment, XMLLocatable {
276260 */
277261class XMLCharacters extends @xmlcharacters, XMLLocatable {
278262 /** The content of this character sequence. */
279- string getCharacters ( ) { xmlChars ( this , result , _ , _ , _ , _) }
263+ string getCharacters ( ) { xmlChars ( this , result , _ , _ , _ , _) }
280264
281265 /** The parent of this character sequence. */
282- XMLParent getParent ( ) { xmlChars ( this , _ , result , _ , _ , _) }
266+ XMLParent getParent ( ) { xmlChars ( this , _ , result , _ , _ , _) }
283267
284268 /** Whether this character sequence is CDATA. */
285- predicate isCDATA ( ) { xmlChars ( this , _ , _ , _ , 1 , _) }
269+ predicate isCDATA ( ) { xmlChars ( this , _ , _ , _ , 1 , _) }
286270
287271 /** A printable representation of this XML character sequence. */
288272 override string toString ( ) { result = this .getCharacters ( ) }
0 commit comments