This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxml2json.ts
More file actions
405 lines (341 loc) · 9.32 KB
/
xml2json.ts
File metadata and controls
405 lines (341 loc) · 9.32 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import {omany, omel, OMOBJ, OMS, OMFOREIGN, OME, OMV, OMI, OMB, OMSTR, OMF, OMA, OMBIND, attvars, attvar, OMATTR, omattributes, OMR} from "../schema/openmath";
import {XMLSerializer} from "@xmldom/xmldom";
// #region "Helper Functions"
/**
* Finishes conversion of node to a JSON result.
* Dangerous, the caller needs to ensure that all attributes are set.
*
* @param node XML node to convert
* @param result partial result
* @param attributes attributes to convert
*/
function makeJSON<T>(
node: Element, result: Partial<T>,
attributes: (any[] | (keyof T))[]
) : T {
attributes.forEach((args) => {
if(typeof args === "string"){
args = [args];
}
result = makeJSONAttribute.call(undefined, node, result, ...args as any[]);
});
// dangerous type-cohersion, needs to be checked by caller
return result as T;
}
/**
* Helper function to convert an attribute from xml to json
* @param node XML node to convert
* @param result partial result object
* @param xmlName name of XML attribute
* @param resultName optionally different name of result object attribute
* @param converter optional converter to parse the attribute
*/
function makeJSONAttribute<T extends omany, S extends keyof T>(
node: Element, result: Partial<T>,
xmlName: string, resultName?: S,
converter?: (string) => T[S]
): Partial<T> {
if(!resultName){
resultName = xmlName as S;
}
if(node.hasAttribute(xmlName)){
if(typeof converter === 'function'){
result[resultName] = converter(node.getAttribute(xmlName));
} else {
// assume that the converter is the identity
// which may be dangerous
result[resultName] = node.getAttribute(xmlName) as any;
}
}
return result as T;
}
/**
*
* @param node node to check
* @param type type to check
*/
function assertNodeType(node: Node, type: string) {
const name = node.nodeName.toLowerCase();
if(name !== type.toLowerCase()){
throw new Error("Expected node to be of type '" + type + "', but got '" + name + "'");
}
}
/** gets all the children of an element that are also an element */
function getChildElements(node: Element): Element[] {
return Array.prototype.filter.call(node.childNodes, c => c.nodeType === 1);
}
// #endregion
/** convert a valid OpenMath XML object into OpenMath JSON */
export function convert(xml: Element): omany {
return convertomany(xml);
}
function convertomany(node: Element): omany {
switch(node.nodeName.toUpperCase()){
case 'OMOBJ':
return convertOMOBJ(node);
case 'OMFOREIGN':
return convertOMFOREIGN(node);
default:
return convertomel(node);
}
}
function convertOMOBJ(node: Element): OMOBJ {
assertNodeType(node, 'OMOBJ');
const nodeChildren = getChildElements(node);
const omobj: Partial<OMOBJ> = {
kind: "OMOBJ",
object: convertomel(nodeChildren[0]),
};
return makeJSON(
node, omobj,
[
'id',
'cdbase',
['version', 'openmath', (version: string) => "2.0"]
]
);
}
function convertomel(node: Element): omel {
switch(node.nodeName.toUpperCase()){
case 'OMS':
return convertOMS(node);
case 'OMV':
return convertOMV(node);
case 'OMI':
return convertOMI(node);
case 'OMB':
return convertOMB(node);
case 'OMSTR':
return convertOMSTR(node);
case 'OMF':
return convertOMF(node);
case 'OMA':
return convertOMA(node);
case 'OMBIND':
return convertOMBIND(node);
case 'OME':
return convertOME(node);
case 'OMATTR':
return convertOMATTR(node);
case 'OMR':
return convertOMR(node);
default:
throw new Error('Invalid XML. ');
}
}
function convertOMS(node: Element): OMS {
assertNodeType(node, 'OMS');
const oms: Partial<OMS> = {
kind: "OMS",
};
return makeJSON(
node, oms,
[
'id',
'cdbase',
'cd',
'name'
]
);
}
function convertOMV(node: Element): OMV {
assertNodeType(node, 'OMV');
const omv: Partial<OMV> = {
kind: "OMV"
};
return makeJSON(
node, omv,
[
'id',
'name'
]
);
}
function convertOMI(node: Element): OMI {
assertNodeType(node, 'OMI');
const omi: Partial<OMI> = {
kind: "OMI"
};
const number = node.textContent;
// in case of a hexa-decimal valid keep it as is
if(number.startsWith('x') || number.startsWith('-x')){
omi["hexadecimal"] = number;
} else {
// if we have a parseable, finite number
// then turn it into a native js number
const numericNumber = Number(number);
if(!isNaN(numericNumber) && isFinite(numericNumber)){
omi["integer"] = numericNumber;
// else it is too big, so keep it as is
} else {
omi["decimal"] = number;
}
}
return makeJSON(
node, omi,
[ 'id' ]
);
}
function convertOMB(node: Element): OMB {
assertNodeType(node, 'OMB');
const omb: Partial<OMB> = {
kind: "OMB",
base64: node.textContent.trim(),
};
return makeJSON(
node, omb,
[ 'id' ]
);
}
function convertOMSTR(node: Element): OMSTR {
assertNodeType(node, 'OMSTR');
return {
kind: "OMSTR",
string: node.textContent
}
}
function convertOMF(node: Element): OMF {
assertNodeType(node, 'OMF');
const omf: Partial<OMF> = {
kind: "OMF"
};
// set the decimal attribute
if(node.hasAttribute('dec')){
omf['decimal'] = node.getAttribute('dec');
} else {
omf['hexadecimal'] = node.getAttribute('hex');
}
return makeJSON(
node, omf,
[
'id'
]
);
}
function convertOMA(node: Element): OMA {
assertNodeType(node, 'OMA');
const nodeChildren = getChildElements(node);
const oma: Partial<OMA> = {
kind: "OMA",
applicant: convertomel(nodeChildren[0])
};
const args = nodeChildren.slice(1).map(convertomel);
if(args.length > 0){
oma['arguments'] = args;
}
return makeJSON(
node, oma,
[
'id',
'cdbase'
]
);
}
function convertOMBIND(node: Element): OMBIND {
assertNodeType(node, 'OMBIND');
const nodeChildren = getChildElements(node);
const omb: Partial<OMBIND> = {
kind: "OMBIND",
binder: convertomel(nodeChildren[0]),
variables: convertOMBVAR(nodeChildren[1]),
object: convertomel(nodeChildren[2])
};
return makeJSON(
node, omb,
[
'id',
'cdbase'
]
);
}
function convertOMBVAR(node: Element): attvars {
assertNodeType(node, 'OMBVAR');
const nodeChildren = getChildElements(node);
// TODO: Take care of id cdbase properly
return nodeChildren.map(convertomvar);
}
function convertomvar(node: Element): (OMV | attvar) {
if(node.nodeName.toUpperCase() === 'OMV'){
return convertOMV(node);
}
return convertOMATTR(node) as attvar;
}
function convertOME(node: Element): OME {
assertNodeType(node, 'OME');
const nodeChildren = getChildElements(node);
const ome: Partial<OME> = {
kind: "OME",
error: convertOMS(nodeChildren[0])
};
const args = nodeChildren.slice(1).map(convertomany);
if(args.length > 0){
ome['arguments'] = args as (omel|OMFOREIGN)[];
}
return makeJSON(
node, ome,
[
'id'
]
);
}
function convertOMATTR(node: Element): OMATTR {
assertNodeType(node, 'OMATTR');
const nodeChildren = getChildElements(node);
const omattr: Partial<OMATTR> = {
kind: "OMATTR",
attributes: convertOMATP(nodeChildren[0]),
object: convertomel(nodeChildren[1])
}
return makeJSON(
node, omattr,
[
'id',
'cdbase'
]
)
}
function convertOMATP(node: Element): omattributes {
assertNodeType(node, 'OMATP');
let attributes: ([OMS, omel|OMFOREIGN])[] = [];
const nodeChildren = getChildElements(node);
for(let i = 0; i + 1 < nodeChildren.length; i = i + 2){
attributes.push(
[
convertOMS(nodeChildren[i]),
convertomany(nodeChildren[i + 1]) as (omel|OMFOREIGN)
]
)
}
// TODO: Take care of id, cdbase properly
return attributes;
}
function convertOMR(node: Element): OMR {
assertNodeType(node, 'OMR');
const omr: Partial<OMR> = {
kind: "OMR"
}
return makeJSON(
node, omr,
[
'id',
'href'
]
)
}
const serializer = new XMLSerializer();
function convertOMFOREIGN(node: Element): OMFOREIGN {
assertNodeType(node, 'OMFOREIGN');
// TODO: We should handle omels
const omforeign: Partial<OMFOREIGN> = {
kind: "OMFOREIGN",
foreign: serializer.serializeToString(node.childNodes[0])
}
return makeJSON(
node, omforeign,
[
'id',
'encoding',
'cdbase'
]
)
}