-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraphfactory.js
208 lines (184 loc) · 3.85 KB
/
graphfactory.js
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
/*
* Copyright (c) 2016-present, IBM Research
* Licensed under The MIT License [see LICENSE for details]
*/
"use strict";
const TriGGraph = require("./triggraph");
const RDFGraph = require("./rdfgraph");
const JSONGraph = require("./jsongraph");
const Constants = require("./constants");
const RDFSParser = require("rdfxml-streaming-parser");
const N3 = require("n3");
const { Store, Parser, Writer } = N3;
function createGraph(mimeType, store = false)
{
let out = null;
switch(mimeType)
{
case "application/json":
return new JSONGraph();
case "application/n-triples":
case "application/n-quads":
case "application/trig":
case "application/turtle":
case "text/turtle":
return new TriGGraph(undefined, undefined, mimeType, store);
case "application/rdf+xml":
return new RDFGraph(undefined, undefined, mimeType);
}
return out;
}
function parseGraph(inputData, mimeType, callback)
{
let out = null;
switch(mimeType)
{
case "application/json":
if(typeof inputData === "object")
{
callback(null, new JSONGraph(inputData));
}
else if(typeof inputData === "string")
{
try
{
let json = JSON.parse(inputData);
callback(null, new JSONGraph(json));
}
catch(exp)
{
callback(exp);
}
}
else
{
callback(`Invalid input data ${typeof inputData}`)
}
break;
case "application/n-triples":
case "application/n-quads":
case "application/trig":
case "application/turtle":
case "text/turtle":
n3Parse(inputData, mimeType, callback);
break;
case "application/rdf+xml":
rdfStreamParse(inputData, mimeType, callback);
break;
}
return out;
}
function serializeGraph(aGraph, callback)
{
let out = null;
let mimeType = aGraph.mimeType;
switch(mimeType)
{
case "application/json":
callback(null, JSON.stringify(aGraph.triples, null, " "));
break;
case "application/n-triples":
case "application/n-quads":
case "application/trig":
case "application/turtle":
case "text/turtle":
n3Serialize(aGraph, callback);
break;
case "application/rdf+xml":
rdfXmlSerialize(aGraph, callback);
break;
default:
callback(`The mimeType ${mimeType} is not currently supported for serialization.`);
}
return out;
}
function rdfStreamParse(inputData, mimeType, callback)
{
const parser = new RDFSParser.RdfXmlParser(
{
baseIRI: Constants.HK_NULL
}
);
parser.write(inputData);
parser.end();
let graph = [];
parser
.on('data', (data) => graph.push(data))
.on('error', console.error)
.on('end', () => {
callback(null, new RDFGraph(graph, `${Constants.HK_NULL}`, mimeType));
});
}
function n3Parse(inputData, mimeType, callback)
{
const parser = new Parser();
let store = new Store();
try
{
parser.parse(inputData, (err, quad) =>
{
if(quad)
{
store.addQuad(quad);
}
else if(!err)
{
let triggraph = new TriGGraph(store, `${Constants.HK_NULL}`, mimeType, true);
callback(null, triggraph);
}
else
{
callback(err);
}
});
}
catch(exp)
{
callback(exp);
}
}
function n3Serialize(aGraph, callback)
{
let graph = aGraph.graph
if (aGraph.store)
{
const writer = new Writer({format: aGraph.mimeType});
writer.addQuads(aGraph.graph.getQuads(null, null,null,null))
graph = writer;
}
_end(graph, callback);
}
function rdfXmlSerialize(aGraph, callback)
{
let graph = aGraph.graph;
if (aGraph.store || Array.isArray(aGraph.graph))
{
const writer = new Writer({format: aGraph.mimeType});
aGraph.graph.forEach ((statement) =>
{
if(statement)
{
writer.addQuad(statement);
}
});
graph = writer;
}
_end(graph, callback);
}
function _end(graphOrWriter, callback)
{
graphOrWriter.end((err, data) =>
{
if(!err)
{
callback(null, data);
}
else
{
callback(err);
}
});
}
exports.createGraph = createGraph;
exports.parseGraph = parseGraph;
exports.serializeGraph = serializeGraph;