@@ -7,18 +7,9 @@ import { parse } from 'yaml';
7
7
import axios from 'axios' ;
8
8
const jsonld = require ( 'jsonld' ) ;
9
9
const ShaclValidator = require ( 'schemarama/shaclValidator' ) . Validator ;
10
- const slugify = require ( 'slugify' ) ;
11
-
12
- import { rawListeners , report } from 'process' ;
13
- import { RDFArrayRemove , output } from 'rdflib/lib/utils-js' ;
14
10
import { GraphType } from 'rdflib/lib/types' ;
15
11
import { DefaultGraph } from 'rdflib/lib/tf-types' ;
16
- import { isSubject } from 'rdflib' ;
17
-
18
- // New Oxigraph JS RDF library (built from rust)
19
-
20
12
import oxigraph from 'oxigraph/node.js' ;
21
- // const oxigraph = require('oxigraph');
22
13
23
14
const STRICT_NQUADS_REGEX = / ( < \S + ?> | _ : \S + ) ? \s + ( < \S + ?> ) \s + ( < \S + ?> | _ : \S + ?| ( " .* " ( ^ ^ < .+ > ) ? ) ) \s + ( < \S + ?> | _ : \S + ?) \s * \. ( \s * # .+ ) ? / g;
24
15
@@ -99,7 +90,7 @@ function loadRDFrdflib(data: string, rdfStore: $rdf.Store, mediaType: string) {
99
90
try {
100
91
101
92
if ( mediaType == "application/ld+json" ) {
102
- console . log ( "Converting to JSON-LD to NQuads to preserve named graphs" ) ;
93
+ outputChannel . appendLine ( "Converting from JSON-LD to NQuads to preserve named graphs" ) ;
103
94
JSONLDtoNQuads ( data )
104
95
. then ( nquads => $rdf . parse ( nquads , rdfStore , "https://example.com/test/" , "application/n-quads" , function ( ) {
105
96
outputChannel . appendLine ( "Successfully parsed: Statements in the graph: " + rdfStore . length ) ;
@@ -155,11 +146,10 @@ function loadRDFOxigraph(data: string, rdfStore: oxigraph.Store, mediaType: stri
155
146
try {
156
147
157
148
if ( mediaType == "application/ld+json" ) {
158
- console . log ( "Converting to JSON-LD to NQuads to preserve named graphs" ) ;
149
+ outputChannel . appendLine ( "Converting to JSON-LD to NQuads to preserve named graphs" ) ;
159
150
JSONLDtoNQuads ( data )
160
151
. then ( nquads => {
161
152
rdfStore . load ( nquads , mediaType , undefined , undefined ) ;
162
- console . log ( rdfStore ) ;
163
153
outputChannel . appendLine ( "Successfully parsed: Statements in the graph: " + rdfStore . size ) ;
164
154
resolve ( rdfStore ) ;
165
155
} ) . catch ( ( reason ) => {
@@ -169,7 +159,6 @@ function loadRDFOxigraph(data: string, rdfStore: oxigraph.Store, mediaType: stri
169
159
} ) ;
170
160
} else {
171
161
rdfStore . load ( data , mediaType , undefined , undefined ) ;
172
- console . log ( rdfStore ) ;
173
162
outputChannel . appendLine ( "Successfully parsed: Statements in the graph: " + rdfStore . size ) ;
174
163
resolve ( rdfStore ) ;
175
164
}
@@ -191,7 +180,7 @@ async function runQuery(query: string, documentText: string, mediaType: string):
191
180
} ) . catch ( ( reason ) => {
192
181
return [ reason ] ;
193
182
} ) . finally ( ( ) => {
194
- console . log ( "Finally... ") ;
183
+ outputChannel . appendLine ( "Ran query ") ;
195
184
} ) ;
196
185
197
186
return result ;
@@ -214,7 +203,7 @@ async function getView(documentText: string, mediaType: string, showTypes: boole
214
203
} ) . catch ( ( reason ) => {
215
204
result = { status : false , message : reason , mediaType : "" } ;
216
205
} ) . finally ( ( ) => {
217
- console . log ( "Finally... ") ;
206
+ outputChannel . appendLine ( "D3 Graph built ") ;
218
207
} ) ;
219
208
return result ;
220
209
}
@@ -553,7 +542,6 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
553
542
if ( context . constructor === Array ) {
554
543
let expandedContextArray :any [ ] = [ ] ;
555
544
for ( let c in context ) {
556
- console . log ( context [ c ] ) ;
557
545
// If the context is a string, and does not start with http, it is likely to be a file.
558
546
// Let's try to load it.
559
547
if ( typeof context [ c ] == 'string' && loadLocalContexts && ! context [ c ] . toLowerCase ( ) . startsWith ( "http" ) ) {
@@ -562,14 +550,13 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
562
550
expandedContextArray . push ( JSON . parse ( json_context_doc . getText ( ) ) ) ;
563
551
outputChannel . appendLine ( `Including context from ${ json_context_path } ` ) ;
564
552
} else if ( typeof context [ c ] == 'string' && prefetchRemoteContexts && context [ c ] . toLowerCase ( ) . startsWith ( "http" ) ) {
565
- console . log ( "Fetching context from URL " + context [ c ] ) ;
553
+ outputChannel . appendLine ( "Fetching context from URL: " + context [ c ] ) ;
566
554
try {
567
555
const response = await axios ( {
568
556
method : 'get' ,
569
557
url : context [ c ] ,
570
558
headers : { Accept : "application/ld+json;profile=http://www.w3.org/ns/json-ld#context" }
571
559
} )
572
- console . log ( response ) ;
573
560
574
561
if ( response . headers [ 'content-type' ] != 'application/json' && response . headers [ 'content-type' ] != 'text/json' && response . headers [ 'content-type' ] != 'application/ld+json' ) {
575
562
throw new Error ( "Service did not return JSON content type: " + response . headers [ 'content-type' ] ) ;
@@ -588,7 +575,6 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
588
575
} catch ( e : any ) {
589
576
outputChannel . appendLine ( "Could not preload context from " + context [ c ] ) ;
590
577
outputChannel . appendLine ( e . message ) ;
591
- console . log ( e ) ;
592
578
if ( ! gracefullyIgnoreFailedPrefetch ) {
593
579
expandedContextArray . push ( context [ c ] ) ;
594
580
} else {
@@ -597,7 +583,7 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
597
583
598
584
}
599
585
} else if ( typeof context [ c ] == 'string' && context [ c ] . toLowerCase ( ) . startsWith ( "http" ) ) {
600
- console . log ( "Adding proxy" ) ;
586
+ // TODO: add ability to inject proxy URL here
601
587
expandedContextArray . push ( context [ c ] ) ;
602
588
} else {
603
589
// Just passing through the value in the context array, as it may be an object or another array.
@@ -838,24 +824,22 @@ export function activate(context: vscode.ExtensionContext) {
838
824
let shaclFileURIs = await vscode . window . showOpenDialog ( options ) ;
839
825
if ( ! shaclFileURIs ) {
840
826
throw Error ( "No file selected!" ) ;
841
- }
827
+ }
842
828
shaclFileURI = shaclFileURIs [ 0 ] ;
843
829
}
844
830
845
-
831
+ outputChannel . appendLine ( `Loading SHACL shapes from ${ shaclFileURI } ` ) ;
846
832
const shaclDocument = await vscode . workspace . openTextDocument ( shaclFileURI ) ;
847
833
const result = await validate ( document , shaclDocument ) ;
848
834
849
- console . log ( result ) ;
850
835
// No failures? File is valid!
851
836
if ( result . failures . length == 0 ) {
852
- console . log ( "File is valid" ) ;
853
837
outputChannel . appendLine ( "Congratulations, your graph is valid!" ) ;
854
838
return ;
855
839
}
856
840
// If we do have failures, let's show them.
857
- console . log ( "File is not valid" ) ;
858
- vscode . window . showWarningMessage ( " Alas, your graph is not valid." ) ;
841
+ outputChannel . appendLine ( `Alas, your graph does not conform to the SHACL shapes defined in ${ shaclFileURI } ` ) ;
842
+ vscode . window . showWarningMessage ( ` Alas, your graph does not conform to the SHACL shapes defined in ${ shaclFileURI } ` ) ;
859
843
860
844
const variables = [ "node" , "message" , "shape" , "property" , "severity" ] ;
861
845
const data = result . failures ;
@@ -1155,22 +1139,26 @@ async function validate(document: vscode.TextDocument, shaclDocument: vscode.Tex
1155
1139
let store = new $rdf . Store ( ) ;
1156
1140
let shapesStore = new $rdf . Store ( ) ;
1157
1141
1142
+ outputChannel . appendLine ( "Loading data into graph" ) ;
1158
1143
await loadRDFrdflib ( doc . data , store , doc . fromMediaType ) ;
1159
1144
let data = await serializeRDF ( store , 'application/ld+json' ) ;
1160
1145
1146
+ outputChannel . appendLine ( "Loading SHACL shape data into graph" ) ;
1161
1147
await loadRDFrdflib ( shapes . data , shapesStore , shapes . fromMediaType ) ;
1162
1148
let shapesData = await serializeRDF ( shapesStore , 'text/turtle' ) ;
1163
1149
1164
1150
try {
1151
+ outputChannel . appendLine ( "Validating data graph against shapes graph" ) ;
1165
1152
const validator = new ShaclValidator ( shapesData , { annotations : { "node" : "http://www.w3.org/ns/shacl#focusNode" , "label" : "http://www.w3.org/2000/01/rdf-schema#label" } } ) ;
1166
1153
let report = await validator . validate ( data , { baseUrl : "http://example.org/test" } ) ;
1154
+ outputChannel . appendLine ( "Validation completed" ) ;
1167
1155
return ( report ) ;
1168
1156
} catch ( e :any ) {
1169
1157
outputChannel . appendLine ( "SHACL Validator threw an error" ) ;
1170
1158
outputChannel . appendLine ( e . message ) ;
1171
1159
return { failures : [ { message : "SHACL Validator threw an error: " + e . message , shape : "N/A" , node : "N/A" , severity : "error" , property : "N/A" } ] } ;
1172
1160
}
1173
-
1161
+
1174
1162
}
1175
1163
1176
1164
0 commit comments