-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSisulator.js
More file actions
320 lines (308 loc) · 13.3 KB
/
Sisulator.js
File metadata and controls
320 lines (308 loc) · 13.3 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// helper functions
function alert(str) {
WScript.Echo(str);
}
function exit(code) {
WScript.Quit(code);
}
// global variables
ERROR = 42;
VALID = 0;
var MAP = {
Anchor: {
description: 'models from Anchor Modeling, http://www.anchormodeling.com',
// name of the root element and resulting JSON object
root: 'schema',
// define 'keys' for elements that may occur more than once
// on the same level in the XML document
key: {
knot: function(xml, fragment) {
return fragment.getAttribute('mnemonic');
},
anchor: function(xml, fragment) {
return fragment.getAttribute('mnemonic');
},
attribute: function(xml, fragment) {
return fragment.getAttribute('mnemonic');
},
tie: function(xml, fragment) {
var roles = fragment.selectNodes('*[@role]');
var key = '', role;
for(var i = 0; i < roles.length; i++) {
role = roles.item(i);
key += role.getAttribute('type') + '_' + role.getAttribute('role');
if(i < roles.length - 1) key += '_';
}
return key;
},
anchorRole: function(xml, fragment) {
return fragment.getAttribute('type') + '_' + fragment.getAttribute('role');
},
knotRole: function(xml, fragment) {
return fragment.getAttribute('type') + '_' + fragment.getAttribute('role');
}
},
// used to replace certain element names with others
replacer: function(name) {
switch(name) {
case 'anchorRoles':
return 'roles';
case 'knotRoles':
return 'roles';
default:
return name;
}
}
},
Workflow: {
description: 'workflow for SQL Server Job Agent',
root: 'workflow',
key: {
job: function(xml, fragment) {
return fragment.getAttribute('name');
},
jobstep: function(xml, fragment) {
return fragment.getAttribute('name');
},
variable: function(xml, fragment) {
return fragment.getAttribute('name');
}
}
},
Source: {
description: 'source data format description',
root: 'source',
key: {
part: function(xml, fragment) {
return fragment.getAttribute('name');
},
term: function(xml, fragment) {
return fragment.getAttribute('name');
},
key: function(xml, fragment) {
return fragment.getAttribute('name');
},
component: function(xml, fragment) {
return fragment.getAttribute('of');
},
calculation: function(xml, fragment) {
return fragment.getAttribute('name');
}
}
},
Target: {
description: 'target loading description',
root: 'target',
key: {
map: function(xml, fragment) {
return fragment.getAttribute('source') + '__' + fragment.getAttribute('target');
},
condition: function(xml, fragment) {
return 'singleton'; // there can be only one!
},
load: function(xml, fragment) {
var pass = fragment.getAttribute('pass');
pass = pass ? '__' + pass : '';
return fragment.getAttribute('source') + '__' + fragment.getAttribute('target') + pass;
},
sql: function(xml, fragment) {
return fragment.getAttribute('position');
}
}
}
}
var Sisulator = {
// this function will recursively traverse the XML document and
// create a 'hash' object that mimics the structure using the given map
// to handle siblings using the same tag
objectify: function(xml, map) {
var listSuffix = 's';
function objectifier(xmlFragment, map, object) {
// element node
if(xmlFragment.nodeType === 1) {
// if there are children or attributes we need a container
if(xmlFragment.attributes.length > 0 || xmlFragment.firstChild) {
if(!object[xmlFragment.nodeName])
object[xmlFragment.nodeName] = new Object();
var partialObject = object[xmlFragment.nodeName];
if(typeof map.key[xmlFragment.nodeName] === 'function') {
var key = map.key[xmlFragment.nodeName](xml, xmlFragment);
if(key) {
partialObject = partialObject[key] = new Object();
partialObject.id = key;
var name = xmlFragment.nodeName + listSuffix;
name = map.replacer ? map.replacer(name) : name;
// reference the object from the array
if(!object[name])
object[name] = [];
object[name].push(key);
}
}
// process attributes
if (xmlFragment.attributes.length > 0) {
for (var j = 0; j < xmlFragment.attributes.length; j++) {
var attribute = xmlFragment.attributes.item(j);
partialObject[attribute.nodeName] = attribute.nodeValue;
}
}
// process children
var child = xmlFragment.firstChild;
if(child) objectifier(child, map, partialObject);
}
}
// text node
else if(xmlFragment.nodeType === 3) {
// add content with underscore naming
if(xmlFragment.nodeValue)
object['_' + xmlFragment.parentNode.nodeName] = xmlFragment.nodeValue;
}
// process siblings
var sibling = xmlFragment.nextSibling;
if(sibling) objectifier(sibling, map, object);
return object;
}
// just initialize and return the result
return objectifier(xml.documentElement, map, {});
},
sisulate: function(xml, map, directive) {
// objectify the xml
var jsonObject = Sisulator.objectify(xml, map);
// consistent with other line breaks and without comments
jsonObject[map.root]._xml = xml.xml.replace(/(\r\n|\n|\r)/g, '\n').replace(/<!--[\s\S]*?-->/g, '');
eval("var " + map.root + " = jsonObject." + map.root);
// this variable holds the result
var _sisula_ = '';
// process and evaluate all sisulas in the directive
var reader = new ActiveXObject("Scripting.FileSystemObject");
var file = reader.OpenTextFile(directive);
var scripts = file.ReadAll();
file.Close();
scripts = scripts.replace(/\r/g, ''); // unify line breaks
// only non-empty lines that are not comments (starts with #)
scripts = scripts.match(/^[^#].+/gm);
if(scripts) {
var script;
var splitter = /\/\*~|~\*\//g; // split JS /*~ sisula template ~*/ JS
// process every sisula
for(var scriptIndex = 0; script = scripts[scriptIndex]; scriptIndex++) {
script = script.replace(/^\s+/,'').replace(/\s+$/,''); // trim
file = reader.OpenTextFile(script);
var sisula = file.ReadAll();
file.Close();
// make sure everything starts with JavaScript (empty row)
sisula = '\n' + sisula;
// split language into JavaScript and SQL template components
var sisulets = sisula.split(splitter);
// substitute from SQL template to JavaScript
for(var i = 1; i < sisulets.length; i+=2) {
// honor escaped dollar signs
sisulets[i] = sisulets[i].replace(/[$]{2}/g, '§DOLLAR§'); // escaping dollar signs
sisulets[i] = sisulets[i].replace(/["]{2}/g, '§DOUBLE§'); // escaping double quotes
sisulets[i] = sisulets[i].replace(/["]{1}/g, '§SINGLE§'); // escaping single quotes
sisulets[i] = sisulets[i].replace(/[$]{([\S\s]*?)}[$]/g, '" + $1 + "'); // multi-expression
sisulets[i] = sisulets[i].replace(/[$]\(([\S\s]*?)\)\?[^\S\n]*([^:\n]*)[:]?[^\S\n]*(.*)/g, '" + ($1 ? "$2" : "$3") + "'); // conditional
sisulets[i] = sisulets[i].replace(/[\$]([\w.]*?)(?:([\$])|([^\w.]|$))/g, '" + ($1 ? $1 : "") + "$3'); // single
sisulets[i] = sisulets[i].replace(/(\r\n|\n|\r)/g, '\\n" +\n'); // line breaks
sisulets[i] = sisulets[i].replace(/^/gm, '"'); // start of line
sisulets[i] = '_sisula_+=' + sisulets[i] + '";'; // variable assignment
}
// join the parts together again (now all JavaScript)
sisula = sisulets.join('');
try {
// this eval needs schema and _sisula_ to be defined
eval(sisula);
}
catch(e) {
alert('Error in script: ' + script);
alert(sisula); // alert was used for debugging sisula code
throw e;
}
}
_sisula_ = _sisula_.replace(/§DOLLAR§/g, '$'); // unescaping dollar signs
_sisula_ = _sisula_.replace(/§SINGLE§/g, '\"'); // unescaping double quotes
_sisula_ = _sisula_.replace(/§DOUBLE§/g, '"'); // unescaping double quotes
_sisula_ = _sisula_.replace(/^\s*[\r\n]/gm, ''); // remove empty lines
_sisula_ = _sisula_.replace(/(\S+[^\S\n])(?:[^\S\n]+)/gm, '$1'); // consume multiple spaces, but not indentation
}
return _sisula_;
}
};
// This is the main() where we start with parsing the command line
var commandLineOptions = {
_opts_: {
// option, description, mandatory
xmlFilename: ['-x', 'XML file made available as a JSON object in the sisulets.', true],
mappingType: ['-m', 'XML to object mapping type.', true],
directiveFilename: ['-d', 'Directive file specifying a list of sisulets that will be processed.', true],
outputFilename: ['-o', 'Output file where the result is stored.', false]
}
};
var o, option, description, map, list = 'Command line options:';
// if no arguments were provided then list the options and exit
if(WScript.Arguments.length == 0) {
for(o in commandLineOptions._opts_) {
option = commandLineOptions._opts_[o][0];
description = commandLineOptions._opts_[o][1];
list += '\n' + option + '\t' + description;
}
list += '\n\nAvailable mapping types:'
for(map in MAP) {
list += '\n' + map + ', ' + MAP[map].description;
}
alert(list);
exit(ERROR);
}
// ergo, we have arguments, so make them a regular array
var i, commandLineArguments = [];
for(i = 0; i < WScript.Arguments.length; i++)
commandLineArguments.push(WScript.Arguments.Item(i));
// parse the given arguments
var pattern, argument, delim = ';';
var joinedArguments = commandLineArguments.join(delim);
for(o in commandLineOptions._opts_) {
option = commandLineOptions._opts_[o][0];
if(joinedArguments.indexOf(option) >= 0) {
pattern = new RegExp(option + delim + '([^' + delim + ']*)');
argumentMatch = joinedArguments.match(pattern);
if(argumentMatch) {
commandLineOptions[o] = argumentMatch[1];
}
}
}
// check mandatory options and exit if not provided
var mandatory;
for(o in commandLineOptions._opts_) {
mandatory = commandLineOptions._opts_[o][2];
if(mandatory) {
if(!commandLineOptions[o] || commandLineOptions[o].length == 0) {
option = commandLineOptions._opts_[o][0];
description = commandLineOptions._opts_[o][1];
alert("The option " + option + " is mandatory with the description:\n" + description);
exit(ERROR);
}
}
}
// instantiate a DOM object at run time
var xmlObject = new ActiveXObject("msxml2.DOMDocument.6.0");
xmlObject.async = false;
xmlObject.resolveExternals = false;
xmlObject.validateOnParse = false;
// load an XML file into the DOM instance
if(!xmlObject.load(commandLineOptions.xmlFilename)) {
alert("The XML file: " + commandLineOptions.xmlFilename + " could not be loaded");
exit(ERROR);
}
// objectify the XML and apply the sisulets to get some output text
var sisulaOutput = Sisulator.sisulate(
xmlObject,
MAP[commandLineOptions.mappingType],
commandLineOptions.directiveFilename
);
// save the output
var outputFilename = commandLineOptions.outputFilename || commandLineOptions.directiveFilename + '.output';
var writer = new ActiveXObject("Scripting.FileSystemObject");
var outputFile = writer.CreateTextFile(outputFilename, true);
outputFile.Write(sisulaOutput);
outputFile.Close();
// the end
exit(VALID);