|
1
|
|
|
import xmljs from 'xml-js'; |
|
2
|
|
|
import Store from '../Model/Store'; |
|
3
|
|
|
|
|
4
|
|
|
import {autoCast, castString} from '../lib/utils'; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
export default class IclsParser { |
|
8
|
|
|
constructor () { |
|
9
|
6 |
|
this._store = new Store(); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
_convertToUnderstandable (xml) { |
|
13
|
6 |
|
return xmljs.xml2js(xml); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
_buildTree (iclsXmlObj) { |
|
17
|
6 |
|
let rootNode = iclsXmlObj.elements[0]; |
|
18
|
6 |
|
this._readTree(rootNode); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
_readTree (el, ancestors = []) { |
|
22
|
127 |
|
if (Array.isArray(el)) { |
|
23
|
45 |
|
for (let element of el) { |
|
24
|
76 |
|
this._readTree(element, ancestors); |
|
25
|
|
|
} |
|
26
|
45 |
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
82 |
|
this._parseSchemeNode(el, ancestors); |
|
30
|
82 |
|
this._parseOptionNode(el, ancestors); |
|
31
|
82 |
|
this._parseColorNode(el, ancestors); |
|
32
|
82 |
|
this._parseAttributeNode(el, ancestors); |
|
33
|
82 |
|
this._parseChildNodes(el, ancestors); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
_prepareNodeValues (values) { |
|
37
|
46 |
|
return Object.keys(values).reduce((accumulator, curr) => { |
|
38
|
48 |
|
accumulator[castString(curr)] = autoCast(values[curr]); |
|
39
|
48 |
|
return accumulator; |
|
40
|
|
|
}, {}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
_parseSchemeNode (el, ancestors) { |
|
45
|
82 |
|
if (el.name !== 'scheme' || el.attributes === undefined) { |
|
46
|
80 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
this._setInStore(el.attributes, ancestors); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
_parseOptionNode (el, ancestors) { |
|
53
|
82 |
|
if (el.name !== 'option' || el.attributes === undefined) { |
|
54
|
30 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
52 |
|
if (el.attributes.value === undefined && el.elements !== undefined) { |
|
58
|
8 |
|
this._readTree(el.elements, [ |
|
59
|
|
|
...ancestors, |
|
60
|
|
|
el.attributes.name |
|
61
|
|
|
]); |
|
62
|
8 |
|
return; |
|
63
|
|
|
} |
|
64
|
44 |
|
let attrs = {}; |
|
65
|
44 |
|
attrs[el.attributes.name] = el.attributes.value; |
|
66
|
44 |
|
this._setInStore(attrs, ancestors); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
_parseColorNode (el, ancestors) { |
|
70
|
82 |
|
if (el.name !== 'colors' || el.elements === undefined) { |
|
71
|
80 |
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
this._readTree( |
|
75
|
|
|
el.elements, |
|
76
|
|
|
[ |
|
77
|
|
|
...ancestors, |
|
78
|
|
|
'colors' |
|
79
|
|
|
] |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
_parseAttributeNode (el, ancestors) { |
|
84
|
82 |
|
if (el.name !== 'attributes' || el.elements === undefined) { |
|
85
|
80 |
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
this._readTree(el.elements, ancestors); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
_parseChildNodes (el, ancestors) { |
|
92
|
82 |
|
if (el.elements === undefined) { |
|
93
|
49 |
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
33 |
|
this._readTree(el.elements, ancestors); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
getStore () { |
|
100
|
6 |
|
return this._store; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
parse (iclsXmlString) { |
|
104
|
6 |
|
const iclsJsObj = this._convertToUnderstandable(iclsXmlString); |
|
105
|
6 |
|
this._buildTree(iclsJsObj); |
|
106
|
6 |
|
return this.getStore(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
_setInStore (attrs, ancestors) { |
|
110
|
46 |
|
attrs = this._prepareNodeValues(attrs); |
|
111
|
46 |
|
for (let key in attrs) { |
|
112
|
48 |
|
let storeKey = this._getStoreKey(key, ancestors); |
|
113
|
48 |
|
this._store.set(storeKey, attrs[key]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
_getStoreKey (key, ancestors) { |
|
118
|
48 |
|
let path = [ |
|
119
|
|
|
...ancestors, |
|
120
|
|
|
key |
|
121
|
|
|
]; |
|
122
|
48 |
|
return castString(path.join('.')); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|