forked from dawidd6/action-download-artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderedJs2Xml.js
More file actions
306 lines (266 loc) · 10.6 KB
/
Copy pathorderedJs2Xml.js
File metadata and controls
306 lines (266 loc) · 10.6 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { Expression, Matcher } from 'path-expression-matcher';
const EOL = "\n";
/**
*
* @param {array} jArray
* @param {any} options
* @returns
*/
export default function toXml(jArray, options) {
let indentation = "";
if (options.format && options.indentBy.length > 0) {
indentation = EOL;
}
// Pre-compile stopNode expressions for pattern matching
const stopNodeExpressions = [];
if (options.stopNodes && Array.isArray(options.stopNodes)) {
for (let i = 0; i < options.stopNodes.length; i++) {
const node = options.stopNodes[i];
if (typeof node === 'string') {
stopNodeExpressions.push(new Expression(node));
} else if (node instanceof Expression) {
stopNodeExpressions.push(node);
}
}
}
// Initialize matcher for path tracking
const matcher = new Matcher();
return arrToStr(jArray, options, indentation, matcher, stopNodeExpressions);
}
function arrToStr(arr, options, indentation, matcher, stopNodeExpressions) {
let xmlStr = "";
let isPreviousElementTag = false;
if (options.maxNestedTags && matcher.getDepth() > options.maxNestedTags) {
throw new Error("Maximum nested tags exceeded");
}
if (!Array.isArray(arr)) {
// Non-array values (e.g. string tag values) should be treated as text content
if (arr !== undefined && arr !== null) {
let text = arr.toString();
text = replaceEntitiesValue(text, options);
return text;
}
return "";
}
for (let i = 0; i < arr.length; i++) {
const tagObj = arr[i];
const tagName = propName(tagObj);
if (tagName === undefined) continue;
// Extract attributes from ":@" property
const attrValues = extractAttributeValues(tagObj[":@"], options);
// Push tag to matcher WITH attributes
matcher.push(tagName, attrValues);
// Check if this is a stop node using Expression matching
const isStopNode = checkStopNode(matcher, stopNodeExpressions);
if (tagName === options.textNodeName) {
let tagText = tagObj[tagName];
if (!isStopNode) {
tagText = options.tagValueProcessor(tagName, tagText);
tagText = replaceEntitiesValue(tagText, options);
}
if (isPreviousElementTag) {
xmlStr += indentation;
}
xmlStr += tagText;
isPreviousElementTag = false;
matcher.pop();
continue;
} else if (tagName === options.cdataPropName) {
if (isPreviousElementTag) {
xmlStr += indentation;
}
const val = tagObj[tagName][0][options.textNodeName];
const safeVal = String(val).replace(/\]\]>/g, ']]]]><![CDATA[>');
xmlStr += `<![CDATA[${safeVal}]]>`;
isPreviousElementTag = false;
matcher.pop();
continue;
} else if (tagName === options.commentPropName) {
const val = tagObj[tagName][0][options.textNodeName]
const safeVal = String(val)
.replace(/--/g, '- -') // -- is illegal anywhere in comment content
.replace(/-$/, '- '); // trailing - would form -- with the closing -->
xmlStr += indentation + `<!--${safeVal}-->`;
isPreviousElementTag = true;
matcher.pop();
continue;
} else if (tagName[0] === "?") {
const attStr = attr_to_str(tagObj[":@"], options, isStopNode);
const tempInd = tagName === "?xml" ? "" : indentation;
let piTextNodeName = tagObj[tagName][0][options.textNodeName];
piTextNodeName = piTextNodeName.length !== 0 ? " " + piTextNodeName : ""; //remove extra spacing
xmlStr += tempInd + `<${tagName}${piTextNodeName}${attStr}?>`;
isPreviousElementTag = true;
matcher.pop();
continue;
}
let newIdentation = indentation;
if (newIdentation !== "") {
newIdentation += options.indentBy;
}
// Pass isStopNode to attr_to_str so attributes are also not processed for stopNodes
const attStr = attr_to_str(tagObj[":@"], options, isStopNode);
const tagStart = indentation + `<${tagName}${attStr}`;
// If this is a stopNode, get raw content without processing
let tagValue;
if (isStopNode) {
tagValue = getRawContent(tagObj[tagName], options);
} else {
tagValue = arrToStr(tagObj[tagName], options, newIdentation, matcher, stopNodeExpressions);
}
if (options.unpairedTags.indexOf(tagName) !== -1) {
if (options.suppressUnpairedNode) xmlStr += tagStart + ">";
else xmlStr += tagStart + "/>";
} else if ((!tagValue || tagValue.length === 0) && options.suppressEmptyNode) {
xmlStr += tagStart + "/>";
} else if (tagValue && tagValue.endsWith(">")) {
xmlStr += tagStart + `>${tagValue}${indentation}</${tagName}>`;
} else {
xmlStr += tagStart + ">";
if (tagValue && indentation !== "" && (tagValue.includes("/>") || tagValue.includes("</"))) {
xmlStr += indentation + options.indentBy + tagValue + indentation;
} else {
xmlStr += tagValue;
}
xmlStr += `</${tagName}>`;
}
isPreviousElementTag = true;
// Pop tag from matcher
matcher.pop();
}
return xmlStr;
}
/**
* Extract attribute values from the ":@" object and return as plain object
* for passing to matcher.push()
*/
function extractAttributeValues(attrMap, options) {
if (!attrMap || options.ignoreAttributes) return null;
const attrValues = {};
let hasAttrs = false;
for (let attr in attrMap) {
if (!Object.prototype.hasOwnProperty.call(attrMap, attr)) continue;
// Remove the attribute prefix to get clean attribute name
const cleanAttrName = attr.startsWith(options.attributeNamePrefix)
? attr.substr(options.attributeNamePrefix.length)
: attr;
attrValues[cleanAttrName] = attrMap[attr];
hasAttrs = true;
}
return hasAttrs ? attrValues : null;
}
/**
* Extract raw content from a stopNode without any processing
* This preserves the content exactly as-is, including special characters
*/
function getRawContent(arr, options) {
if (!Array.isArray(arr)) {
// Non-array values return as-is
if (arr !== undefined && arr !== null) {
return arr.toString();
}
return "";
}
let content = "";
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
const tagName = propName(item);
if (tagName === options.textNodeName) {
// Raw text content - NO processing, NO entity replacement
content += item[tagName];
} else if (tagName === options.cdataPropName) {
// CDATA content
content += item[tagName][0][options.textNodeName];
} else if (tagName === options.commentPropName) {
// Comment content
content += item[tagName][0][options.textNodeName];
} else if (tagName && tagName[0] === "?") {
// Processing instruction - skip for stopNodes
continue;
} else if (tagName) {
// Nested tags within stopNode
// Recursively get raw content and reconstruct the tag
// For stopNodes, we don't process attributes either
const attStr = attr_to_str_raw(item[":@"], options);
const nestedContent = getRawContent(item[tagName], options);
if (!nestedContent || nestedContent.length === 0) {
content += `<${tagName}${attStr}/>`;
} else {
content += `<${tagName}${attStr}>${nestedContent}</${tagName}>`;
}
}
}
return content;
}
/**
* Build attribute string for stopNodes - NO entity replacement
*/
function attr_to_str_raw(attrMap, options) {
let attrStr = "";
if (attrMap && !options.ignoreAttributes) {
for (let attr in attrMap) {
if (!Object.prototype.hasOwnProperty.call(attrMap, attr)) continue;
// For stopNodes, use raw value without processing
let attrVal = attrMap[attr];
if (attrVal === true && options.suppressBooleanAttributes) {
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}`;
} else {
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}="${attrVal}"`;
}
}
}
return attrStr;
}
function propName(obj) {
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
if (key !== ":@") return key;
}
}
function attr_to_str(attrMap, options, isStopNode) {
let attrStr = "";
if (attrMap && !options.ignoreAttributes) {
for (let attr in attrMap) {
if (!Object.prototype.hasOwnProperty.call(attrMap, attr)) continue;
let attrVal;
if (isStopNode) {
// For stopNodes, use raw value without any processing
attrVal = attrMap[attr];
} else {
// Normal processing: apply attributeValueProcessor and entity replacement
attrVal = options.attributeValueProcessor(attr, attrMap[attr]);
attrVal = replaceEntitiesValue(attrVal, options);
}
if (attrVal === true && options.suppressBooleanAttributes) {
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}`;
} else {
attrStr += ` ${attr.substr(options.attributeNamePrefix.length)}="${attrVal}"`;
}
}
}
return attrStr;
}
function checkStopNode(matcher, stopNodeExpressions) {
if (!stopNodeExpressions || stopNodeExpressions.length === 0) return false;
for (let i = 0; i < stopNodeExpressions.length; i++) {
if (matcher.matches(stopNodeExpressions[i])) {
return true;
}
}
return false;
}
function replaceEntitiesValue(textValue, options) {
if (textValue && textValue.length > 0 && options.processEntities) {
for (let i = 0; i < options.entities.length; i++) {
const entity = options.entities[i];
textValue = textValue.replace(entity.regex, entity.val);
}
}
return textValue;
}
function cdataVal(val) {
}
function commentVal(val) {
}