-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxmlWrite.js
144 lines (125 loc) · 2.9 KB
/
xmlWrite.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
'use strict';
var xml = '';
var hanging = '';
var followsElement = true;
var followsEndElement = false;
var depth = 0;
var pretty = 0;
var spacer = ' ';
function replaceAll(s, from, to) {
return s.split(from).join(to);
}
function encode(s) {
var es = s;
if (typeof s === 'string') { // might be a number, boolean or null
es = replaceAll(es, '&','&');
es = replaceAll(es, '<','<');
es = replaceAll(es, '>','>');
es = replaceAll(es, '"','"');
es = replaceAll(es, "'",''');
}
return es;
}
function startFragment(indent,indentStr) {
if (indent) {
pretty = indent;
if ((indentStr) && (indentStr !== '')) {
spacer = indentStr;
}
else {
spacer = ' ';
}
}
else {
pretty = 0;
}
followsElement = true;
followsEndElement = false;
xml = '';
hanging = '';
depth = 0;
}
module.exports = {
startFragment: startFragment,
startDocument : function (encoding,standalone,indent,indentStr) {
startFragment(indent,indentStr);
xml = '<?xml version="1.0" encoding="' + (encoding ? encoding : 'UTF-8') + '"' +
(standalone ? ' standalone="' + standalone + '"' : '') + ' ?>';
},
docType : function (s) {
xml += '<!DOCTYPE ' + s + '>';
},
startElement : function (s) {
xml += hanging;
if (s !== '') {
if ((pretty) && (followsElement || followsEndElement)) xml += '\n'+new Array(pretty*depth+1).join(spacer);
xml += '<' + s;
hanging = '>';
depth++;
followsElement = true;
}
else hanging = '';
},
emptyElement : function (s) {
xml += hanging + '<' + s;
hanging = '/>';
},
attribute : function (a,v) {
xml += ' ' + a + '="' + encode(v) + '"';
},
content : function (s) {
xml += hanging + encode(s);
hanging = '';
followsElement = false;
followsEndElement = false;
},
comment : function (s) {
xml += hanging + '<!--' + encode(s) + '-->';
hanging = '';
},
processingInstruction : function (s) {
xml += hanging;
if ((pretty) && (followsElement || followsEndElement)) xml += '\n'+new Array(pretty*depth+1).join(spacer);
xml += '<?' + encode(s) + '?>';
hanging = '';
},
cdata : function (s) {
xml += hanging;
if ((pretty) && (followsElement || followsEndElement)) xml += '\n'+new Array(pretty*depth+1).join(spacer);
xml += '<![CDATA[' + s + ']]>';
hanging = '';
},
fragment : function (s) {
xml += hanging + s;
hanging = '';
followsElement = false;
followsEndElement = true;
},
endElement : function (s) {
var suppress = false;
if (hanging == '>') {
hanging = '/>';
suppress = true;
}
xml += hanging;
if (s !== '') {
depth--;
if (!suppress) {
if (pretty && followsEndElement) xml += '\n'+new Array(pretty*depth+1).join(spacer);
xml += '</' + s + '>';
}
followsElement = false;
followsEndElement = true;
}
hanging = '';
},
endFragment : function () {
xml += hanging;
hanging = '';
return xml;
},
endDocument : function () {
//hanging is '' at this point
return xml;
}
};