All files / languages/xml xml-language-server.ts

97.98% Statements 146/149
78.57% Branches 66/84
100% Functions 11/11
98.63% Lines 144/146

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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363                      1x 1x 1x 1x 1x 1x       1x       26x       4x 1x     3x   3x 3x   3x 3x 3x     2x           1x               3x       18x 18x 18x                     18x     18x     18x   18x         18x   18x 17x     1x 1x 1x     1x     1x 1x 1x     1x 1x 1x     1x 1x 1x   1x   1x 1x     1x               18x 106x     106x   106x 1x       106x     106x 49x 49x 49x   49x   49x 49x   49x                   18x 18x 18x   18x 106x     106x     106x 35x 35x   35x 35x 35x   35x 16x   16x       16x       16x             35x   35x 18x   18x 2x 2x 2x           106x     106x 4x   4x 2x   2x   2x   2x 1x   1x     2x 2x             2x               106x     106x 18x 18x     18x 18x         18x   18x 18x   18x 18x     18x   18x 18x 18x 18x       18x 17x   17x 15x                   18x 1x 1x   1x 1x 1x     17x 1x 16x 16x   16x 1x   15x           84x 83x   1x         35x   35x 1x     34x 18x             18x       16x       17x   17x             15x     2x      
import {
	Connection,
	Diagnostic,
	DiagnosticSeverity,
	Range,
} from 'vscode-languageserver/browser';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { LanguageServerBase } from '@src/languages/language-server';
import { XmlParseResult } from './xml-types';
 
// Inline namespace constants to avoid importing @faubulous/mentor-rdf which has CommonJS dependencies
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\\-.]';
 
export class XmlLanguageServer extends LanguageServerBase {
	constructor(connection: Connection) {
		super(connection, 'xml', 'RDF/XML');
	}
 
	override async validateTextDocument(document: TextDocument): Promise<void> {
		if (!this?.connection) {
			return;
		}
 
		this.log(`Validating document: ${document.uri}`);
 
		const content = document.getText();
		const diagnostics: Diagnostic[] = [];
 
		Eif (content.length) {
			try {
				const result = await this.parseXml(document);
 
				// Send the parsed data to the client
				this.connection.sendNotification('mentor.message.updateContext', {
					uri: document.uri,
					languageId: this.languageId,
					parsedData: result
				});
			} catch (e) {
				diagnostics.push({
					severity: DiagnosticSeverity.Error,
					message: e ? e.toString() : "An error occurred while parsing the document.",
					range: Range.create(0, 0, 0, 0)
				});
			}
		}
 
		this.connection.sendDiagnostics({ uri: document.uri, diagnostics });
	}
 
	protected async parseXml(document: TextDocument): Promise<XmlParseResult> {
		const data = document.getText();
		const lines = data.split('\n');
		const result: XmlParseResult = {
			namespaces: {},
			namespaceDefinitions: {},
			subjects: {},
			references: {},
			typeAssertions: {},
			typeDefinitions: {},
			textLiteralRanges: []
		};
 
		// Parse DOCTYPE for entity definitions
		this._parseDoctypeEntities(data, result);
 
		// Parse namespace definitions and xml:base
		this._parseNamespaces(lines, result);
 
		// Parse elements and attributes
		this._parseElements(lines, result);
 
		return result;
	}
 
	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].toLowerCase();
				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].toLowerCase();
				const namespaceIri = result.namespaces[prefix];
 
				const localName = tag[2].toLowerCase();
				const fullName = `${prefix}:${localName}`;
				const column = tag.index + 1; // After '<'
 
				if (namespaceIri && !this._isXmlSpecificTagName(prefix, localName, namespaceIri)) {
					const iri = namespaceIri + localName;
 
					Iif (namespaceIri === 'https://spec.industrialontologies.org/ontology/construct/') {
						console.log(`Found construct reference (${iri}) at line ${lineNumber}`);
					}
 
					Iif (iri === "https://spec.industrialontologies.org/ontology/construct/MeasurementCapability") {
						console.log("Found MeasurementCapability reference at line " + lineNumber);
					}
 
					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;
 
						if (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].toLowerCase();
						const tagLocal = tag[2].toLowerCase();
						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 if (Evalue.length > 0) {
			const schemeOrPrefix = value.split(':')[0];
 
			if (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 {
			index[iri].push(range);
		}
	}
 
	private _isXmlSpecificTagName(prefix: string, localName: string, namespaceIri: string): boolean {
		const _XML = 'http://www.w3.org/XML/1998/namespace';
 
		if (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;
	}
}