| 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 | 1×
1×
1×
901×
1×
7444×
876×
876×
6568×
876×
876×
5692×
1×
1255×
1×
6916×
6916×
2554×
6916×
6916×
6916×
1×
139×
139×
139×
278×
710×
988×
988×
139×
139×
7055×
7055×
139×
6916×
6916×
6916×
6916×
6916×
6916×
4523×
2393×
139×
1×
3×
3×
3×
1×
4×
4×
4×
1×
138×
138×
138×
138×
138×
625×
625×
6×
3×
3×
619×
619×
138×
138×
138×
1×
138×
1×
276×
276×
276×
901×
901×
625×
276×
1×
138×
138×
138×
138×
138×
138×
439×
439×
138×
138×
138×
2×
136×
136×
136×
136×
437×
437×
437×
437×
604×
604×
437×
437×
437×
4×
4×
437×
604×
604×
333×
604×
604×
437×
437×
437×
192×
437×
1×
138×
138×
138×
3725×
3725×
439×
138×
138×
136×
136×
136×
3719×
3719×
1336×
3719×
437×
1129×
525×
1129×
437×
3282×
136×
139×
139×
139×
2393×
2393×
1265×
2393×
2393×
2393×
2393×
2393×
139×
139×
78×
139×
| const Errors = require("./errors");
const DocUtils = require("./doc-utils");
function inRange(range, match) {
return range[0] <= match.offset && match.offset < range[1];
}
function updateInTextTag(part, inTextTag) {
if (part.type === "tag" && part.position === "start" && part.text) {
Iif (inTextTag) {
throw new Error("Malformed xml : Already in text tag");
}
return true;
}
if (part.type === "tag" && part.position === "end" && part.text) {
Iif (!inTextTag) {
throw new Error("Malformed xml : Already not in text tag");
}
return false;
}
return inTextTag;
}
function offsetSort(a, b) {
return a.offset - b.offset;
}
function getTag(tag) {
let start = 1;
if (tag[1] === "/") {
start = 2;
}
const index = tag.indexOf(" ");
const end = index === -1 ? tag.length - 1 : index;
return {
tag: tag.slice(start, end),
position: start === 1 ? "start" : "end",
};
}
function tagMatcher(content, textMatchArray, othersMatchArray) {
let cursor = 0;
const contentLength = content.length;
const allMatches = DocUtils.concatArrays(
[
textMatchArray.map(function (tag) {
return {tag, text: true};
}),
othersMatchArray.map(function (tag) {
return {tag, text: false};
}),
]
).reduce(function (allMatches, t) {
allMatches[t.tag] = t.text;
return allMatches;
}, {});
const totalMatches = [];
while (cursor < contentLength) {
cursor = content.indexOf("<", cursor);
if (cursor === -1) {
break;
}
const offset = cursor;
cursor = content.indexOf(">", cursor);
const tagText = content.slice(offset, cursor + 1);
const {tag, position} = getTag(tagText);
const text = allMatches[tag];
if (text == null) {
continue;
}
totalMatches.push({type: "tag", position, text, offset, value: tagText});
}
return totalMatches;
}
function getUnopenedTagException(options) {
const err = new Errors.XTTemplateError("Unopened tag");
err.properties = {
xtag: options.xtag.split(" ")[0],
id: "unopened_tag",
context: options.xtag,
explanation: `The tag beginning with '${options.xtag.substr(0, 10)}' is unopened`,
};
return err;
}
function getUnclosedTagException(options) {
const err = new Errors.XTTemplateError("Unclosed tag");
err.properties = {
xtag: options.xtag.split(" ")[0].substr(1),
id: "unclosed_tag",
context: options.xtag,
explanation: `The tag beginning with '${options.xtag.substr(0, 10)}' is unclosed`,
};
return err;
}
function getDelimiterErrors(delimiterMatches, fullText) {
const errors = [];
let inDelimiter = false;
let lastDelimiterMatch = {offset: 0};
let xtag;
delimiterMatches.forEach(function (delimiterMatch) {
xtag = fullText.substr(lastDelimiterMatch.offset, delimiterMatch.offset - lastDelimiterMatch.offset);
if ((delimiterMatch.position === "start" && inDelimiter) || (delimiterMatch.position === "end" && !inDelimiter)) {
if (delimiterMatch.position === "start") {
errors.push(getUnclosedTagException({xtag}));
}
else {
errors.push(getUnopenedTagException({xtag}));
}
}
else {
inDelimiter = !inDelimiter;
lastDelimiterMatch = delimiterMatch;
}
});
const delimiterMatch = {offset: fullText.length};
xtag = fullText.substr(lastDelimiterMatch.offset, delimiterMatch.offset - lastDelimiterMatch.offset);
if (inDelimiter) {
errors.push(getUnclosedTagException({xtag}));
}
return errors;
}
function getAllIndexes(arr, val, position) {
const indexes = [];
let offset = -1;
do {
offset = arr.indexOf(val, offset + 1);
if (offset !== -1) {
indexes.push({offset, position});
}
} while (offset !== -1);
return indexes;
}
function Reader(innerContentParts) {
this.innerContentParts = innerContentParts;
this.full = "";
this.parseDelimiters = (delimiters) => {
this.full = this.innerContentParts.join("");
let offset = 0;
this.ranges = this.innerContentParts.map(function (part) {
offset += part.length;
return offset - part.length;
});
const delimiterMatches = DocUtils.concatArrays([getAllIndexes(this.full, delimiters.start, "start"), getAllIndexes(this.full, delimiters.end, "end")]).sort(offsetSort);
const errors = getDelimiterErrors(delimiterMatches, this.full);
if (errors.length > 0) {
Errors.throwMultiError(errors);
}
const delimiterLength = {start: delimiters.start.length, end: delimiters.end.length};
let cutNext = 0;
let delimiterIndex = 0;
this.parsed = this.ranges.map(function (offset, i) {
const range = [offset, offset + this.innerContentParts[i].length];
const partContent = this.innerContentParts[i];
const delimitersInOffset = [];
while (delimiterIndex < delimiterMatches.length && inRange(range, delimiterMatches[delimiterIndex])) {
delimitersInOffset.push(delimiterMatches[delimiterIndex]);
delimiterIndex++;
}
const parts = [];
let cursor = 0;
if(cutNext > 0) {
cursor = cutNext;
cutNext = 0;
}
delimitersInOffset.forEach(function (delimiterInOffset) {
const value = partContent.substr(cursor, delimiterInOffset.offset - offset - cursor);
if (value.length > 0) {
parts.push({type: "content", value});
}
parts.push({type: "delimiter", position: delimiterInOffset.position});
cursor = delimiterInOffset.offset - offset + delimiterLength[delimiterInOffset.position];
});
cutNext = cursor - partContent.length;
const value = partContent.substr(cursor);
if (value.length > 0) {
parts.push({type: "content", value});
}
return parts;
}, this);
};
}
module.exports = {
parse(xmlparsed, delimiters) {
let inTextTag = false;
const innerContentParts = [];
xmlparsed.forEach(function (part) {
inTextTag = updateInTextTag(part, inTextTag);
if(inTextTag && part.type === "content") {
innerContentParts.push(part.value);
}
});
const reader = new Reader(innerContentParts);
reader.parseDelimiters(delimiters);
const newArray = [];
let index = 0;
xmlparsed.forEach(function (part) {
inTextTag = updateInTextTag(part, inTextTag);
if (part.type === "content") {
part.position = inTextTag ? "insidetag" : "outsidetag";
}
if(inTextTag && part.type === "content") {
Array.prototype.push.apply(newArray, reader.parsed[index].map(function (p) {
if (p.type === "content") {
p.position = "insidetag";
}
return p;
}));
index++;
}
else {
newArray.push(part);
}
});
return newArray;
},
xmlparse(content, xmltags) {
const matches = tagMatcher(content, xmltags.text, xmltags.other);
let cursor = 0;
const parsed = matches.reduce(function (parsed, match) {
const value = content.substr(cursor, match.offset - cursor);
if (value.length > 0) {
parsed.push({type: "content", value});
}
cursor = match.offset + match.value.length;
delete match.offset;
Eif(match.value.length > 0) {
parsed.push(match);
}
return parsed;
}, []);
const value = content.substr(cursor);
if (value.length > 0) {
parsed.push({type: "content", value});
}
return parsed;
},
};
|