All files / languages/xml xml-parser.ts

96.29% Statements 130/135
75.67% Branches 56/74
100% Functions 11/11
96.94% Lines 127/131

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341        37x 37x 37x 37x 37x 37x       37x                           25x 25x 25x                     25x     25x     25x   25x                     25x         25x   25x 18x     7x 7x 7x     7x     7x 7x 7x     7x 7x 7x     7x 7x 7x   7x   7x 7x     7x               25x 152x     152x   152x 1x       152x     152x 59x 59x 59x   59x   59x 59x   59x                   25x 25x 25x   25x 152x     152x     152x 55x 55x   55x 55x 55x   55x 25x   25x             55x   55x 38x   38x 3x 3x 3x           152x     152x 9x   9x 3x   3x   3x   3x     3x     3x 3x             3x               152x     152x 31x 31x     31x 31x         31x   31x 31x   31x 31x     31x   31x 31x 31x 31x       31x 25x   25x 10x                   31x 7x 7x   7x 7x 7x     24x 1x 23x 23x   23x     23x           122x 122x                             55x   55x       55x 30x             30x       25x       25x   25x             10x     15x      
import { Range } from 'vscode-languageserver-types';
import { XmlParseResult } from './xml-types';
 
// Inline namespace constants to keep this module dependency-free.
const _RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const _RDFS = 'http://www.w3.org/2000/01/rdf-schema#';
const _OWL = 'http://www.w3.org/2002/07/owl#';
const _SH = 'http://www.w3.org/ns/shacl#';
const _SKOS = 'http://www.w3.org/2004/02/skos/core#';
const _SKOS_XL = 'http://www.w3.org/2008/05/skos-xl#';
 
// XML NCName character class per the XML Namespaces 1.0 spec:
// letters, digits, hyphens, periods, underscores (and Unicode ranges, approximated by \w).
const _NC_NAME = '[\\w\\-.]';
 
/**
 * A parser for RDF/XML documents that produces an index of namespaces,
 * subjects, references and type assertions using a line-based analysis.
 */
export class XmlParser {
	/**
	 * Parses the content of an RDF/XML document.
	 * @param data The raw XML document text.
	 * @returns The parsed content data.
	 */
	parse(data: string): XmlParseResult {
		// Comments are not markup: commented-out elements must not be indexed as resources.
		const markup = this._blankOutComments(data);
		const lines = markup.split('\n');
		const result: XmlParseResult = {
			namespaces: {},
			namespaceDefinitions: {},
			subjects: {},
			references: {},
			typeAssertions: {},
			typeDefinitions: {},
			textLiteralRanges: []
		};
 
		// Parse DOCTYPE for entity definitions
		this._parseDoctypeEntities(markup, result);
 
		// Parse namespace definitions and xml:base
		this._parseNamespaces(lines, result);
 
		// Parse elements and attributes
		this._parseElements(lines, result);
 
		return result;
	}
 
	/**
	 * Replaces the XML comments in a document with spaces. The line breaks are kept, so that
	 * the ranges of the remaining markup are the same as in the document. A comment that is
	 * not closed extends to the end of the document.
	 * @param data The raw XML document text.
	 * @returns The document text without comments.
	 */
	private _blankOutComments(data: string): string {
		return data.replace(/<!--[\s\S]*?(-->|$)/g, comment => comment.replace(/[^\n]/g, ' '));
	}
 
	private _parseDoctypeEntities(data: string, result: XmlParseResult): void {
		// Match DOCTYPE section
		const doctype = data.match(/<!DOCTYPE[^>]*\[([^\]]*)\]>/s);
 
		if (!doctype) {
			return;
		}
 
		const lines = data.substring(0, data.indexOf(doctype[0]) + doctype[0].length).split('\n');
		const doctypeContent = doctype[1];
		const doctypeStartLine = lines.length - doctypeContent.split('\n').length;
 
		// Find ENTITY definitions
		const entityRegex = new RegExp(`<!ENTITY\\s+(${_NC_NAME}+)\\s+"([^"]+)">`, 'g');
		let match;
 
		while ((match = entityRegex.exec(doctypeContent)) !== null) {
			const prefix = match[1];
			const namespaceIri = match[2];
 
			// Find line number for this entity
			const beforeMatch = doctypeContent.substring(0, match.index);
			const lineOffset = beforeMatch.split('\n').length - 1;
			const lineNumber = doctypeStartLine + lineOffset;
 
			// Find column position
			const lineStart = beforeMatch.lastIndexOf('\n') + 1;
			const lineText = doctypeContent.substring(lineStart);
			const column = lineText.indexOf(prefix);
 
			result.namespaces[prefix] = namespaceIri;
 
			Eif (!result.namespaceDefinitions[prefix]) {
				result.namespaceDefinitions[prefix] = [];
			}
 
			result.namespaceDefinitions[prefix].push(Range.create(
				lineNumber, column,
				lineNumber, column + prefix.length
			));
		}
	}
 
	private _parseNamespaces(lines: string[], result: XmlParseResult): void {
		for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
			const line = lines[lineNumber];
 
			// Check for xml:base attribute
			const base = line.match(/xml:base\s*=\s*["']([^"']+)["']/i);
 
			if (base) {
				result.baseIri = base[1];
			}
 
			// Find xmlns definitions
			const xmlnsRegex = new RegExp(`xmlns:(${_NC_NAME}+)\\s*=\\s*["']([^"']+)["']`, 'gi');
			let nsMatch;
 
			while ((nsMatch = xmlnsRegex.exec(line)) !== null) {
				const prefix = nsMatch[1];
				const namespaceIri = nsMatch[2];
				const column = nsMatch.index + 6; // "xmlns:" length
 
				result.namespaces[prefix] = namespaceIri;
 
				Eif (!result.namespaceDefinitions[prefix]) {
					result.namespaceDefinitions[prefix] = [];
				}
				result.namespaceDefinitions[prefix].push(Range.create(
					lineNumber, column,
					lineNumber, column + prefix.length
				));
			}
		}
	}
 
	private _parseElements(lines: string[], result: XmlParseResult): void {
		// Track element positions for text literal detection
		let inElement = false;
		let elementEndLine = -1;
		let elementEndColumn = -1;
 
		for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
			const line = lines[lineNumber];
 
			// Find opening tags with prefixes (e.g., <owl:Class, <rdf:Property)
			const tagRegex = new RegExp(`<(${_NC_NAME}+):(${_NC_NAME}+)`, 'g');
			let tag;
 
			while ((tag = tagRegex.exec(line)) !== null) {
				const prefix = tag[1];
				const namespaceIri = result.namespaces[prefix];
 
				const localName = tag[2];
				const fullName = `${prefix}:${localName}`;
				const column = tag.index + 1; // After '<'
 
				if (namespaceIri && !this._isXmlSpecificTagName(prefix, localName, namespaceIri)) {
					const iri = namespaceIri + localName;
 
					this._addRangeToIndex(result.references, iri, Range.create(
						lineNumber, column,
						lineNumber, column + fullName.length
					));
				}
 
				// Track element end for text literal detection
				const tagEnd = line.indexOf('>', tag.index);
 
				if (tagEnd !== -1) {
					const isSelfClosing = line[tagEnd - 1] === '/';
 
					if (!isSelfClosing) {
						inElement = true;
						elementEndLine = lineNumber;
						elementEndColumn = tagEnd + 1;
					}
				}
			}
 
			// Find rdf:about, rdf:resource, rdf:datatype attributes
			this._parseRdfAttributes(line, lineNumber, result);
 
			// Track text content for literal ranges
			if (inElement) {
				const closeTagMatch = line.match(new RegExp(`<\\/(${_NC_NAME}+:${_NC_NAME}+)>`));
 
				if (closeTagMatch) {
					const textEndColumn = line.indexOf(closeTagMatch[0]);
 
					Eif (textEndColumn > elementEndColumn || lineNumber > elementEndLine) {
						// Check if there's actual text content
						let hasText = false;
 
						Iif (lineNumber === elementEndLine) {
							hasText = line.substring(elementEndColumn, textEndColumn).trim().length > 0;
						} else {
							hasText = true; // Multi-line content, assume text exists
						}
 
						Eif (hasText) {
							result.textLiteralRanges.push(Range.create(
								elementEndLine, elementEndColumn,
								lineNumber, textEndColumn
							));
						}
					}
 
					inElement = false;
				}
			}
		}
	}
 
	private _parseRdfAttributes(line: string, lineNumber: number, result: XmlParseResult): void {
		// Match rdf:about, rdf:resource, rdf:datatype attributes
		const attrRegex = /rdf:(about|resource|datatype)\s*=\s*["']([^"']+)["']/gi;
		let attrMatch;
 
		while ((attrMatch = attrRegex.exec(line)) !== null) {
			const attrName = attrMatch[1].toLowerCase();
			const attrValue = attrMatch[2];
 
			// Find the column where the value starts (inside quotes)
			const valueStart = line.indexOf(attrValue, attrMatch.index);
			const range = Range.create(
				lineNumber, valueStart,
				lineNumber, valueStart + attrValue.length
			);
 
			const iri = this._getIriFromXmlString(attrValue, result.namespaces, result.baseIri);
 
			Eif (iri) {
				this._addRangeToIndex(result.references, iri, range);
 
				Eif (attrName === 'about') {
					this._addRangeToIndex(result.subjects, iri, range);
 
					// Check if this is a typed subject (not rdf:Description)
					const tag = line.match(new RegExp(`<(${_NC_NAME}+):(${_NC_NAME}+)`));
 
					Eif (tag) {
						const tagPrefix = tag[1];
						const tagLocal = tag[2];
						const tagNamespace = result.namespaces[tagPrefix];
 
						// If the tag is not rdf:Description, we treat the subject as 
						// having the type of the tag name.
						if (tagNamespace !== _RDF && tagLocal !== 'Description') {
							this._addRangeToIndex(result.typeAssertions, iri, range);
 
							if (this._isDefinitionNamespace(tagNamespace)) {
								this._addRangeToIndex(result.typeDefinitions, iri, range);
							}
						}
					}
				}
			}
		}
	}
 
	private _getIriFromXmlString(value: string, namespaces: { [key: string]: string }, baseIri?: string): string | undefined {
		if (value.startsWith('&')) {
			const prefix = value.trim().split(';')[0].substring(1);
			const namespaceIri = namespaces[prefix];
 
			Eif (namespaceIri) {
				const localName = value.split(';')[1];
				return namespaceIri + localName;
			}
		}
		else if (value.startsWith('#') || !value.includes(':')) {
			return baseIri ? baseIri + value : value;
		} else Eif (value.length > 0) {
			const schemeOrPrefix = value.split(':')[0];
 
			Iif (namespaces[schemeOrPrefix]) {
				return namespaces[schemeOrPrefix] + value.split(':')[1];
			} else {
				return value;
			}
		}
	}
 
	private _addRangeToIndex(index: { [key: string]: Range[] }, iri: string, range: Range): void {
		if (!index[iri]) {
			index[iri] = [range];
		} else E{
			index[iri].push(range);
		}
	}
 
	/**
	 * Indicates whether an element name belongs to the XML or RDF/XML syntax itself, such as
	 * `rdf:RDF` or `rdf:Description`, rather than denoting a resource. XML names are
	 * case-sensitive, so the names are compared as written.
	 * @param prefix The prefix of the element name.
	 * @param localName The local name of the element name.
	 * @param namespaceIri The namespace IRI the prefix is bound to.
	 */
	private _isXmlSpecificTagName(prefix: string, localName: string, namespaceIri: string): boolean {
		const _XML = 'http://www.w3.org/XML/1998/namespace';
 
		Iif (namespaceIri === _XML) {
			return true;
		}
 
		if (namespaceIri === _RDF) {
			switch (localName) {
				case 'about':
				case 'RDF':
				case 'resource':
				case 'Description':
				case 'datatype':
				case 'parseType':
					return true;
			}
		}
 
		return false;
	}
 
	private _isDefinitionNamespace(namespaceIri: string | undefined): boolean {
		Iif (!namespaceIri) return false;
 
		switch (namespaceIri) {
			case _RDF:
			case _RDFS:
			case _OWL:
			case _SKOS:
			case _SKOS_XL:
			case _SH:
				return true;
		}
 
		return false;
	}
}