All files / languages/xml xml-document.ts

99.05% Statements 105/106
89.28% Branches 50/56
100% Functions 19/19
99.02% Lines 102/103

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                                        46x         46x         46x                 3x       2x       2x       46x   46x       5x   5x   5x     5x               7x               38x 38x   38x 38x 38x 38x 38x 38x           38x 37x     38x       8x 2x 2x   2x 1x   1x     6x 3x 3x 3x   3x 1x   2x           4x   4x 1x     3x   3x 1x     2x   2x 1x   1x     1x   1x 1x   1x         3x   3x 1x     2x 2x         2x 1x     1x 1x                         3x       3x 2x 2x     2x 2x                               4x       4x 2x 2x   2x 2x                                 5x       5x 3x 3x   3x 2x                             3x       3x 2x 2x   2x 1x                 9x   9x 6x   6x                     6x   6x     6x       6x 6x   6x           6x   6x 10x       1x     6x   6x 4x     6x    
import * as vscode from 'vscode';
import { LanguageTagUsageStats, Store, VocabularyRepository } from '@faubulous/mentor-rdf';
import { IToken, RdfSyntax } from '@faubulous/mentor-rdf-parsers';
import { ISettingsService } from '@src/services/core';
import { DocumentContext } from '@src/services/document/document-context';
import { ITokenizedDocumentContext } from '@src/services/document/document-context.interface';
import { RangeIndex } from '@src/utilities/range-index';
import { WorkspaceUri } from '@src/providers/workspace-uri';
import { XmlParseResult } from '@src/languages/xml/xml-types';
import { XmlParser } from '@src/languages/xml/xml-parser';
import { getIriFromPrefixedName, getTextFingerprint } from '@src/utilities';
import { addLanguageTagUsage, replaceGraphQuads } from '@src/utilities/rdf';
import { getLog } from '@src/utilities/vscode/log';
 
/**
 * A document context for RDF/XML documents.
 */
export class XmlDocument extends DocumentContext {
	readonly syntax: RdfSyntax;
 
	private _inferenceExecuted = false;
 
	/**
	 * Indicates whether parsed data has been received from the language server.
	 */
	private _hasContent = false;
 
	/**
	 * The ranges where text literals appear in the XML document.
	 */
	private _textLiterals: vscode.Range[] = [];
 
	/**
	 * The fingerprint of the text that was last parsed without error, or `undefined`. Lets
	 * the diagnostics pass recognize a text that is known to parse without keeping it.
	 */
	private _parsedTextFingerprint: string | undefined;
 
	get isLoaded(): boolean {
		return this._hasContent && this.graphs.length > 0;
	}
 
	get isParsed(): boolean {
		return this._hasContent;
	}
 
	isTokenized(): this is ITokenizedDocumentContext {
		return false;
	}
 
	constructor(uri: vscode.Uri, store: Store, vocabulary: VocabularyRepository, settings: ISettingsService) {
		super(uri, store, vocabulary, settings);
 
		this.syntax = RdfSyntax.RdfXml;
	}
 
	parse(text: string): IToken[] {
		this._parsedTextFingerprint = undefined;
 
		this.setParsedData(new XmlParser().parse(text));
 
		this._parsedTextFingerprint = getTextFingerprint(text);
 
		// RDF/XML documents are parsed structurally, not tokenized.
		return [];
	}
 
	/**
	 * Indicates whether the given text is the text that was last parsed without error.
	 * @param text A text.
	 */
	isParsedFrom(text: string): boolean {
		return this._parsedTextFingerprint !== undefined && this._parsedTextFingerprint === getTextFingerprint(text);
	}
 
	/**
	 * Set the parsed data of the document and update the derived indexes.
	 * @param data The parsed document data.
	 */
	setParsedData(data: XmlParseResult): void {
		this.baseIri = data.baseIri;
		this.namespaces = data.namespaces;
		// The parser result is transient; the context keeps the ranges packed.
		this.namespaceDefinitions = RangeIndex.fromRecord(data.namespaceDefinitions);
		this.subjects = RangeIndex.fromRecord(data.subjects);
		this.references = RangeIndex.fromRecord(data.references);
		this.typeAssertions = RangeIndex.fromRecord(data.typeAssertions);
		this.typeDefinitions = RangeIndex.fromRecord(data.typeDefinitions);
		this._textLiterals = data.textLiteralRanges.map(r => new vscode.Range(
			new vscode.Position(r.start.line, r.start.character),
			new vscode.Position(r.end.line, r.end.character)
		));
 
		// The xml namespace is implicitly defined in RDF/XML.
		if (!this.namespaces['xml']) {
			this.namespaces['xml'] = 'http://www.w3.org/XML/1998/namespace#';
		}
 
		this._hasContent = true;
	}
 
	getIriFromXmlString(value: string): string | undefined {
		if (value.startsWith('&')) {
			const prefix = value.trim().split(';')[0].substring(1);
			const namespaceIri = this.namespaces[prefix];
 
			if (namespaceIri) {
				const localName = value.split(';')[1];
 
				return namespaceIri + localName;
			}
		}
		else if (value.startsWith('#') || !value.includes(':')) {
			return this.baseIri + value;
		} else Eif (value.length > 0) {
			const schemeOrPrefix = value.split(':')[0];
 
			if (this.namespaces[schemeOrPrefix]) {
				return this.namespaces[schemeOrPrefix] + value.split(':')[1];
			} else {
				return value;
			}
		}
	}
 
	override getIriAtPosition(position: { line: number, character: number }): string | undefined {
		const document = this.getTextDocument();
 
		if (!document) {
			return;
		}
 
		const line = document.lineAt(position.line).text;
 
		if (!line) {
			return;
		}
 
		const prefixRange = this.getPrefixedNameRangeAtPosition(line, position);
 
		if (prefixRange) {
			const name = document.getText(prefixRange);
 
			return getIriFromPrefixedName(this.namespaces, name);
		}
 
		const valueRange = this.getAttributeValueRangeAtPosition(line, position);
 
		Eif (valueRange) {
			const value = document.getText(valueRange);
 
			return this.getIriFromXmlString(value);
		}
	}
 
	override getLiteralAtPosition(position: vscode.Position): string | undefined {
		const document = this.getTextDocument();
 
		if (!document) {
			return;
		}
 
		for (const range of this._textLiterals) {
			const r = new vscode.Range(
				new vscode.Position(range.start.line, range.start.character),
				new vscode.Position(range.end.line, range.end.character),
			);
 
			if (r.contains(position)) {
				return document.getText(r);
			}
 
			Eif (r.start.line > position.line) {
				return undefined;
			}
		}
	}
 
	/**
	 * Get the (prefixed) name of an attribute in the XML document at the given position.
	 * @param line A line of text in the XML document.
	 * @param position The position where to look for the attribute value or name; should be within the attribute name or value.
	 * @returns The range in the document where the attribute name is found, 1-based for use with the vscode.TextDocument class.
	 */
	getAttributeNameRangeNearPosition(line: string, position: { line: number, character: number }): vscode.Range | undefined {
		// Match an entire XML attribute (e.g., xml:example="Example")
		const attributeNameExpression = /(([a-zA-Z_][\w.-]*:)?[a-zA-Z_][\w.-]*)=["']([^"']+)["']/g;
 
		let match: RegExpExecArray | null;
 
		while ((match = attributeNameExpression.exec(line)) !== null) {
			const start = match.index;
			const end = start + match[1].length;
 
			// Check if the position is within the range of the entire attribute.
			Eif (position.character >= start && position.character <= match.index + match[0].length) {
				return new vscode.Range(
					new vscode.Position(position.line, start),
					new vscode.Position(position.line, end)
				);
			}
		}
	}
 
	/**
	 * Get the value of a quoted string in the XML document at the given position without quotation marks.
	 * @param line A line of text in the XML document.
	 * @param position The position where to look for the attribute value.
	 * @returns The range in the document where the attribute value is found if the position is within the value, `undefined` otherwise.
	 */
	getAttributeValueRangeAtPosition(line: string, position: { line: number, character: number }): vscode.Range | undefined {
		// Match quoted values (e.g., "http://example.org/C1_Test")
		const quotedValueExpression = /["']([^"']+)["']/g;
 
		let match: RegExpExecArray | null;
 
		while ((match = quotedValueExpression.exec(line)) !== null) {
			const start = match.index + 1;
			const end = start + match[1].length;
 
			Eif (position.character >= start && position.character <= end) {
				return new vscode.Range(
					new vscode.Position(position.line, start),
					new vscode.Position(position.line, end)
				);
			}
		}
	}
 
	/**
	 * Get the range of a prefixed name (e.g. rdfs:label) in the XML document at the given position.
	 * @param line A line of text in the XML document.
	 * @param position The position where to look for the attribute value.
	 * @param namespaces The namespaces defined in the document.
	 * @returns The range in the document where the prefixed name is found if the position is within the name, `undefined` otherwise.
	 */
	getPrefixedNameRangeAtPosition(line: string, position: { line: number, character: number }): vscode.Range | undefined {
		// Match namespace-prefixed attributes (e.g., xml:lang)
		const regex = /[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*/g;
 
		let match: RegExpExecArray | null;
 
		while ((match = regex.exec(line)) !== null) {
			const start = match.index;
			const end = start + match[0].length;
 
			if (position.character >= start && position.character <= end) {
				return new vscode.Range(
					new vscode.Position(position.line, start),
					new vscode.Position(position.line, end)
				);
			}
		}
	}
 
	/**
	 * Get the range of an XML entity name at the given position in the XML document.
	 * @param line A line of text in the XML document.
	 * @param position The position where to look for the entity name.
	 * @returns The range in the document where the entity name is found if the position is within the entity name, `undefined` otherwise.
	 */
	getEntityRangeAtPosition(line: string, position: { line: number, character: number }): vscode.Range | undefined {
		const regex = /ENTITY ([a-zA-Z_][\w.-]*)/g;
 
		let match: RegExpExecArray | null;
 
		while ((match = regex.exec(line)) !== null) {
			const start = match.index + 7;
			const end = start + match[1].length;
 
			if (position.character >= start && position.character <= end) {
				return new vscode.Range(
					new vscode.Position(position.line, start),
					new vscode.Position(position.line, end)
				);
			}
		}
	}
 
	override async infer(): Promise<void> {
		const reasoner = this._store.reasoner;
 
		if (reasoner && !this._inferenceExecuted) {
			this._inferenceExecuted = true;
 
			this._store.executeInference(WorkspaceUri.toCanonicalString(this.graphIri));
		}
	}
 
	/**
	 * Loads triples into the triple store.
	 * This method assumes parsed data has already been set via setParsedData().
	 * @param data The file content.
	 */
	override async loadTriples(data: string): Promise<void> {
		// Counted while the quads are added, instead of in a separate scan of the graph.
		const languageTagStats: LanguageTagUsageStats = {};
 
		const graphUri = WorkspaceUri.toCanonicalString(this.graphIri);
 
		// The graphs of a previous graph IRI, e.g. before a notebook cell got its slug.
		const previousGraphs = this.graphs.filter(uri => uri !== graphUri);
 
		// Initialize the graphs *before* trying to load the document so
		// that they are initialized even when loading the document fails.
		this.graphs.length = 0;
		this.graphs.push(graphUri);
 
		Iif (previousGraphs.length > 0) {
			this._store.deleteGraphs(previousGraphs, true);
		}
 
		// The RDF/XML parser writes into a store: parse into a separate one, so that
		// only the triples that changed are applied to the graph of the document.
		const parsed = new Store();
 
		try {
			await parsed.loadFromXmlStream(data, graphUri, false, true, quad => addLanguageTagUsage(languageTagStats, quad.object));
		} catch (e) {
			// This is not a critical error because the graph might be invalid. The
			// triples read before the error are loaded, as before.
			getLog().error('Failed to load triples from RDF/XML:', e);
		}
 
		const g = this._store.dataFactory.namedNode(graphUri);
 
		if (replaceGraphQuads(this._store, g, Array.from(parsed.matchQuads(null, null, null, g)))) {
			this._inferenceExecuted = false;
		}
 
		this.languageTagStats = languageTagStats;
	}
}