@cntwg/xml-lib-js 0.0.20 → 0.0.21
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 +9 -0
- package/doc/xmldoc-lib.md +6 -2
- package/index.js +3 -3
- package/lib/xmldoc-lib.js +156 -106
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
#### *v0.0.21*
|
|
2
|
+
|
|
3
|
+
Pre-release version.
|
|
4
|
+
|
|
5
|
+
> - some fixes in `xmldoc-lib.js` module;
|
|
6
|
+
> - added method `hasChild()` from `TXmlElementController` class;
|
|
7
|
+
> - removed constants `DEF_XML_PARSE_OPTIONS` and `DEF_XML_ROOT_ETAG_NAME` from `xmldoc-lib.js` module exports;
|
|
8
|
+
> - updated dependency on `@ygracs/xobj-lib-js` module to v0.0.14rc3.
|
|
9
|
+
|
|
1
10
|
#### *v0.0.20*
|
|
2
11
|
|
|
3
12
|
Pre-release version.
|
package/doc/xmldoc-lib.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
>|***rev.*:**|0.1.
|
|
1
|
+
>|***rev.*:**|0.1.20|
|
|
2
2
|
>|:---|---:|
|
|
3
|
-
>|date:|2023-
|
|
3
|
+
>|date:|2023-03-01|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
@@ -183,6 +183,10 @@ The method received value that can be a string or an array. If value is an array
|
|
|
183
183
|
or
|
|
184
184
|
`[ <lang>, <textValue> ]`.
|
|
185
185
|
|
|
186
|
+
##### **hasChild(name)**
|
|
187
|
+
|
|
188
|
+
This method checks whether or not a child element addressed by `name` is exists.
|
|
189
|
+
|
|
186
190
|
##### **getChild(name)**
|
|
187
191
|
|
|
188
192
|
This method returns a child element addressed by `name`.
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.009-20230316]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -14,9 +14,9 @@ const xmldoc = require('./lib/xmldoc-lib.js');
|
|
|
14
14
|
exports.xObj = xObj;
|
|
15
15
|
|
|
16
16
|
// will deprecate
|
|
17
|
-
exports.DEF_XML_PARSE_OPTIONS = xmldoc.
|
|
17
|
+
exports.DEF_XML_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
|
|
18
18
|
// will deprecate
|
|
19
|
-
exports.DEF_XML_ROOT_ETAG_NAME = xmldoc.
|
|
19
|
+
exports.DEF_XML_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
|
|
20
20
|
|
|
21
21
|
exports.XML_DEF_PARSE_OPTIONS = xmldoc.XML_DEF_PARSE_OPTIONS;
|
|
22
22
|
exports.XML_DEF_ROOT_ETAG_NAME = xmldoc.XML_DEF_ROOT_ETAG_NAME;
|
package/lib/xmldoc-lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.065-20230316]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -66,12 +66,12 @@ class TXmlAttributesMapper {
|
|
|
66
66
|
return isPlainObject(items) ? Object.entries(items) : [];
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
hasAttribute(
|
|
69
|
+
hasAttribute(name){
|
|
70
70
|
let result = false;
|
|
71
71
|
try {
|
|
72
72
|
result = xObj.checkXObjAttribute(
|
|
73
73
|
this.#_host,
|
|
74
|
-
|
|
74
|
+
name,
|
|
75
75
|
this.#_options.settings.attributesKey,
|
|
76
76
|
);
|
|
77
77
|
} catch (err) {
|
|
@@ -81,12 +81,12 @@ class TXmlAttributesMapper {
|
|
|
81
81
|
return result;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
getAttribute(
|
|
84
|
+
getAttribute(name){
|
|
85
85
|
let result = '';
|
|
86
86
|
try {
|
|
87
87
|
result = xObj.readXObjAttr(
|
|
88
88
|
this.#_host,
|
|
89
|
-
|
|
89
|
+
name,
|
|
90
90
|
this.#_options.settings.attributesKey,
|
|
91
91
|
);
|
|
92
92
|
} catch (err) {
|
|
@@ -112,24 +112,19 @@ class TXmlAttributesMapper {
|
|
|
112
112
|
return result;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
delAttribute(
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
);
|
|
127
|
-
let isSUCCEED = isPlainObject(items);
|
|
128
|
-
if (isSUCCEED) {
|
|
129
|
-
// // TODO: catch errors in strict mode
|
|
130
|
-
isSUCCEED = delete items[name];
|
|
115
|
+
delAttribute(name){
|
|
116
|
+
let result = false;
|
|
117
|
+
try {
|
|
118
|
+
result = xObj.deleteXObjAttribute(
|
|
119
|
+
this.#_host,
|
|
120
|
+
name,
|
|
121
|
+
this.#_options.settings.attributesKey,
|
|
122
|
+
);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
// // TODO: verify what error is thrown
|
|
125
|
+
result = false;
|
|
131
126
|
};
|
|
132
|
-
return
|
|
127
|
+
return result;
|
|
133
128
|
}
|
|
134
129
|
|
|
135
130
|
clear(){
|
|
@@ -244,42 +239,60 @@ class TXmlElementController {
|
|
|
244
239
|
}
|
|
245
240
|
|
|
246
241
|
setTextValue(value){
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
242
|
+
const _value = valueToArray(value);
|
|
243
|
+
const len = _value.length;
|
|
244
|
+
if (len > 0) {
|
|
245
|
+
if (len === 1) {
|
|
246
|
+
return xObj.writeXObjParam(
|
|
247
|
+
this.#_host,
|
|
248
|
+
_value[0],
|
|
249
|
+
this.#_parseOptions.settings.textKey,
|
|
250
|
+
);
|
|
251
|
+
} else {
|
|
252
|
+
const [ lang, param ] = _value;
|
|
253
|
+
if (xObj.writeXObjParam(
|
|
254
|
+
this.#_host,
|
|
255
|
+
param,
|
|
256
|
+
this.#_parseOptions.settings.textKey,
|
|
257
|
+
)) {
|
|
258
|
+
return this.#_attributes.setAttribute(XML_LANG_ATTR_TNAME, lang);
|
|
259
|
+
};
|
|
262
260
|
};
|
|
263
261
|
};
|
|
264
262
|
return false;
|
|
265
263
|
}
|
|
266
264
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
265
|
+
hasChild(name){
|
|
266
|
+
const _name = xObj.evalXObjEName(name);
|
|
267
|
+
let result = false;
|
|
268
|
+
if (typeof _name === 'string' && _name !== '') {
|
|
269
|
+
try {
|
|
270
|
+
result = isObject(xObj.getXObjElement(this.#_host, name));
|
|
271
|
+
} catch (err) {
|
|
272
|
+
// // TODO: [?] verify what error is thrown
|
|
273
|
+
throw err;
|
|
274
|
+
};
|
|
273
275
|
};
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
getChild(name){
|
|
280
|
+
const _name = xObj.evalXObjEName(name);
|
|
281
|
+
let result = null;
|
|
282
|
+
if (typeof _name === 'string' && _name !== '') {
|
|
283
|
+
try {
|
|
284
|
+
const item = xObj.getXObjElement(this.#_host, name);
|
|
285
|
+
if (isArray(item)) {
|
|
286
|
+
result = new TXmlElementsListController(item, this.#_options);
|
|
287
|
+
} else if (isObject(item)) {
|
|
288
|
+
result = new TXmlElementController(item, this.#_options);
|
|
289
|
+
};
|
|
290
|
+
} catch (err) {
|
|
291
|
+
// // TODO: [?] verify what error is thrown
|
|
292
|
+
throw err;
|
|
293
|
+
};
|
|
281
294
|
};
|
|
282
|
-
return
|
|
295
|
+
return result;
|
|
283
296
|
}
|
|
284
297
|
|
|
285
298
|
_getChildRaw(name){
|
|
@@ -306,7 +319,7 @@ class TXmlElementController {
|
|
|
306
319
|
};
|
|
307
320
|
} else if (isObject(obj)) {
|
|
308
321
|
//console.log('CHECK: TXmlElementController._setChildRaw() => <obj> is object...');
|
|
309
|
-
item = xObj.insertXObjElement(this.#_host, name, true);
|
|
322
|
+
item = xObj.insertXObjElement(this.#_host, name, { force: true });
|
|
310
323
|
// // TODO: set element
|
|
311
324
|
};
|
|
312
325
|
if (item) isSUCCEED = true;
|
|
@@ -325,38 +338,48 @@ class TXmlElementController {
|
|
|
325
338
|
return result.isSucceed ? result.item : null;
|
|
326
339
|
}
|
|
327
340
|
|
|
328
|
-
addChild(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
341
|
+
addChild(name){
|
|
342
|
+
const _name = xObj.evalXObjEName(name);
|
|
343
|
+
let result = null;
|
|
344
|
+
if (typeof _name === 'string' && _name !== '') {
|
|
345
|
+
try {
|
|
346
|
+
let { item } = xObj.addXObjElement(this.#_host, name);
|
|
347
|
+
if (item) result = new TXmlElementController(item, this.#_options)
|
|
348
|
+
} catch (err) {
|
|
349
|
+
// // TODO: [?] verify what error is thrown
|
|
350
|
+
throw err;
|
|
351
|
+
};
|
|
334
352
|
};
|
|
335
|
-
|
|
336
|
-
return (
|
|
337
|
-
result.isSucceed
|
|
338
|
-
? new TXmlElementController(result.item, this.#_options)
|
|
339
|
-
: null
|
|
340
|
-
);
|
|
353
|
+
return result;
|
|
341
354
|
}
|
|
342
355
|
|
|
343
|
-
delChild(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
356
|
+
delChild(name){
|
|
357
|
+
const _name = xObj.evalXObjEName(name);
|
|
358
|
+
let isSUCCEED = false;
|
|
359
|
+
if (typeof _name === 'string' && _name !== '') {
|
|
360
|
+
try {
|
|
361
|
+
const item = xObj.getXObjElement(this.#_host, name);
|
|
362
|
+
let isACCEPTED = false;
|
|
363
|
+
if (isArray(item)) {
|
|
364
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.typeof() => [object Array]');
|
|
365
|
+
if (item.length < 2) {
|
|
366
|
+
isACCEPTED = true;
|
|
367
|
+
} else {
|
|
368
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.length() => ['+item.length+']');
|
|
369
|
+
};
|
|
370
|
+
} else if (isObject(item)) {
|
|
371
|
+
isACCEPTED = true;
|
|
372
|
+
} else {
|
|
373
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.typeof() => ['+typeof item+']');
|
|
374
|
+
//console.log('CHECK: TXmlElementController.delChild() => item.value() => ['+item+']');
|
|
375
|
+
};
|
|
376
|
+
if (isACCEPTED) isSUCCEED = xObj.deleteXObjElement(this.#_host, name);
|
|
377
|
+
} catch (err) {
|
|
378
|
+
// // TODO: [?] verify what error is thrown
|
|
379
|
+
throw err;
|
|
380
|
+
};
|
|
358
381
|
};
|
|
359
|
-
return
|
|
382
|
+
return isSUCCEED;
|
|
360
383
|
}
|
|
361
384
|
|
|
362
385
|
clear(){
|
|
@@ -660,7 +683,7 @@ class TXmlContentRootElement extends TXmlElementController {
|
|
|
660
683
|
const entries = Object.keys(obj);
|
|
661
684
|
const len = entries.length;
|
|
662
685
|
if (len === 0) {
|
|
663
|
-
rootItem = xObj.insertXObjElement(obj, rootETagName
|
|
686
|
+
rootItem = xObj.insertXObjElement(obj, rootETagName);
|
|
664
687
|
} else {
|
|
665
688
|
const {
|
|
666
689
|
declarationKey,
|
|
@@ -692,13 +715,13 @@ class TXmlContentRootElement extends TXmlElementController {
|
|
|
692
715
|
};
|
|
693
716
|
};
|
|
694
717
|
if (targetKey === '') {
|
|
695
|
-
rootItem = xObj.insertXObjElement(obj, rootETagName
|
|
718
|
+
rootItem = xObj.insertXObjElement(obj, rootETagName);
|
|
696
719
|
} else {
|
|
697
720
|
if (autoBindRoot) {
|
|
698
721
|
rootItem = obj[targetKey];
|
|
699
722
|
rootETagName = targetKey;
|
|
700
723
|
if (!isPlainObject(rootItem)) {
|
|
701
|
-
rootItem = xObj.insertXObjElement(obj, rootETagName
|
|
724
|
+
rootItem = xObj.insertXObjElement(obj, rootETagName);
|
|
702
725
|
};
|
|
703
726
|
} else {
|
|
704
727
|
throw new TypeError(`TXmlContentRootElement : ${XML_TE_NROOT_EMSG}`);
|
|
@@ -706,7 +729,7 @@ class TXmlContentRootElement extends TXmlElementController {
|
|
|
706
729
|
};
|
|
707
730
|
};
|
|
708
731
|
} else if (!isPlainObject(rootItem)) {
|
|
709
|
-
rootItem = xObj.insertXObjElement(obj, rootETagName
|
|
732
|
+
rootItem = xObj.insertXObjElement(obj, rootETagName);
|
|
710
733
|
};
|
|
711
734
|
// init parent class instance
|
|
712
735
|
super(rootItem, _options);
|
|
@@ -814,7 +837,7 @@ class TXmlContentContainer {
|
|
|
814
837
|
try {
|
|
815
838
|
result = xmlParser.js2xml(this.#_content, this.#_parseOptions.js2xml);
|
|
816
839
|
} catch (err) {
|
|
817
|
-
console.log('CHECK: TXmlContentContainer.saveToXMLString() => Error => '+err);
|
|
840
|
+
//console.log('CHECK: TXmlContentContainer.saveToXMLString() => Error => '+err);
|
|
818
841
|
//isERR = true;
|
|
819
842
|
throw err;
|
|
820
843
|
};
|
|
@@ -844,9 +867,21 @@ class TXmlContentContainer {
|
|
|
844
867
|
data.errEvent = 'OPS_IS_SUCCEED';
|
|
845
868
|
resolve(data);
|
|
846
869
|
}).catch(err => {
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
870
|
+
switch (err.code) {
|
|
871
|
+
case 'ENOENT': {
|
|
872
|
+
data.isERR = true;
|
|
873
|
+
data.errEvent = err.code;
|
|
874
|
+
data.content = '';
|
|
875
|
+
resolve(data);
|
|
876
|
+
break;
|
|
877
|
+
}
|
|
878
|
+
default: {
|
|
879
|
+
console.log('CHECK: TXmlContentContainer.saveToFile() => Error => '+err);
|
|
880
|
+
console.log('CHECK: TXmlContentContainer.saveToFile() => Error => '+err.code);
|
|
881
|
+
reject(err);
|
|
882
|
+
break;
|
|
883
|
+
}
|
|
884
|
+
};
|
|
850
885
|
});
|
|
851
886
|
};
|
|
852
887
|
});
|
|
@@ -870,9 +905,20 @@ class TXmlContentContainer {
|
|
|
870
905
|
data.content = this.saveToXMLString();
|
|
871
906
|
fs.writeFileSync(data.source, data.content, 'utf8');
|
|
872
907
|
} catch (err) {
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
908
|
+
switch (err.code) {
|
|
909
|
+
case 'ENOENT': {
|
|
910
|
+
data.isERR = true;
|
|
911
|
+
data.errEvent = err.code;
|
|
912
|
+
data.content = '';
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
default: {
|
|
916
|
+
console.log('CHECK: TXmlContentContainer.saveToFileSync() => Error => '+err);
|
|
917
|
+
console.log('CHECK: TXmlContentContainer.saveToFileSync() => Error => '+err.code);
|
|
918
|
+
throw err;
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
};
|
|
876
922
|
};
|
|
877
923
|
};
|
|
878
924
|
return data;
|
|
@@ -888,8 +934,8 @@ class TXmlContentContainer {
|
|
|
888
934
|
isSUCCEED = this._bindContent(obj, this.#_options);
|
|
889
935
|
};
|
|
890
936
|
} catch (err) {
|
|
891
|
-
console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err);
|
|
892
|
-
console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err.code);
|
|
937
|
+
//console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err);
|
|
938
|
+
//console.log('CHECK: TXmlContentContainer.loadFromXMLString() => Error => '+err.code);
|
|
893
939
|
throw err;
|
|
894
940
|
};
|
|
895
941
|
};
|
|
@@ -931,16 +977,18 @@ class TXmlContentContainer {
|
|
|
931
977
|
}).catch(err => {
|
|
932
978
|
switch (err.code) {
|
|
933
979
|
case 'ENOENT':
|
|
934
|
-
case 'EISDIR':
|
|
980
|
+
case 'EISDIR': {
|
|
935
981
|
data.isERR = true;
|
|
936
982
|
data.errEvent = err.code;
|
|
937
983
|
resolve(data);
|
|
938
984
|
break;
|
|
939
|
-
|
|
985
|
+
}
|
|
986
|
+
default: {
|
|
940
987
|
console.log('CHECK: TXmlContentContainer.loadFromFile() => Error => '+err);
|
|
941
988
|
console.log('CHECK: TXmlContentContainer.loadFromFile() => Error => '+err.code);
|
|
942
989
|
reject(err);
|
|
943
990
|
break;
|
|
991
|
+
}
|
|
944
992
|
};
|
|
945
993
|
});
|
|
946
994
|
};
|
|
@@ -978,15 +1026,17 @@ class TXmlContentContainer {
|
|
|
978
1026
|
} catch (err) {
|
|
979
1027
|
switch (err.code) {
|
|
980
1028
|
case 'ENOENT':
|
|
981
|
-
case 'EISDIR':
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1029
|
+
case 'EISDIR': {
|
|
1030
|
+
data.isERR = true;
|
|
1031
|
+
data.errEvent = err.code;
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
default: {
|
|
1035
|
+
console.log('CHECK: TXmlContentContainer.loadFromFileSync() => Error => '+err);
|
|
1036
|
+
console.log('CHECK: TXmlContentContainer.loadFromFileSync() => Error => '+err.code);
|
|
1037
|
+
throw err;
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
990
1040
|
};
|
|
991
1041
|
};
|
|
992
1042
|
};
|
|
@@ -1001,8 +1051,8 @@ class TXmlContentContainer {
|
|
|
1001
1051
|
|
|
1002
1052
|
// === module exports block ===
|
|
1003
1053
|
|
|
1004
|
-
exports.DEF_XML_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS; // will deprecate
|
|
1005
|
-
exports.DEF_XML_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME; // will deprecate
|
|
1054
|
+
//exports.DEF_XML_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS; // will deprecate
|
|
1055
|
+
//exports.DEF_XML_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME; // will deprecate
|
|
1006
1056
|
|
|
1007
1057
|
exports.XML_DEF_PARSE_OPTIONS = XML_DEF_PARSE_OPTIONS;
|
|
1008
1058
|
exports.XML_DEF_ROOT_ETAG_NAME = XML_DEF_ROOT_ETAG_NAME;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cntwg/xml-lib-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "A library for handling an XML-documents",
|
|
5
5
|
"author": "ygracs <cs70th-om@rambler.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@ygracs/bsfoc-lib-js": "^0.1.2",
|
|
33
33
|
"@ygracs/xml-js6": "^0.0.3-b",
|
|
34
|
-
"@ygracs/xobj-lib-js": "^0.0.14-
|
|
34
|
+
"@ygracs/xobj-lib-js": "^0.0.14-rc3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"jest": "^29.4.2"
|