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 pathjson2xml.ts
More file actions
273 lines (231 loc) · 6.73 KB
/
json2xml.ts
File metadata and controls
273 lines (231 loc) · 6.73 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
import {DOMImplementation} from "@xmldom/xmldom";
import {omany, omel, OMOBJ, OMS, OMFOREIGN, OME, OMV, OMI, OMB, OMSTR, OMF, OMA, OMBIND, attvars, attvar, OMATTR, omattributes, OMR} from "../schema/openmath";
// a pseudo-document to create elements under
const implementation = new DOMImplementation();
const OPENMATH_NAMESPACE = "http://www.openmath.org/OpenMath";
const document = implementation.createDocument(OPENMATH_NAMESPACE, null, null);
// #region "Helper Functions"
/** helper function to create an element */
function makeElement<T extends omany>(
name: string,
obj?: T, properties?: (keyof T)[],
...childrenOrContent: (Node | String)[]
){
const element = document.createElementNS(OPENMATH_NAMESPACE, name);
if(typeof obj !== "undefined" && obj !== null){
const theProps = makeProps<T>(obj, ...properties);
for(var key in theProps){
setAttribute(element, key, theProps[key]);
}
}
if(childrenOrContent.length === 1 && typeof childrenOrContent[0] === 'string'){
element.textContent = childrenOrContent[0] as string;
} else {
childrenOrContent.forEach((node: Node) => {
element.appendChild(node);
});
}
return element;
}
function setAttribute(node: Element, name: string, value: string) {
node.setAttributeNS(OPENMATH_NAMESPACE, name, value);
}
/**
*
* @param obj Object to create properties from
* @param properties List of properties to read
*/
function makeProps<T extends omany>(obj: T, ...properties: (keyof T)[]) : {[key: string]: any} {
const theProps: {[key: string]: any} = {};
properties.forEach((p: keyof T) => {
const prop = obj[p];
if(typeof prop !== "undefined"){
theProps[p as string] = obj[p];
}
});
return theProps;
}
// #endregion
export function convert(json: omany): Element {
return convertomany(json);
}
function convertomany(json: omany) : Element {
switch(json.kind) {
case "OMOBJ":
return convertOMOBJ(json);
case "OMFOREIGN":
return convertOMFOREIGN(json);
default:
return convertomel(json);
}
}
function convertOMOBJ(json: OMOBJ) : Element {
const node = makeElement(
'OMOBJ', json,
['id'],
convertomel(json.object)
);
if(json['openmath'] === '2.0'){
setAttribute(node, 'version', '2.0');
}
return node;
}
function convertomel(json: omel): Element {
switch(json.kind){
case 'OMS':
return convertOMS(json);
case 'OMV':
return convertOMV(json);
case 'OMI':
return convertOMI(json);
case 'OMB':
return convertOMB(json);
case 'OMSTR':
return convertOMSTR(json);
case 'OMF':
return convertOMF(json);
case 'OMA':
return convertOMA(json);
case 'OMBIND':
return convertOMBIND(json);
case 'OME':
return convertOME(json);
case 'OMATTR':
return convertOMATTR(json);
case 'OMR':
return convertOMR(json);
}
}
function convertOMS(json: OMS) : Element {
return makeElement(
'OMS', json,
['id', 'cdbase', 'cd', 'name']
);
}
function convertOMV(json: OMV) : Element {
return makeElement(
'OMV', json,
['id', 'name']
);
}
function convertOMI(json: OMI) : Element {
// extract a numeric string
let number: string;
if(typeof json["integer"] !== "undefined"){
number = json["integer"].toString();
} else if(typeof json["hexadecimal"] !== "undefined"){
number = json["hexadecimal"].toString();
} else {
number = json["decimal"].toString();
}
return makeElement(
'OMI', json,
['id'],
document.createTextNode(number)
);
}
function convertOMB(json: OMB): Element {
// create a base64 string
let base64: string;
if(typeof json["bytes"] !== "undefined"){
base64 = Buffer.from(json["bytes"]).toString('base64');
} else {
base64 = json["base64"];
}
return makeElement(
'OMB', json,
['id'],
document.createTextNode(base64)
);
}
function convertOMSTR(json: OMSTR): Element {
return makeElement(
'OMSTR', json,
['id'],
document.createTextNode(json.string)
)
}
function convertOMF(json: OMF) : Element {
const omf = makeElement(
'OMF', json,
['id']
);
// set the numeric value
if(typeof json["float"] !== "undefined"){
setAttribute(omf, "dec", json["float"].toString());
} else if(typeof json["hexadecimal"] !== "undefined"){
setAttribute(omf, "hex", json["hexadecimal"].toString());
} else {
setAttribute(omf, "dec", json["decimal"].toString());
}
return omf;
}
function convertOMA(json: OMA): Element {
const applicant = convertomel(json.applicant);
const args = (json.arguments || []).map(convertomel);
return makeElement(
'OMA', json,
['id', 'cdbase'],
applicant, ...args
);
}
function convertOMBIND(json: OMBIND): Element {
const binder = convertomel(json.binder);
const ombvar = convertOMBVAR(json.variables);
const object = convertomel(json.object);
return makeElement(
'OMBIND', json,
['id', 'cdbase'],
binder, ombvar, object
);
}
function convertOMBVAR(json: (OMV | attvar)[]) : Element {
const children = json.map((e) => (e.kind === 'OMV') ? convertOMV(e) : convertOMATTR(e));
return makeElement(
'OMBVAR', undefined, undefined,
...children
);
}
function convertOME(json: OME) : Element {
const error = convertOMS(json.error);
const args = (json.arguments || []).map(convertomany);
return makeElement(
'OME', json,
['id'],
error, ...args
)
}
function convertOMATTR(json: OMATTR): Element {
const omatp = convertOMATP(json.attributes);
const obj = convertomel(json.object);
return makeElement(
'OMATTR', json,
['id', 'cdbase'],
omatp, obj
);
}
function convertOMATP(json: omattributes): Element {
let children: Node[] = [];
json.forEach((v: [OMS, omel|OMFOREIGN]) => {
children.push(convertOMS(v[0]));
children.push(convertomany(v[1]));
});
return makeElement(
'OMATP', null, null,
...children
);
}
function convertOMR(json: OMR): Element {
return makeElement(
'OMR', json,
['id', 'href']
);
}
function convertOMFOREIGN(json: OMFOREIGN): Element {
const foreign = document.createTextNode(json.foreign);
return makeElement(
'OMFOREIGN', json,
['id', 'encoding', 'cdbase'],
foreign
);
}