22 * Provides classes and predicates for working with XML files and their content.
33 */
44
5- import semmle.code.cpp.Location
5+ import semmle.files.FileSystem
66
77/** An XML element that has a location. */
88abstract class XMLLocatable extends @xmllocatable {
99 /** Gets the source location for this element. */
1010 Location getLocation ( ) { xmllocations ( this , result ) }
1111
1212 /**
13- * Holds if this element has the specified location information,
14- * including file path, start line, start column, end line and end column.
13+ * Holds if this element is at the specified location.
14+ * The location spans column `startcolumn` of line `startline` to
15+ * column `endcolumn` of line `endline` in file `filepath`.
16+ * For more information, see
17+ * [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
1518 */
1619 predicate hasLocationInfo (
1720 string filepath , int startline , int startcolumn , int endline , int endcolumn
1821 ) {
1922 exists ( File f , Location l | l = this .getLocation ( ) |
20- locations_default ( l , unresolveElement ( f ) , startline , startcolumn , endline , endcolumn ) and
23+ locations_default ( l , f , startline , startcolumn , endline , endcolumn ) and
2124 filepath = f .getAbsolutePath ( )
2225 )
2326 }
2427
25- /** Gets a printable representation of this element. */
28+ /** Gets a textual representation of this element. */
2629 abstract string toString ( ) ;
2730}
2831
@@ -112,15 +115,21 @@ class XMLFile extends XMLParent, File {
112115 override string toString ( ) { result = XMLParent .super .toString ( ) }
113116
114117 /** Gets the name of this XML file. */
115- override string getName ( ) { files ( this , result , _ , _ , _ ) }
118+ override string getName ( ) { result = File . super . getAbsolutePath ( ) }
116119
117- /** Gets the path of this XML file. */
118- string getPath ( ) { files ( this , _, result , _, _) }
120+ /**
121+ * DEPRECATED: Use `getAbsolutePath()` instead.
122+ *
123+ * Gets the path of this XML file.
124+ */
125+ deprecated string getPath ( ) { result = getAbsolutePath ( ) }
119126
120- /** Gets the path of the folder that contains this XML file. */
121- string getFolder ( ) {
122- result = this .getPath ( ) .substring ( 0 , this .getPath ( ) .length ( ) - this .getName ( ) .length ( ) )
123- }
127+ /**
128+ * DEPRECATED: Use `getParentContainer().getAbsolutePath()` instead.
129+ *
130+ * Gets the path of the folder that contains this XML file.
131+ */
132+ deprecated string getFolder ( ) { result = getParentContainer ( ) .getAbsolutePath ( ) }
124133
125134 /** Gets the encoding of this XML file. */
126135 string getEncoding ( ) { xmlEncoding ( this , result ) }
@@ -135,7 +144,17 @@ class XMLFile extends XMLParent, File {
135144 XMLDTD getADTD ( ) { xmlDTDs ( result , _, _, _, this ) }
136145}
137146
138- /** A "Document Type Definition" of an XML file. */
147+ /**
148+ * An XML document type definition (DTD).
149+ *
150+ * Example:
151+ *
152+ * ```
153+ * <!ELEMENT person (firstName, lastName?)>
154+ * <!ELEMENT firstName (#PCDATA)>
155+ * <!ELEMENT lastName (#PCDATA)>
156+ * ```
157+ */
139158class XMLDTD extends @xmldtd {
140159 /** Gets the name of the root element of this DTD. */
141160 string getRoot ( ) { xmlDTDs ( this , result , _, _, _) }
@@ -162,7 +181,17 @@ class XMLDTD extends @xmldtd {
162181 }
163182}
164183
165- /** An XML tag in an XML file. */
184+ /**
185+ * An XML element in an XML file.
186+ *
187+ * Example:
188+ *
189+ * ```
190+ * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
191+ * package="com.example.exampleapp" android:versionCode="1">
192+ * </manifest>
193+ * ```
194+ */
166195class XMLElement extends @xmlelement, XMLParent , XMLLocatable {
167196 /** Holds if this XML element has the given `name`. */
168197 predicate hasName ( string name ) { name = getName ( ) }
@@ -207,7 +236,16 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
207236 override string toString ( ) { result = XMLParent .super .toString ( ) }
208237}
209238
210- /** An attribute that occurs inside an XML element. */
239+ /**
240+ * An attribute that occurs inside an XML element.
241+ *
242+ * Examples:
243+ *
244+ * ```
245+ * package="com.example.exampleapp"
246+ * android:versionCode="1"
247+ * ```
248+ */
211249class XMLAttribute extends @xmlattribute, XMLLocatable {
212250 /** Gets the name of this attribute. */
213251 string getName ( ) { xmlAttrs ( this , _, result , _, _, _) }
@@ -228,7 +266,15 @@ class XMLAttribute extends @xmlattribute, XMLLocatable {
228266 override string toString ( ) { result = this .getName ( ) + "=" + this .getValue ( ) }
229267}
230268
231- /** A namespace used in an XML file */
269+ /**
270+ * A namespace used in an XML file.
271+ *
272+ * Example:
273+ *
274+ * ```
275+ * xmlns:android="http://schemas.android.com/apk/res/android"
276+ * ```
277+ */
232278class XMLNamespace extends @xmlnamespace {
233279 /** Gets the prefix of this namespace. */
234280 string getPrefix ( ) { xmlNs ( this , result , _, _) }
@@ -247,7 +293,15 @@ class XMLNamespace extends @xmlnamespace {
247293 }
248294}
249295
250- /** A comment of the form `<!-- ... -->` is an XML comment. */
296+ /**
297+ * A comment in an XML file.
298+ *
299+ * Example:
300+ *
301+ * ```
302+ * <!-- This is a comment. -->
303+ * ```
304+ */
251305class XMLComment extends @xmlcomment, XMLLocatable {
252306 /** Gets the text content of this XML comment. */
253307 string getText ( ) { xmlComments ( this , result , _, _) }
@@ -262,6 +316,12 @@ class XMLComment extends @xmlcomment, XMLLocatable {
262316/**
263317 * A sequence of characters that occurs between opening and
264318 * closing tags of an XML element, excluding other elements.
319+ *
320+ * Example:
321+ *
322+ * ```
323+ * <content>This is a sequence of characters.</content>
324+ * ```
265325 */
266326class XMLCharacters extends @xmlcharacters, XMLLocatable {
267327 /** Gets the content of this character sequence. */
0 commit comments