forked from dbetke/parse-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_xsd.js
More file actions
151 lines (134 loc) · 7.04 KB
/
parse_xsd.js
File metadata and controls
151 lines (134 loc) · 7.04 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
/*global phantom, require */
(function () {
"use strict";
var fs = require("fs"),
page = require("webpage").create(),
XSDPath = "test/mugl.xsd",
outputPath = "output/";
var xmlstring = fs.read(XSDPath);
phantom.onError = function(msg, trace) {
console.log(msg);
phantom.exit(1);
};
page.injectJs("lib/jquery/jquery-1.8.2.js");
var generateJSON = page.evaluate(function (xmlstring) {
var formatters = {
'xsd:string' : function (attr) {return '"' + attr + '"'; },
'xsd:integer' : function (attr) {return attr; },
'RGBColor' : function (attr) {return 'function () {return new window.multigraph.math.RGBColor.parse("' + attr + '"); }'; },
'Boolean' : function (attr) {return '"' + attr + '"'; },
'DPoint' : function (attr) {return 'function () { return new window.multigraph.math.Point(' + attr.replace(' ', ',') + '); }'; },
'Frame' : function (attr) {return '"' + attr + '"'; },
'xsd:double' : function (attr) {return attr; },
'DataType' : function (attr) {return '"' + attr + '"'; },
'Displacement' : function (attr) {return 'function () {return new window.multigraph.math.Displacement(' + attr + '); }'; },
'DPointOrNumber' : function (attr) {return 'function () {return new window.multigraph.math.Point(' + attr.replace(' ', ',') + '); }'; },
'DataOrAutoValue' : function (attr) {return '"' + attr + '"'; },
'DataValue' : function (attr) {return '"' + attr + '"'; },
'RendererType' : function (attr) {return 'function () {window.multigraph.core.Renderer.Type.parse("' + attr + '"); }'; },
'Comparator' : function (attr) {return '"' + attr + '"'; },
'Insets' : function (attr) {
if (typeof attr !== 'object') {
return 'function () {return new window.multigraph.math.Insets(/*top*/' + attr + ', /*left*/' + attr + ', /*bottom*/' + attr + ', /*right*/' + attr + '); }';
} else {
return 'function () {return new window.multigraph.math.Insets(/*top*/' + attr.values.top + ', /*left*/' + attr.values.left + ', /*bottom*/' + attr.values.bottom + ', /*right*/' + attr.values.right + '); }';
}
}
},
indent = " ";
function handleXML(xmlstring) {
var xmldoc = $.parseXML(xmlstring);
return processComplexType(xmldoc, $(xmldoc).find('group[name=GraphContent]'), 'foo');
} //end handleXML
function processComplexType(xmldoc, obj, name, prefix) {
var output = [],
partialObjects = {},
attrDefault,
attrName,
attrType,
$jstype,
jstypePartial,
jstypeType,
jstypeName,
jstypeValue;
if (prefix === undefined) {
prefix = "";
}
//for each object with specified name, find the attribute
obj.find('attribute').each(function () {
attrDefault = $(this).attr('default');
if ((attrDefault !== undefined) && (attrDefault !== 'unknown')) {
$jstype = $(this).find('jstype');
attrName = $(this).attr('name');
attrType = $(this).attr('type');
//check if jstype annotation
if ($jstype !== undefined && $jstype.length > 0) {
jstypePartial = $jstype.attr('partial');
jstypeType = $jstype.attr('type');
jstypeName = $jstype.attr('name');
jstypeValue = $jstype.attr('value');
//check if partial
if (jstypePartial === 'true') {
if (partialObjects[jstypeName] === undefined) {
partialObjects[jstypeName] = {'type': [jstypeType], 'values': {}};
}
//save to partial objects
partialObjects[jstypeName]['values'][jstypeValue] = attrDefault;
} else {
//if jstype but not partial, save new jstype attrType for special formatting rules
attrType = jstypeType;
try {
output.push(indent + prefix + '"' + attrName + '" : ' + formatters[attrType](attrDefault));
} catch (e) {
throw new Error("ERROR: " + e + "\nattribute name = " + attrName + " attribute type = " + attrType + " attribute default " + attrDefault);
}
}
} else {
try {
output.push(indent + prefix + '"' + attrName + '" : ' + formatters[attrType](attrDefault));
} catch (err) {
throw new Error("ERROR: " + err + "\nattribute name = " + attrName + " attribute type = " + attrType + " attribute default " + attrDefault);
}
}
}
});
//find each element and process it (for the attribute name, type, and default)
obj.find('element').each(function () {
var subObj,
subObjOutput;
try {
//if found, save subObj
subObj = $(xmldoc).find('complexType[name=' + $(this).attr('type') + ']');
} catch (e) {
//else do nothing
}
if (subObj !== undefined) {
//process subObj for attribute name, type, and default
subObjOutput = processComplexType(xmldoc, subObj, $(this).attr('name'), indent + prefix);
//test for blank string (instead of null value)
if (subObjOutput !== "") {
output.push(subObjOutput);
}
}
});
//adds partial objects to output
$.each(partialObjects, function (name, obj) {
output.push(indent + prefix + '"' + name + '" : ' + formatters[obj.type](obj, obj.values));
});
//generate results if output array is not empty
if (output.length > 0) {
return prefix + '"' + name + '"' + " : {\n" + output.join(",\n") + "\n" + prefix + "}";
} else {
return "";
}
}//end process complex type
return handleXML(xmlstring);
}, xmlstring);
//writes output
fs.makeTree(outputPath);
console.log("Writing defaults.js")
fs.write(outputPath + "defaults.js", generateJSON, "w");
window.setTimeout(function () {
phantom.exit();
}, 2000);
}());