xml-disassembler 1.10.2 → 1.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/disassemble.log +506 -558
- package/dist/index.cjs +171 -157
- package/dist/index.cjs.map +1 -1
- package/dist/index.min.cjs +1 -1
- package/dist/index.min.cjs.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.min.umd.js +1 -1
- package/dist/index.min.umd.js.map +1 -1
- package/dist/index.mjs +173 -159
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +171 -157
- package/dist/index.umd.js.map +1 -1
- package/dist/typings/builders/buildLeafFile.d.ts +2 -1
- package/dist/typings/builders/strategies/grouped-by-tag/buildDisassembledFiles.d.ts +1 -1
- package/dist/typings/builders/strategies/uid/buildDisassembledFiles.d.ts +1 -1
- package/dist/typings/handlers/reassembleXMLFileHandler.d.ts +1 -1
- package/dist/typings/parsers/strategies/grouped-by-tag/parseElement.d.ts +1 -1
- package/dist/typings/parsers/strategies/uid/parseElement.d.ts +2 -2
- package/dist/typings/types/types.d.ts +1 -2
- package/package.json +1 -1
- package/dist/typings/builders/buildReassembledFiles.d.ts +0 -1
- package/dist/typings/builders/buildRootElementHeader.d.ts +0 -2
- package/dist/typings/parsers/parseRootElement.d.ts +0 -2
package/dist/index.cjs
CHANGED
|
@@ -60,34 +60,6 @@ const XML_PARSER_OPTION = {
|
|
|
60
60
|
};
|
|
61
61
|
const JSON_PARSER_OPTION = Object.assign(Object.assign({}, XML_PARSER_OPTION), { format: true, indentBy: INDENT, suppressBooleanAttributes: false, suppressEmptyNode: false });
|
|
62
62
|
|
|
63
|
-
function buildReassembledFile(combinedXmlContents, reassembledPath, xmlElement, xmlRootElementHeader, xmlDeclarationStr) {
|
|
64
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
let finalXmlContent = combinedXmlContents.join("\n");
|
|
66
|
-
const escapedXmlDeclaration = xmlDeclarationStr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
67
|
-
const xmlDeclarationLineRegex = new RegExp(`^\\s*${escapedXmlDeclaration}\\s*$`, "gm");
|
|
68
|
-
finalXmlContent = finalXmlContent.replace(xmlDeclarationLineRegex, "");
|
|
69
|
-
finalXmlContent = finalXmlContent.replace(new RegExp(`<${xmlElement}\\s*[^>]*>`, "g"), "");
|
|
70
|
-
finalXmlContent = finalXmlContent.replace(new RegExp(`</${xmlElement}>`, "g"), "");
|
|
71
|
-
finalXmlContent = finalXmlContent.replace(/<!\[CDATA\[\s*([\s\S]*?)\s*]]>/g, function (_, cdataContent) {
|
|
72
|
-
const trimmedContent = cdataContent.trim();
|
|
73
|
-
const lines = trimmedContent.split("\n");
|
|
74
|
-
const indentedLines = lines.map(function (line) {
|
|
75
|
-
return line.replace(/^\s*/, "");
|
|
76
|
-
});
|
|
77
|
-
return ("<![CDATA[\n" + INDENT + indentedLines.join("\n" + INDENT) + "\n]]>");
|
|
78
|
-
});
|
|
79
|
-
finalXmlContent = finalXmlContent.replace(/(\n\s*){2,}/g, `\n${INDENT}`);
|
|
80
|
-
const closeTag = `</${xmlElement}>`;
|
|
81
|
-
yield promises.writeFile(reassembledPath, `${xmlDeclarationStr}\n${xmlRootElementHeader}${finalXmlContent}${closeTag}`);
|
|
82
|
-
logger.debug(`Created reassembled file: ${reassembledPath}`);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function buildXMLString(element) {
|
|
87
|
-
const xmlBuilder = new fastXmlParser.XMLBuilder(JSON_PARSER_OPTION);
|
|
88
|
-
return xmlBuilder.build(element).trimEnd();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
63
|
function stripWhitespaceTextNodes(node) {
|
|
92
64
|
if (Array.isArray(node)) {
|
|
93
65
|
return node.map(stripWhitespaceTextNodes).filter((entry) => {
|
|
@@ -141,45 +113,10 @@ function parseXML(input_1) {
|
|
|
141
113
|
});
|
|
142
114
|
}
|
|
143
115
|
|
|
144
|
-
function buildRootElementHeader(rootElement, rootElementName) {
|
|
145
|
-
let rootElementHeader = `<${rootElementName}`;
|
|
146
|
-
for (const [attrKey, attrValue] of Object.entries(rootElement)) {
|
|
147
|
-
if (attrKey.startsWith("@")) {
|
|
148
|
-
const cleanAttrKey = attrKey.slice(2);
|
|
149
|
-
rootElementHeader += ` ${cleanAttrKey}="${String(attrValue)}"`;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
rootElementHeader += ">";
|
|
153
|
-
return rootElementHeader;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function buildXMLDeclaration(parsedXml) {
|
|
157
|
-
let xmlDeclarationStr = XML_DEFAULT_DECLARATION;
|
|
158
|
-
if (parsedXml["?xml"]) {
|
|
159
|
-
const xmlDeclaration = parsedXml["?xml"];
|
|
160
|
-
const attributes = Object.entries(xmlDeclaration)
|
|
161
|
-
.map(([key, value]) => `${key.replace("@_", "")}="${value}"`)
|
|
162
|
-
.join(" ");
|
|
163
|
-
xmlDeclarationStr = `<?xml ${attributes}?>`;
|
|
164
|
-
}
|
|
165
|
-
return xmlDeclarationStr;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function parseRootElement(xmlParsed) {
|
|
169
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
const xmlDeclarationStr = buildXMLDeclaration(xmlParsed);
|
|
171
|
-
const rootElementName = Object.keys(xmlParsed)[1];
|
|
172
|
-
const rootElement = xmlParsed[rootElementName];
|
|
173
|
-
const rootElementHeader = buildRootElementHeader(rootElement, rootElementName);
|
|
174
|
-
return [rootElementName, rootElementHeader, xmlDeclarationStr];
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
116
|
class ReassembleXMLFileHandler {
|
|
179
117
|
processFilesInDirectory(dirPath) {
|
|
180
118
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
-
const
|
|
182
|
-
let rootResult = undefined;
|
|
119
|
+
const parsedXmlObjects = [];
|
|
183
120
|
const files = yield promises.readdir(dirPath);
|
|
184
121
|
files.sort((fileA, fileB) => {
|
|
185
122
|
const fullNameA = fileA.split(".")[0].toLowerCase();
|
|
@@ -194,47 +131,44 @@ class ReassembleXMLFileHandler {
|
|
|
194
131
|
const parsedObject = yield this.parseToXmlObject(filePath);
|
|
195
132
|
if (parsedObject === undefined)
|
|
196
133
|
continue;
|
|
197
|
-
|
|
198
|
-
rootResult = rootResultFromFile;
|
|
199
|
-
const combinedXmlString = buildXMLString(parsedObject);
|
|
200
|
-
combinedXmlContents.push(combinedXmlString);
|
|
134
|
+
parsedXmlObjects.push(parsedObject);
|
|
201
135
|
}
|
|
202
136
|
}
|
|
203
137
|
else if (fileStat.isDirectory()) {
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
rootResult = subRootResult;
|
|
138
|
+
const subParsedObjects = yield this.processFilesInDirectory(filePath);
|
|
139
|
+
parsedXmlObjects.push(...subParsedObjects);
|
|
207
140
|
}
|
|
208
141
|
}
|
|
209
|
-
return
|
|
142
|
+
return parsedXmlObjects;
|
|
210
143
|
});
|
|
211
144
|
}
|
|
212
145
|
reassemble(xmlAttributes) {
|
|
213
146
|
return __awaiter(this, void 0, void 0, function* () {
|
|
214
147
|
const { filePath, fileExtension, postPurge = false } = xmlAttributes;
|
|
215
|
-
let combinedXmlContents = [];
|
|
216
148
|
const fileStat = yield promises.stat(filePath);
|
|
217
149
|
if (!fileStat.isDirectory()) {
|
|
218
150
|
logger.error(`The provided path to reassemble is not a directory: ${filePath}`);
|
|
219
151
|
return;
|
|
220
152
|
}
|
|
221
153
|
logger.debug(`Parsing directory to reassemble: ${filePath}`);
|
|
222
|
-
const
|
|
223
|
-
|
|
154
|
+
const parsedXmlObjects = yield this.processFilesInDirectory(filePath);
|
|
155
|
+
if (!parsedXmlObjects.length) {
|
|
156
|
+
logger.error(`No files under ${filePath} were parsed successfully. A reassembled XML file was not created.`);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const { xml: mergedXml, declaration } = mergeXmlElements(parsedXmlObjects);
|
|
160
|
+
const xmlDeclarationStr = createXmlDeclaration(declaration);
|
|
161
|
+
const xmlContent = buildXMLString(mergedXml);
|
|
162
|
+
const finalXml = xmlDeclarationStr + xmlContent;
|
|
224
163
|
const parentDirectory = posix.dirname(filePath);
|
|
225
164
|
const subdirectoryBasename = posix.basename(filePath);
|
|
226
165
|
const fileName = fileExtension
|
|
227
166
|
? `${subdirectoryBasename}.${fileExtension}`
|
|
228
167
|
: `${subdirectoryBasename}.xml`;
|
|
229
168
|
const outputPath = posix.join(parentDirectory, fileName);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
yield
|
|
233
|
-
if (postPurge)
|
|
234
|
-
yield promises.rm(filePath, { recursive: true });
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
logger.error(`No files under ${filePath} were parsed successfully. A reassembled XML file was not created.`);
|
|
169
|
+
yield promises.writeFile(outputPath, finalXml, "utf-8");
|
|
170
|
+
if (postPurge) {
|
|
171
|
+
yield promises.rm(filePath, { recursive: true });
|
|
238
172
|
}
|
|
239
173
|
});
|
|
240
174
|
}
|
|
@@ -264,6 +198,51 @@ class ReassembleXMLFileHandler {
|
|
|
264
198
|
});
|
|
265
199
|
}
|
|
266
200
|
}
|
|
201
|
+
function mergeXmlElements(elements) {
|
|
202
|
+
if (elements.length === 0)
|
|
203
|
+
throw new Error("No elements to merge.");
|
|
204
|
+
const first = elements[0];
|
|
205
|
+
const declaration = first['?xml'];
|
|
206
|
+
const rootKey = Object.keys(first).find((k) => k !== '?xml');
|
|
207
|
+
if (!rootKey) {
|
|
208
|
+
throw new Error("No root element found in the provided XML elements.");
|
|
209
|
+
}
|
|
210
|
+
const mergedContent = {};
|
|
211
|
+
for (const element of elements) {
|
|
212
|
+
const current = element[rootKey];
|
|
213
|
+
for (const [childKey, value] of Object.entries(current)) {
|
|
214
|
+
if (Array.isArray(value)) {
|
|
215
|
+
mergedContent[childKey] = mergedContent[childKey]
|
|
216
|
+
? mergedContent[childKey].concat(value)
|
|
217
|
+
: [...value];
|
|
218
|
+
}
|
|
219
|
+
else if (typeof value === "object") {
|
|
220
|
+
mergedContent[childKey] = mergedContent[childKey]
|
|
221
|
+
? [].concat(mergedContent[childKey], value)
|
|
222
|
+
: [value];
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
if (!mergedContent.hasOwnProperty(childKey)) {
|
|
226
|
+
mergedContent[childKey] = value;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
xml: { [rootKey]: mergedContent },
|
|
233
|
+
declaration,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function createXmlDeclaration(declaration) {
|
|
237
|
+
let declarationStr = `${XML_DEFAULT_DECLARATION}\n`;
|
|
238
|
+
if (declaration) {
|
|
239
|
+
const attributes = Object.entries(declaration)
|
|
240
|
+
.map(([key, value]) => `${key.replace("@_", "")}="${value}"`)
|
|
241
|
+
.join(" ");
|
|
242
|
+
declarationStr = `<?xml ${attributes}?>\n`;
|
|
243
|
+
}
|
|
244
|
+
return declarationStr;
|
|
245
|
+
}
|
|
267
246
|
|
|
268
247
|
function parseUniqueIdElement(element, uniqueIdElements) {
|
|
269
248
|
if (uniqueIdElements === undefined) {
|
|
@@ -294,6 +273,11 @@ function createShortHash(element) {
|
|
|
294
273
|
return fullHash.slice(0, 8);
|
|
295
274
|
}
|
|
296
275
|
|
|
276
|
+
function buildXMLString(element) {
|
|
277
|
+
const xmlBuilder = new fastXmlParser.XMLBuilder(JSON_PARSER_OPTION);
|
|
278
|
+
return xmlBuilder.build(element).trimEnd();
|
|
279
|
+
}
|
|
280
|
+
|
|
297
281
|
function transformToYaml(xmlPath) {
|
|
298
282
|
return __awaiter(this, void 0, void 0, function* () {
|
|
299
283
|
const parsedXml = yield parseXML(xmlPath);
|
|
@@ -380,26 +364,27 @@ function buildNestedFile(element, disassembledPath, uniqueIdElements, rootElemen
|
|
|
380
364
|
|
|
381
365
|
function parseElement$1(params) {
|
|
382
366
|
return __awaiter(this, void 0, void 0, function* () {
|
|
383
|
-
const { element, disassembledPath, uniqueIdElements, rootElementName, rootAttributes, key,
|
|
384
|
-
if (typeof element === "object") {
|
|
367
|
+
const { element, disassembledPath, uniqueIdElements, rootElementName, rootAttributes, key, leafCount, hasNestedElements, xmlDeclarationStr, format, } = params;
|
|
368
|
+
if (typeof element === "object" && element !== null) {
|
|
385
369
|
yield buildNestedFile(element, disassembledPath, uniqueIdElements, rootElementName, rootAttributes, key, xmlDeclarationStr, format);
|
|
386
|
-
return [
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
const updatedLeafContent = `${leafContent}${indent}<${key}>${String(element)}</${key}>\n`;
|
|
390
|
-
return [updatedLeafContent, leafCount + 1, hasNestedElements];
|
|
370
|
+
return [{}, leafCount, true];
|
|
391
371
|
}
|
|
372
|
+
const leafContent = {
|
|
373
|
+
[key]: element,
|
|
374
|
+
};
|
|
375
|
+
return [leafContent, leafCount + 1, hasNestedElements];
|
|
392
376
|
});
|
|
393
377
|
}
|
|
394
378
|
|
|
395
|
-
function buildLeafFile(leafContent, disassembledPath, baseName, rootElementName,
|
|
379
|
+
function buildLeafFile(leafContent, disassembledPath, baseName, rootElementName, rootAttributes, xmlDeclarationStr, format) {
|
|
396
380
|
return __awaiter(this, void 0, void 0, function* () {
|
|
397
|
-
let leafFile = `${xmlDeclarationStr}\n`;
|
|
398
|
-
leafFile += `${rootElementHeader}\n`;
|
|
399
|
-
leafFile += leafContent;
|
|
400
|
-
leafFile += `</${rootElementName}>`;
|
|
401
381
|
const leafOutputPath = posix.join(disassembledPath, `${baseName}.xml`);
|
|
402
|
-
yield promises.
|
|
382
|
+
yield promises.mkdir(disassembledPath, { recursive: true });
|
|
383
|
+
const wrappedXml = {
|
|
384
|
+
[rootElementName]: Object.assign(Object.assign({}, rootAttributes), leafContent),
|
|
385
|
+
};
|
|
386
|
+
const serialized = `${xmlDeclarationStr}\n${buildXMLString(wrappedXml)}`;
|
|
387
|
+
yield promises.writeFile(leafOutputPath, serialized);
|
|
403
388
|
logger.debug(`Created disassembled file: ${leafOutputPath}`);
|
|
404
389
|
const transformer = getTransformer(format);
|
|
405
390
|
if (transformer) {
|
|
@@ -409,6 +394,18 @@ function buildLeafFile(leafContent, disassembledPath, baseName, rootElementName,
|
|
|
409
394
|
});
|
|
410
395
|
}
|
|
411
396
|
|
|
397
|
+
function buildXMLDeclaration(parsedXml) {
|
|
398
|
+
let xmlDeclarationStr = XML_DEFAULT_DECLARATION;
|
|
399
|
+
if (parsedXml["?xml"]) {
|
|
400
|
+
const xmlDeclaration = parsedXml["?xml"];
|
|
401
|
+
const attributes = Object.entries(xmlDeclaration)
|
|
402
|
+
.map(([key, value]) => `${key.replace("@_", "")}="${value}"`)
|
|
403
|
+
.join(" ");
|
|
404
|
+
xmlDeclarationStr = `<?xml ${attributes}?>`;
|
|
405
|
+
}
|
|
406
|
+
return xmlDeclarationStr;
|
|
407
|
+
}
|
|
408
|
+
|
|
412
409
|
function extractRootAttributes(rootElement) {
|
|
413
410
|
const attributesOnly = {};
|
|
414
411
|
for (const [attrKey, attrValue] of Object.entries(rootElement)) {
|
|
@@ -419,7 +416,7 @@ function extractRootAttributes(rootElement) {
|
|
|
419
416
|
return attributesOnly;
|
|
420
417
|
}
|
|
421
418
|
|
|
422
|
-
function buildDisassembledFiles$1(filePath, disassembledPath, uniqueIdElements, baseName,
|
|
419
|
+
function buildDisassembledFiles$1(filePath, disassembledPath, uniqueIdElements, baseName, postPurge, format) {
|
|
423
420
|
return __awaiter(this, void 0, void 0, function* () {
|
|
424
421
|
const parsedXml = yield parseXML(filePath);
|
|
425
422
|
if (parsedXml === undefined)
|
|
@@ -427,49 +424,40 @@ function buildDisassembledFiles$1(filePath, disassembledPath, uniqueIdElements,
|
|
|
427
424
|
const rootElementName = Object.keys(parsedXml)[1];
|
|
428
425
|
const xmlDeclarationStr = buildXMLDeclaration(parsedXml);
|
|
429
426
|
const rootElement = parsedXml[rootElementName];
|
|
430
|
-
const rootElementHeader = buildRootElementHeader(rootElement, rootElementName);
|
|
431
427
|
const rootAttributes = extractRootAttributes(rootElement);
|
|
432
|
-
let leafContent =
|
|
428
|
+
let leafContent = {};
|
|
433
429
|
let leafCount = 0;
|
|
434
430
|
let hasNestedElements = false;
|
|
435
|
-
for (const key of Object.keys(rootElement).filter((
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
rootElementName,
|
|
443
|
-
rootAttributes,
|
|
444
|
-
key,
|
|
445
|
-
indent,
|
|
446
|
-
leafContent,
|
|
447
|
-
leafCount,
|
|
448
|
-
hasNestedElements,
|
|
449
|
-
xmlDeclarationStr,
|
|
450
|
-
format,
|
|
451
|
-
});
|
|
452
|
-
leafContent = updatedLeafContent;
|
|
453
|
-
leafCount = updatedLeafCount;
|
|
454
|
-
hasNestedElements = updatedHasNestedElements;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
const [updatedLeafContent, updatedLeafCount, updatedHasNestedElements] = yield parseElement$1({
|
|
459
|
-
element: rootElement[key],
|
|
431
|
+
for (const key of Object.keys(rootElement).filter((k) => !k.startsWith("@"))) {
|
|
432
|
+
const elements = Array.isArray(rootElement[key])
|
|
433
|
+
? rootElement[key]
|
|
434
|
+
: [rootElement[key]];
|
|
435
|
+
for (const element of elements) {
|
|
436
|
+
const [parsedLeafContent, updatedLeafCount, updatedHasNestedElements] = yield parseElement$1({
|
|
437
|
+
element,
|
|
460
438
|
disassembledPath,
|
|
461
439
|
uniqueIdElements,
|
|
462
440
|
rootElementName,
|
|
463
441
|
rootAttributes,
|
|
464
442
|
key,
|
|
465
|
-
indent,
|
|
466
|
-
leafContent,
|
|
467
443
|
leafCount,
|
|
468
444
|
hasNestedElements,
|
|
469
445
|
xmlDeclarationStr,
|
|
470
446
|
format,
|
|
471
447
|
});
|
|
472
|
-
|
|
448
|
+
const newContent = parsedLeafContent[key];
|
|
449
|
+
if (newContent !== undefined) {
|
|
450
|
+
const existing = leafContent[key];
|
|
451
|
+
const existingArray = Array.isArray(existing)
|
|
452
|
+
? existing
|
|
453
|
+
: existing !== undefined
|
|
454
|
+
? [existing]
|
|
455
|
+
: [];
|
|
456
|
+
const incomingArray = Array.isArray(newContent)
|
|
457
|
+
? newContent
|
|
458
|
+
: [newContent];
|
|
459
|
+
leafContent[key] = [...existingArray, ...incomingArray];
|
|
460
|
+
}
|
|
473
461
|
leafCount = updatedLeafCount;
|
|
474
462
|
hasNestedElements = updatedHasNestedElements;
|
|
475
463
|
}
|
|
@@ -479,38 +467,41 @@ function buildDisassembledFiles$1(filePath, disassembledPath, uniqueIdElements,
|
|
|
479
467
|
return;
|
|
480
468
|
}
|
|
481
469
|
if (leafCount > 0) {
|
|
482
|
-
yield buildLeafFile(leafContent, disassembledPath, baseName, rootElementName,
|
|
470
|
+
yield buildLeafFile(leafContent, disassembledPath, baseName, rootElementName, rootAttributes, xmlDeclarationStr, format);
|
|
483
471
|
}
|
|
484
472
|
if (postPurge) {
|
|
485
|
-
promises.unlink(filePath);
|
|
473
|
+
yield promises.unlink(filePath);
|
|
486
474
|
}
|
|
487
475
|
});
|
|
488
476
|
}
|
|
489
477
|
|
|
490
478
|
function parseElement(params) {
|
|
491
479
|
return __awaiter(this, void 0, void 0, function* () {
|
|
492
|
-
const { element, key,
|
|
480
|
+
const { element, key, hasNestedElements } = params;
|
|
493
481
|
const nestedGroups = {};
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
482
|
+
const isArray = Array.isArray(element);
|
|
483
|
+
const isObjectWithMultipleFields = typeof element === "object" &&
|
|
484
|
+
element !== null &&
|
|
485
|
+
Object.keys(element).length > 1;
|
|
486
|
+
const isNested = isArray || isObjectWithMultipleFields;
|
|
487
|
+
if (isNested) {
|
|
488
|
+
nestedGroups[key] = [element];
|
|
498
489
|
return {
|
|
499
|
-
leafContent,
|
|
500
|
-
leafCount,
|
|
490
|
+
leafContent: {},
|
|
491
|
+
leafCount: params.leafCount,
|
|
501
492
|
hasNestedElements: true,
|
|
502
493
|
nestedGroups,
|
|
503
494
|
};
|
|
504
495
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
}
|
|
496
|
+
const leafContent = {
|
|
497
|
+
[key]: element,
|
|
498
|
+
};
|
|
499
|
+
return {
|
|
500
|
+
leafContent,
|
|
501
|
+
leafCount: params.leafCount + 1,
|
|
502
|
+
hasNestedElements,
|
|
503
|
+
nestedGroups,
|
|
504
|
+
};
|
|
514
505
|
});
|
|
515
506
|
}
|
|
516
507
|
|
|
@@ -532,21 +523,31 @@ function buildGroupedNestedFile(tag, elements, disassembledPath, rootElementName
|
|
|
532
523
|
});
|
|
533
524
|
}
|
|
534
525
|
|
|
535
|
-
function
|
|
526
|
+
function orderXmlElementKeys(content, keyOrder) {
|
|
527
|
+
const ordered = {};
|
|
528
|
+
for (const key of keyOrder) {
|
|
529
|
+
if (content[key] !== undefined) {
|
|
530
|
+
ordered[key] = content[key];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return ordered;
|
|
534
|
+
}
|
|
535
|
+
function buildDisassembledFiles(filePath, disassembledPath, baseName, postPurge, format) {
|
|
536
536
|
return __awaiter(this, void 0, void 0, function* () {
|
|
537
|
+
var _a;
|
|
537
538
|
const parsedXml = yield parseXML(filePath);
|
|
538
539
|
if (parsedXml === undefined)
|
|
539
540
|
return;
|
|
540
541
|
const rootElementName = Object.keys(parsedXml)[1];
|
|
541
542
|
const xmlDeclarationStr = buildXMLDeclaration(parsedXml);
|
|
542
543
|
const rootElement = parsedXml[rootElementName];
|
|
543
|
-
const rootElementHeader = buildRootElementHeader(rootElement, rootElementName);
|
|
544
544
|
const rootAttributes = extractRootAttributes(rootElement);
|
|
545
|
-
let leafContent =
|
|
545
|
+
let leafContent = {};
|
|
546
546
|
let leafCount = 0;
|
|
547
547
|
let hasNestedElements = false;
|
|
548
548
|
const nestedGroups = {};
|
|
549
|
-
|
|
549
|
+
const keyOrder = Object.keys(parsedXml[rootElementName]).filter((k) => !k.startsWith("@"));
|
|
550
|
+
for (const key of keyOrder) {
|
|
550
551
|
const elements = Array.isArray(rootElement[key])
|
|
551
552
|
? rootElement[key]
|
|
552
553
|
: [rootElement[key]];
|
|
@@ -554,21 +555,33 @@ function buildDisassembledFiles(filePath, disassembledPath, baseName, indent, po
|
|
|
554
555
|
const result = yield parseElement({
|
|
555
556
|
element,
|
|
556
557
|
key,
|
|
557
|
-
indent,
|
|
558
|
-
leafContent,
|
|
559
558
|
leafCount,
|
|
560
559
|
hasNestedElements});
|
|
561
|
-
|
|
560
|
+
if (Object.keys(result.leafContent).length > 0) {
|
|
561
|
+
const newContent = result.leafContent[key];
|
|
562
|
+
if (newContent !== undefined) {
|
|
563
|
+
const existing = leafContent[key];
|
|
564
|
+
const existingArray = Array.isArray(existing)
|
|
565
|
+
? existing
|
|
566
|
+
: existing !== undefined
|
|
567
|
+
? [existing]
|
|
568
|
+
: [];
|
|
569
|
+
const incomingArray = Array.isArray(newContent)
|
|
570
|
+
? newContent
|
|
571
|
+
: [newContent];
|
|
572
|
+
leafContent[key] = [...existingArray, ...incomingArray];
|
|
573
|
+
}
|
|
574
|
+
}
|
|
562
575
|
leafCount = result.leafCount;
|
|
563
576
|
hasNestedElements = result.hasNestedElements;
|
|
564
577
|
for (const tag in result.nestedGroups) {
|
|
565
578
|
if (!nestedGroups[tag])
|
|
566
579
|
nestedGroups[tag] = [];
|
|
567
|
-
nestedGroups[tag].push(...result.nestedGroups[tag]);
|
|
580
|
+
nestedGroups[tag].push(...((_a = result.nestedGroups[tag]) !== null && _a !== void 0 ? _a : []));
|
|
568
581
|
}
|
|
569
582
|
}
|
|
570
583
|
}
|
|
571
|
-
if (!hasNestedElements) {
|
|
584
|
+
if (!hasNestedElements && leafCount > 0) {
|
|
572
585
|
logger.error(`The XML file ${filePath} only has leaf elements. This file will not be disassembled.`);
|
|
573
586
|
return;
|
|
574
587
|
}
|
|
@@ -576,7 +589,8 @@ function buildDisassembledFiles(filePath, disassembledPath, baseName, indent, po
|
|
|
576
589
|
yield buildGroupedNestedFile(tag, nestedGroups[tag], disassembledPath, rootElementName, rootAttributes, xmlDeclarationStr, format);
|
|
577
590
|
}
|
|
578
591
|
if (leafCount > 0) {
|
|
579
|
-
|
|
592
|
+
const orderedLeafContent = orderXmlElementKeys(leafContent, keyOrder);
|
|
593
|
+
yield buildLeafFile(orderedLeafContent, disassembledPath, baseName, rootElementName, rootAttributes, xmlDeclarationStr, format);
|
|
580
594
|
}
|
|
581
595
|
if (postPurge) {
|
|
582
596
|
yield promises.unlink(filePath);
|
|
@@ -654,10 +668,10 @@ class DisassembleXMLFileHandler {
|
|
|
654
668
|
if (prePurge && node_fs.existsSync(outputPath))
|
|
655
669
|
yield promises.rm(outputPath, { recursive: true });
|
|
656
670
|
if (strategy === "grouped-by-tag") {
|
|
657
|
-
yield buildDisassembledFiles(filePath, outputPath, fullName,
|
|
671
|
+
yield buildDisassembledFiles(filePath, outputPath, fullName, postPurge, format);
|
|
658
672
|
}
|
|
659
673
|
else {
|
|
660
|
-
yield buildDisassembledFiles$1(filePath, outputPath, uniqueIdElements, fullName,
|
|
674
|
+
yield buildDisassembledFiles$1(filePath, outputPath, uniqueIdElements, fullName, postPurge, format);
|
|
661
675
|
}
|
|
662
676
|
});
|
|
663
677
|
}
|