-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessors.js
231 lines (195 loc) · 8.13 KB
/
processors.js
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
var fs = require('fs');
var Element = function(){
this.elementName;
this.elements = [];
this.attributes = [];
this.annotation;
this.pElement;
this.minOccurs;
this.maxOccurs;
this.value;
};
var Attribute = function () {
this.attributeName;
this.defaultValue;
this.type;
this.annotation;
this.pElement;
this.value;
this.restriction = [];
//will need to handle restrictions as well
};
var xsdProcessor = {
readXsd:function (){
//https://github.com/Leonidas-from-XIV/node-xml2js
var parseString = require('xml2js').parseString;
var contents = fs.readFileSync("./newrelic.xsd", "utf8");
var myresults;
parseString(contents, function (err, result) {
myresults = result;
});
//what the loop sees
//console.log(myresults['xs:schema']['xs:element'][0]);
return xsdProcessor.processXsd(myresults['xs:schema'], null);
},
processXsd : function (xsValue, element){
//this reads the xml and sends our the individual elements for additional processing
if(xsValue.hasOwnProperty("xs:element")){
//build the configuration element by hand since it will always exist.
var xsElement = xsValue["xs:element"][0];
var element = new Element();
element.elementName = xsElement["$"].name;
element.annotation = xsdProcessor.getAnnotation(xsElement);
xsdProcessor.setOccurs(xsElement, element);
var complex = xsElement["xs:complexType"][0];
xsdProcessor.processAttributes(complex, element);
var innerNode = complex["xs:all"][0];
for(var i = 0;i<innerNode["xs:element"].length;i++){
xsdProcessor.processElement(innerNode["xs:element"][i], element);
}
return element;
}
},
processElement : function (xsElement, pElement){
//console.log(xsElement["$"].name);
//this == parent element
//setup the element being created this pass
var element = new Element();
if(typeof pElement !== "undefined"){
element.pElement = pElement;
}
//gets the name of the element
element.elementName = xsElement["$"].name;
//capture annotations
element.annotation = xsdProcessor.getAnnotation(xsElement);
//set min and max Occurs
xsdProcessor.setOccurs(xsElement, element);
//pull the complexType out
var complex;
if(xsElement.hasOwnProperty("xs:complexType")) {
complex = xsElement["xs:complexType"][0];
}
if(typeof complex !== "undefined"){
xsdProcessor.processAttributes(complex, element);
//discover and select the new level then loop over the child elements
var innerNode;
if(complex.hasOwnProperty("xs:all")) innerNode = complex["xs:all"][0];
else if(complex.hasOwnProperty("xs:sequence")) innerNode = complex["xs:sequence"][0];
if(typeof innerNode !== "undefined" && innerNode.hasOwnProperty("xs:element"))
for(var i = 0;i<innerNode["xs:element"].length;i++){
xsdProcessor.processElement(innerNode["xs:element"][i], element);
}
}
//add to parent if not empty
if(typeof pElement !== "undefined" && pElement.elementName) pElement.elements.push(element);
//output for testing
//console.log(element);
},
processAttributes : function (complex, element){
//use this. to access element
if(!complex.hasOwnProperty("xs:attribute")) return;
var attributesArray = complex["xs:attribute"];
for(var i = 0;i < attributesArray.length;i++){
var attribute = new Attribute();
attribute.attributeName = attributesArray[i]["$"].name;
attribute.pElement = element;
attribute.annotation = xsdProcessor.getAnnotation(attributesArray[i]);
attribute.defaultValue = attributesArray[i]["$"]["default"];
if(attributesArray[i].hasOwnProperty("xs:simpleType")){
attribute.type = "restriction";
attributesArray[i]["xs:simpleType"][0]["xs:restriction"][0]["xs:enumeration"].forEach(function(enumeration){
attribute.restriction.push(enumeration["$"]["value"]);
});
}
else{
attribute.type = attributesArray[i]["$"].type;
}
element.attributes.push(attribute);
}
},
getAnnotation : function (xsItem){
//capture annotations
if(xsItem.hasOwnProperty("xs:annotation")) {
return xsItem["xs:annotation"][0]["xs:documentation"][0];
}
if(!xsItem.hasOwnProperty("xs:complexType")) return;
if(xsItem["xs:complexType"][0].hasOwnProperty("xs:annotation")){
return xsItem["xs:complexType"][0]["xs:annotation"][0]["xs:documentation"][0];
}
},
setOccurs : function (xsElement, element){
element.minOccurs = (xsElement["$"].hasOwnProperty("minOccurs")) ? parseInt(xsElement["$"]["minOccurs"]) : 1;
element.maxOccurs = (xsElement["$"].hasOwnProperty("maxOccurs")) ? parseInt(xsElement["$"]["maxOccurs"]) : 1;
//fix NaN from unbounded maxOccurs
element.maxOccurs = (isNaN(element.maxOccurs)) ? 10000 : element.maxOccurs;
}
};
var configProcessor = {
readXml:function (){
//https://github.com/Leonidas-from-XIV/node-xml2js
var parseString = require('xml2js').parseString;
var contents = fs.readFileSync("./newrelic.config", "utf8");
var myresults;
parseString(contents, function (err, result) {
myresults = result;
});
//what the loop sees
//console.log(myresults);
return configProcessor.processXml(myresults, null);
},
processXml : function (xsValue, element){
//this reads the xml and sends our the individual elements for additional processing
if(xsValue.hasOwnProperty("configuration")){
var element = new Element();
element.elementName = "configuration";
//attributes
configProcessor.processAttributes(xsValue["configuration"]["$"],element);
for(var prop in xsValue["configuration"]){
if(prop != "$") configProcessor.processElement(xsValue["configuration"][prop][0], prop, element);
}
}
return element;
},
processElement : function (xsElement, name, pElement) {
//setup the element being created this pass
var element = new Element();
if(name != "configuration")element.pElement = pElement;
//gets the name of the element
element.elementName = name;
//attributes
if(xsElement.hasOwnProperty("$")) configProcessor.processAttributes(xsElement["$"],element);
//child elements
for(var prop in xsElement){
if(prop != "$" && typeof xsElement[prop] != "string"){
for(var i = 0;i < xsElement[prop].length; i++){
configProcessor.processElement(xsElement[prop][i], prop, element);
}
}
}
//console.log(xsValue);
if(typeof xsElement == "string") element.value = xsElement;
//add to parent if not empty
if(pElement.elementName) pElement.elements.push(element);
},
processAttributes : function (xsElement, element){
for(var prop in xsElement){
var attribute = new Attribute();
attribute.attributeName = prop;
attribute.value = xsElement[prop]
element.attributes.push(attribute);
}
}
};
function getElementByPath(path, element){
var steps = path.split('.');
if(steps[steps.length - 1] == "configuration") steps.pop();
var itemToFind = steps.pop();
var selectedElement = element;
for(var i =0;i<element.elements.length;i++){
if(element.elements[i].elementName == itemToFind) {
selectedElement = getElementByPath(steps.join('.'), element.elements[i]);
break;
}
}
return selectedElement;
}