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 | 40x 8x 40x 40x 40x 40x 3x 2x 34x 34x 34x 34x 34x 34x 34x 34x 34x 33x 34x 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 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 1x | import * as vscode from 'vscode';
import { Store } from '@faubulous/mentor-rdf';
import { RdfSyntax } from '@faubulous/mentor-rdf-parsers';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { DocumentContext } from '@src/services/document/document-context';
import { WorkspaceUri } from '@src/providers/workspace-uri';
import { XmlParseResult } from '@src/languages/xml/xml-types';
import { getIriFromPrefixedName } from '@src/utilities';
/**
* A document context for RDF/XML documents.
*/
export class XmlDocument extends DocumentContext {
readonly syntax: RdfSyntax;
private _inferenceExecuted = false;
private get store() {
return container.resolve<Store>(ServiceToken.Store);
}
/**
* 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[] = [];
constructor(uri: vscode.Uri) {
super(uri);
this.syntax = RdfSyntax.RdfXml;
}
get isLoaded(): boolean {
return this._hasContent && this.graphs.length > 0;
}
/**
* Indicates whether parsed content has been received from the language server.
*/
get hasTokens(): boolean {
return this._hasContent;
}
/**
* Set the parsed data from the language server.
* @param data The parsed document data.
*/
setParsedData(data: XmlParseResult): void {
this.baseIri = data.baseIri;
this.namespaces = data.namespaces;
this.namespaceDefinitions = data.namespaceDefinitions;
this.subjects = data.subjects;
this.references = data.references;
this.typeAssertions = data.typeAssertions;
this.typeDefinitions = 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 if (Evalue.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> {
try {
const graphUri = WorkspaceUri.toCanonicalString(this.graphIri);
// 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);
// Reset inference flag: loadFromXmlStream clears both the source graph and
// the inference graph, so inference must re-run after each reload.
this._inferenceExecuted = false;
// Load triples from the RDF/XML content into the store.
await this.store.loadFromXmlStream(data, graphUri, false);
} catch (e) {
// This is not a critical error because the graph might be invalid.
console.error('Failed to load triples from RDF/XML:', e);
}
}
} |