forked from lantanagroup/FHIR.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertToXml.js
214 lines (214 loc) · 9.28 KB
/
convertToXml.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
209
210
211
212
213
214
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConvertToXml = void 0;
const convert = require("xml-js");
const parseConformance_1 = require("./parseConformance");
const xmlHelper_1 = require("./xmlHelper");
const constants_1 = require("./constants");
class ConvertToXml {
constructor(parser) {
this.attributeProperties = {
'Extension': 'url'
};
this.parser = parser || new parseConformance_1.ParseConformance(true);
}
convert(obj) {
if (obj.hasOwnProperty('resourceType')) {
const xmlObj = this.resourceToXML(obj);
return convert.js2xml(xmlObj);
}
}
resourceToXML(obj, xmlObj) {
const resourceElement = {
type: 'element',
name: obj.resourceType,
attributes: {
xmlns: 'http://hl7.org/fhir'
},
elements: []
};
if (!xmlObj) {
xmlObj = {
declaration: {
attributes: {
version: '1.0',
encoding: 'UTF-8'
}
},
elements: [resourceElement]
};
}
if (!this.parser.parsedStructureDefinitions[obj.resourceType]) {
throw new Error('Unknown resource type: ' + obj.resourceType);
}
const properties = this.parser.parsedStructureDefinitions[obj.resourceType]._properties || [];
for (let property of properties) {
this.propertyToXML(resourceElement, this.parser.parsedStructureDefinitions[obj.resourceType], obj, property._name);
}
return xmlObj;
}
propertyToXML(parentXmlObj, parentType, obj, propertyName, parentPropertyType) {
const isAttribute = (propertyName === 'id' && !!parentPropertyType) || this.attributeProperties[parentPropertyType] === propertyName;
if (propertyName.startsWith('_'))
return;
const propertyType = parentType._properties.find(property => property._name == propertyName);
function xmlEscapeString(value) {
return value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\r/g, '
')
.replace(/\n/g, '
');
}
const pushProperty = (value, extra) => {
const nextXmlObj = {
type: 'element',
name: propertyName,
elements: [],
attributes: {}
};
if (extra) {
if (extra.id) {
nextXmlObj.attributes.id = extra.id;
}
if (extra.fhir_comments) {
for (let fhirComment of extra.fhir_comments) {
parentXmlObj.elements.push({ type: 'comment', comment: fhirComment });
}
}
if (extra.extension) {
const extensionStructure = this.parser.parsedStructureDefinitions['Extension'];
this.propertyToXML(nextXmlObj, extensionStructure, extra, 'extension');
}
}
switch (propertyType._type) {
case 'string':
case 'base64Binary':
case 'code':
case 'id':
case 'markdown':
case 'uri':
case 'url':
case 'canonical':
case 'oid':
case 'boolean':
case 'integer':
case 'decimal':
case 'unsignedInt':
case 'positiveInt':
case 'date':
case 'dateTime':
case 'time':
case 'instant':
const actual = !value || !(typeof value === 'string') ? value : xmlEscapeString(value);
nextXmlObj.attributes.value = actual;
break;
case 'xhtml':
if (propertyName === 'div' && value) {
let divXmlObj;
try {
divXmlObj = convert.xml2js(value);
divXmlObj = xmlHelper_1.XmlHelper.escapeInvalidCharacters(divXmlObj);
}
catch (ex) {
throw new Error('The embedded xhtml is not properly formatted/escaped: ' + ex.message);
}
if (divXmlObj.elements.length === 1 && divXmlObj.elements[0].name === 'div') {
divXmlObj.elements[0].attributes = divXmlObj.elements[0].attributes || {};
divXmlObj.elements[0].attributes.xmlns = 'http://www.w3.org/1999/xhtml';
nextXmlObj.elements = divXmlObj.elements[0].elements;
nextXmlObj.attributes = divXmlObj.elements[0].attributes;
}
}
break;
case 'Resource':
if (value) {
const resourceXmlObj = this.resourceToXML(value).elements[0];
delete resourceXmlObj.attributes.xmlns;
nextXmlObj.elements.push(resourceXmlObj);
}
break;
case 'Element':
case 'BackboneElement':
for (let x in propertyType._properties) {
const nextProperty = propertyType._properties[x];
this.propertyToXML(nextXmlObj, propertyType, value, nextProperty._name, propertyType._type);
}
break;
default:
let nextType = this.parser.parsedStructureDefinitions[propertyType._type];
if (propertyType._type.startsWith('#')) {
const typeSplit = propertyType._type.substring(1).split('.');
for (let i = 0; i < typeSplit.length; i++) {
if (i == 0) {
nextType = this.parser.parsedStructureDefinitions[typeSplit[i]];
}
else {
nextType = nextType._properties.find(nextTypeProperty => nextTypeProperty._name === typeSplit[i]);
}
if (!nextType) {
break;
}
}
}
if (!nextType) {
console.log('Could not find type ' + propertyType._type);
}
else {
for (let nextProperty of nextType._properties) {
this.propertyToXML(nextXmlObj, nextType, value, nextProperty._name, propertyType._type);
}
}
}
let hasAttributes = false;
let hasElements = nextXmlObj.elements && nextXmlObj.elements.length > 0;
if (nextXmlObj.attributes) {
for (let attrKey in nextXmlObj.attributes) {
if (nextXmlObj.attributes[attrKey] || nextXmlObj.attributes[attrKey] === false || nextXmlObj.attributes[attrKey] === 0) {
hasAttributes = true;
}
}
}
if (isAttribute && nextXmlObj.attributes && nextXmlObj.attributes.hasOwnProperty('value')) {
if (!parentXmlObj.attributes) {
parentXmlObj.attributes = {};
}
parentXmlObj.attributes[nextXmlObj.name] = nextXmlObj.attributes['value'];
}
else if (hasAttributes || hasElements) {
parentXmlObj.elements.push(nextXmlObj);
}
};
if (obj) {
let extra;
if (propertyType._multiple && obj[propertyName]) {
for (let i = 0; i < obj[propertyName].length; i++) {
if (constants_1.Constants.PrimitiveTypes.indexOf(propertyType._type) >= 0) {
if (obj['_' + propertyName]) {
extra = obj['_' + propertyName][i];
}
}
else {
extra = {
fhir_comments: obj[propertyName][i].fhir_comments
};
}
pushProperty(obj[propertyName][i], extra);
}
}
else {
if (constants_1.Constants.PrimitiveTypes.indexOf(propertyType._type) >= 0) {
extra = obj['_' + propertyName];
}
else if (obj[propertyName]) {
extra = {
fhir_comments: obj[propertyName].fhir_comments
};
}
pushProperty(obj[propertyName], extra);
}
}
}
}
exports.ConvertToXml = ConvertToXml;
//# sourceMappingURL=convertToXml.js.map