/
shapeDefCollectionParser.js
673 lines (558 loc) · 24.4 KB
/
shapeDefCollectionParser.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright (c) Evolus Solutions. All rights reserved.
// License: GPL/MPL
// $Id$
/* class */ function ShapeDefCollectionParser() {
}
ShapeDefCollectionParser.prototype.injectEntityDefs = function (content, file) {
//getting the current local
var locale = Config.getLocale();
var dtdFile = file.parent.clone();
debug("Locale: " + locale + ", appending to: " + dtdFile.path);
dtdFile.append(locale + ".dtd");
if (!dtdFile.exists()) {
dtdFile = file.parent.clone();
dtdFile.append("default.dtd");
if (!dtdFile.exists()) {
dtdFile = file.parent.clone();
dtdFile.append("en-US.dtd");
if (!dtdFile.exists()) {
return content;
}
}
}
var dtdContent = FileIO.read(dtdFile, ShapeDefCollectionParser.CHARSET);
var doctypeContent = "<!DOCTYPE Shapes [\n" + dtdContent + "\n]>\n";
content = content.replace(/(<Shapes)/, function (zero, one) {
return doctypeContent + one;
});
return content;
};
ShapeDefCollectionParser.prototype.injectEntityDefsFromUrl = function (content, url) {
try {
//getting the current local
var locale = "en-US";
var dtdUrl = url.replace(/Definition\.xml$/, locale + ".dtd");
var request = new XMLHttpRequest();
request.open("GET", dtdUrl, false);
request.send("");
var doctypeContent = request.responseText;
if (doctypeContent && doctypeContent.length && request.status != 404) {
doctypeContent = "<!DOCTYPE Shapes [\n" + doctypeContent + "\n]>\n";
content = content.replace(/(<Shapes)/, function (zero, one) {
return doctypeContent + one;
});
}
} catch (ex) {
}
return content;
};
ShapeDefCollectionParser.CHARSET = "UTF-8";
ShapeDefCollectionParser.getCollectionPropertyConfigName = function (collectionId, propName) {
return "Collection." + collectionId + ".properties." + propName;
};
/* public ShapeDefCollection */ ShapeDefCollectionParser.prototype.parseURL = function (url) {
try {
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send("");
var content = this.injectEntityDefsFromUrl(request.responseText, url)
var domParser = new DOMParser();
var dom = domParser.parseFromString(content, "text/xml");
var collection = this.parse(dom, url);
collection._location = url;
collection.installDirPath = path.dirname(url);
return collection;
} catch (e) {
Console.dumpError(e, "stdout");
error(e);
}
};
/* void */ ShapeDefCollectionParser.prototype.parseURLAsync = function (url, callback) {
try {
var thiz = this;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function (event) {
if (request.readyState != 4) return;
if (request.status != 200 && request.status != 0) {
callback(null, "file loading failed");
return;
}
try {
var content = thiz.injectEntityDefsFromUrl(request.responseText, url)
var domParser = new DOMParser();
dom = domParser.parseFromString(content, "text/xml");
var collection = thiz.parse(dom, url);
collection._location = url;
callback(collection);
} catch (ex) {
callback(null, "Parsing failed, " + ex);
}
};
request.send(null);
} catch (e) {
callback(null, "Unknown error loading the collection, " + e);
Console.dumpError(e, "stdout");
error(e);
}
};
/* public ShapeDefCollection */ ShapeDefCollectionParser.prototype.parseFile = function (file, uri) {
try {
var fileContents = FileIO.read(file, ShapeDefCollectionParser.CHARSET);
var domParser = new DOMParser();
fileContents = this.injectEntityDefs(fileContents, file)
var dom = domParser.parseFromString(fileContents, "text/xml");
var collection = this.parse(dom, uri);
collection._location = uri;
return collection;
} catch (e) {
Console.dumpError(e, "stdout");
}
};
ShapeDefCollectionParser.prototype.loadCustomLayout = function (installDirPath) {
var layoutUri = path.join(installDirPath, "Layout.xhtml");
if (!fs.existsSync(layoutUri)) return null;
try {
var html = fs.readFileSync(layoutUri, {encoding: "utf8"});
if (!html) return null;
var dom = Dom.parseDocument(html);
var container = Dom.getSingle("/html:html/html:body", dom);
if (!container) container = dom.documentElement;
var div = dom.createElementNS(PencilNamespaces.html, "div");
while (container.firstChild) {
var n = container.firstChild;
container.removeChild(n);
div.appendChild(n);
}
Dom.workOn("//html:img[@src]", div, function (image) {
var src = image.getAttribute("src");
if (src && src.indexOf("data:image") != 0) {
var parts = src.split("/");
src = installDirPath;
for (var p of parts) src = path.join(src, p);
image.setAttribute("src", src);
}
});
return document.importNode(div, true);
} catch (ex) {
console.error(ex);
}
return null;
};
/* public ShapeDefCollection */ ShapeDefCollectionParser.prototype.parse = function (dom, uri) {
var collection = new ShapeDefCollection();
collection.url = uri ? uri : dom.documentURI;
collection.installDirPath = path.dirname(uri);
collection.customLayout = this.loadCustomLayout(collection.installDirPath);
var s1 = collection.url.toString();
var s2 = window.location.href.toString();
var max = Math.min(s1.length, s2.length);
var i = 0;
for (i = 0; i < max; i++) {
if (s1[i] != s2[i] ) break;
}
collection.relURL = s1.substring(i);
var shapeDefsNode = dom.documentElement;
collection.id = shapeDefsNode.getAttribute("id");
collection.displayName = shapeDefsNode.getAttribute("displayName");
collection.description = shapeDefsNode.getAttribute("description");
collection.author = shapeDefsNode.getAttribute("author");
collection.infoUrl = shapeDefsNode.getAttribute("url");
collection.system = shapeDefsNode.getAttribute("system") == "true";
collection.fonts = [];
Dom.workOn("./p:Script", shapeDefsNode, function (scriptNode) {
var context = { collection: collection };
try {
pEval(scriptNode.textContent, context, "COLLECTION_SCRIPT: " + collection.displayName + ", " + collection.relURL + " (" + scriptNode.getAttribute("comments") + ")");
} catch (e) {
console.error("Collection script evaluation failed: " + collection.displayName, e);
}
});
Dom.workOn("./p:Fonts/p:Font", shapeDefsNode, function (fontNode) {
var font = {
name: fontNode.getAttribute("name")
};
for (var variantName in FontRepository.SUPPORTED_VARIANTS) {
var filePath = fontNode.getAttribute(variantName);
if (filePath) font[variantName] = filePath;
}
console.log("Font:", font);
collection.fonts.push(font);
});
this.parseCollectionProperties(shapeDefsNode, collection);
var parser = this;
Dom.workOn("./p:Shape | ./p:Shortcut", shapeDefsNode, function (node) {
if (node.localName == "Shape") {
collection.addDefinition(parser.parseShapeDef(node, collection));
} else {
collection.addShortcut(parser.parseShortcut(node, collection));
}
});
return collection;
};
/* private void */ ShapeDefCollectionParser.prototype.parseCollectionProperties = function (shapeDefsNode, collection) {
Dom.workOn("./p:Properties/p:PropertyGroup", shapeDefsNode, function (propGroupNode) {
var group = new PropertyGroup;
group.name = propGroupNode.getAttribute("name");
Dom.workOn("./p:Property", propGroupNode, function (propNode) {
var property = new Property();
property.name = propNode.getAttribute("name");
property.displayName = propNode.getAttribute("displayName");
var type = propNode.getAttribute("type");
try {
property.type = window[type];
} catch (e) {
alert(e);
throw Util.getMessage("invalid.property.type", type);
}
var literal = Dom.getText(propNode);
property.initialValue = property.type.fromString(Dom.getText(propNode));
try {
var s = Config.get(ShapeDefCollectionParser.getCollectionPropertyConfigName (collection.id, property.name));
if (typeof(s) != "undefined" && s != null) {
property.value = property.type.fromString(s);
} else {
property.value = property.initialValue;
}
} catch (e) {
property.value = property.initialValue;
}
//parsing meta
Dom.workOn("./@p:*", propNode, function (metaAttribute) {
var metaValue = metaAttribute.nodeValue;
metaValue = metaValue.replace(/\$([a-z][a-z0-9]*)/gi, function (zero, one) {
property.relatedProperties[one] = true;
return "properties." + one;
});
property.meta[metaAttribute.localName] = metaValue;
});
group.properties.push(property);
collection.properties[property.name] = property;
});
collection.propertyGroups.push(group);
});
/*/ styles
Dom.workOn("./p:Styles/p:Group", shapeDefsNode, function (styleGroupNode) {
var group = new StyleGroup;
group.name = styleGroupNode.getAttribute("name");
Dom.workOn("./p:Style", styleGroupNode, function (styleNode) {
var style = new Style();
style.name = styleNode.getAttribute("name");
style.iconPath = styleNode.getAttribute("icon");
if (style.iconPath && style.iconPath.indexOf("data:image") != 0) {
style.iconPath = collection.url.substring(0, collection.url.lastIndexOf("/") + 1) + style.iconPath;
}
Dom.workOn("./p:Property", styleNode, function (propNode) {
var property = new Property();
property.name = propNode.getAttribute("name");
property.displayName = propNode.getAttribute("displayName");
var type = propNode.getAttribute("type");
try {
property.type = window[type];
} catch (e) {
throw "Invalid property type: " + type;
}
var valueElement = Dom.getSingle("./p:*", propNode);
if (valueElement) {
if (valueElement.localName == "Null") {
property.value = null;
}
} else {
property.value = property.type.fromString(Dom.getText(propNode));
}
style.properties[property.name] = property;
});
group.styles[style.name] = style;
});
collection.styleGroups[group.name] = group;
});
//debug(collection.styleGroups);/*/
};
/* public ShapeDef */ ShapeDefCollectionParser.prototype.parseShapeDef = function (shapeDefNode, collection) {
var shapeDef = new ShapeDef();
shapeDef.id = collection.id + ":" + shapeDefNode.getAttribute("id");
shapeDef.displayName = shapeDefNode.getAttribute("displayName");
shapeDef.system = shapeDefNode.getAttribute("system") == "true";
shapeDef.collection = collection;
var inherits = shapeDefNode.getAttribute("inherits");
if (inherits) {
if (inherits.indexOf(":") < 0) inherits = collection.id + ":" + inherits;
var parentShapeDef = collection.shapeDefMap[inherits];
if (parentShapeDef) {
shapeDef.parentShapeDef = parentShapeDef;
this.processInheritance(shapeDef);
}
}
var iconPath = shapeDefNode.getAttribute("icon");
// if (iconPath.indexOf("data:image") != 0) {
// iconPath = collection.url.substring(0, collection.url.lastIndexOf("/") + 1) + iconPath;
// }
shapeDef.iconPath = iconPath;
// adding shapeDef meta
shapeDef.meta = {};
Dom.workOn("./@p:*", shapeDefNode, function (metaAttribute) {
var metaValue = metaAttribute.nodeValue;
metaValue = metaValue.replace(/\$([a-z][a-z0-9]*)/gi, function (zero, one) {
property.relatedProperties[one] = true;
return "properties." + one;
});
shapeDef.meta[metaAttribute.localName] = metaValue;
});
var parser = this;
//parse properties
Dom.workOn("./p:Properties/p:PropertyGroup", shapeDefNode, function (propGroupNode) {
//find existing property group to support duplicate inherited groups
var groupName = propGroupNode.getAttribute("name");
var group = null;
for (var g of shapeDef.propertyGroups) {
if (g.name == groupName) {
group = g;
break;
}
}
if (!group) {
group = new PropertyGroup();
group.name = groupName;
shapeDef.propertyGroups.push(group);
}
Dom.workOn("./p:Property", propGroupNode, function (propNode) {
var property = new Property();
property.name = propNode.getAttribute("name");
property.displayName = propNode.getAttribute("displayName");
if(propNode.getAttribute("reload")) property.reload = propNode.getAttribute("reload");
var type = propNode.getAttribute("type");
try {
property.type = window[type];
if (!property.type) {
console.error("Unknown type: " + type + ", in " + collection.displayName);
}
} catch (e) {
alert(e);
throw Util.getMessage("invalid.property.type", type);
}
var valueElement = Dom.getSingle("./p:*", propNode);
if (valueElement) {
if (valueElement.localName == "E") {
var expression = Dom.getText(valueElement);
expression = expression.replace(/\$\$([a-z][a-z0-9]*)/gi, function (zero, one) {
return "collection.properties." + one + ".value";
});
property.initialValueExpression = expression;
} else if (valueElement.localName == "Null") {
property.initialValue = null;
}
} else {
property.initialValue = property.type.fromString(Dom.getText(propNode));
}
property.relatedProperties = {};
//parsing meta
Dom.workOn("./@p:*", propNode, function (metaAttribute) {
var metaValue = metaAttribute.nodeValue;
metaValue = metaValue.replace(/\$([a-z][a-z0-9]*)/gi, function (zero, one) {
property.relatedProperties[one] = true;
return "properties." + one;
});
property.meta[metaAttribute.localName] = metaValue;
});
if (shapeDef.propertyMap[property.name]) {
shapeDef.removeProperty(property.name);
}
group.properties.push(property);
shapeDef.propertyMap[property.name] = property;
});
});
/*/ styles
for (var kk in collection.styleGroups) {
shapeDef.styleGroups[kk] = collection.styleGroups[kk];
}
Dom.workOn("./p:Styles/p:Group", shapeDefNode, function (styleGroupNode) {
var name = styleGroupNode.getAttribute("name");
var group = new StyleGroup;
if (shapeDef.styleGroups[name]) {
group = shapeDef.styleGroups[name];
}
group.name = name;
Dom.workOn("./p:Style", styleGroupNode, function (styleNode) {
var style = new Style();
var name = styleNode.getAttribute("name");
if (group.styles[name]) {
style = group.styles[name];
}
style.name = styleNode.getAttribute("name");
style.iconPath = styleNode.getAttribute("icon");
if (style.iconPath && style.iconPath.indexOf("data:image") != 0) {
style.iconPath = collection.url.substring(0, collection.url.lastIndexOf("/") + 1) + style.iconPath;
}
Dom.workOn("./p:Property", styleNode, function (propNode) {
var property = new Property();
var name = propNode.getAttribute("name");
if (style.properties[name]) {
property = style.properties[name];
}
property.name = propNode.getAttribute("name");
property.displayName = propNode.getAttribute("displayName");
var type = propNode.getAttribute("type");
try {
property.type = window[type];
} catch (e) {
alert(e);
throw "Invalid property type: " + type;
}
var valueElement = Dom.getSingle("./p:*", propNode);
if (valueElement) {
if (valueElement.localName == "Null") {
property.value = null;
}
} else {
property.value = property.type.fromString(Dom.getText(propNode));
}
style.properties[property.name] = property;
});
group.styles[style.name] = style;
});
shapeDef.styleGroups[group.name] = group;
});
8*/
//parse behaviors
Dom.workOn("./p:Behaviors/p:For", shapeDefNode, function (forNode) {
var targets = forNode.getAttribute("ref");
if (targets) {
targets = targets.split(",");
for (var t = 0; t < targets.length; t++) {
var behavior = new Behavior();
behavior.target = targets[t];
shapeDef.behaviorMap[behavior.target] = behavior;
Dom.workOn("./p:*", forNode, function (behaviorItemNode) {
var item = new BehaviorItem();
item.handler = Pencil.behaviors[behaviorItemNode.localName];
var count = Dom.workOn("./p:Arg", behaviorItemNode, function (argNode) {
item.args.push(new BehaviorItemArg(Dom.getText(argNode), shapeDef, behavior.target, argNode.getAttribute("literal")));
});
if (count == 0) {
var text = Dom.getText(behaviorItemNode);
item.args.push(new BehaviorItemArg(text, shapeDef, behavior.target, null));
}
behavior.items.push(item);
});
shapeDef.behaviors.push(behavior);
}
}
});
//parsing actions
Dom.workOn("./p:Actions/p:Action", shapeDefNode, function (actionNode) {
var action = new ShapeAction();
action.id = actionNode.getAttribute("id");
action.displayName = actionNode.getAttribute("displayName");
action.meta = {};
Dom.workOn("./@p:*", actionNode, function (metaAttribute) {
var metaValue = metaAttribute.nodeValue;
metaValue = metaValue.replace(/\$([a-z][a-z0-9]*)/gi, function (zero, one) {
return "properties." + one;
});
action.meta[metaAttribute.localName] = metaValue;
});
var implNode = Dom.getSingle("./p:Impl", actionNode);
var text = implNode.textContent;
action.implFunction = null;
try {
action.implFunction = function () {
var s = "var x = function () { " + text + "}; x.apply(contextObject)";
return pEval(s, {contextObject: this});
};
} catch (e) {
Console.dumpError(e);
}
if (shapeDef.actionMap[action.id]) {
shapeDef.removeAction(action.id);
}
shapeDef.actionMap[action.id] = action;
shapeDef.actions.push(action);
});
// pickup the content node
shapeDef.contentNode = Dom.getSingle("./p:Content", shapeDefNode);
// replacing id -> p:name
Dom.workOn(".//*[@id]", shapeDef.contentNode, function (node) {
var id = node.getAttribute("id");
node.setAttributeNS(PencilNamespaces.p, "p:name", id);
node.removeAttribute("id");
});
var parentContentPlaceHolder = Dom.getSingle(".//p:ParentContent", shapeDef.contentNode);
if (parentContentPlaceHolder && shapeDef.parentShapeDef && shapeDef.parentShapeDef.contentNode) {
var f = shapeDef.contentNode.ownerDocument.createDocumentFragment();
for (var i = 0; i < shapeDef.parentShapeDef.contentNode.childNodes.length; i ++) {
var child = shapeDef.parentShapeDef.contentNode.childNodes[i];
child = shapeDef.contentNode.ownerDocument.importNode(child, true);
f.appendChild(child);
}
parentContentPlaceHolder.parentNode.replaceChild(f, parentContentPlaceHolder);
}
return shapeDef;
};
/* public ShapeDef */ ShapeDefCollectionParser.prototype.processInheritance = function (shapeDef) {
// this.contentNode = null;
// this.propertyGroups = [];
// this.behaviors = [];
// this.actions = [];
//
// this.propertyMap = {};
// this.behaviorMap = {};
// this.actionMap = {};
shapeDef.propertyGroups = [].concat();
for (var group of shapeDef.parentShapeDef.propertyGroups) {
var clonedGroup = group.clone();
shapeDef.propertyGroups.push(clonedGroup);
for (var prop of clonedGroup.properties) {
shapeDef.propertyMap[prop.name] = prop;
}
}
shapeDef.behaviors = [].concat(shapeDef.parentShapeDef.behaviors);
for (var name in shapeDef.parentShapeDef.behaviorMap) {
shapeDef.behaviorMap[name] = shapeDef.parentShapeDef.behaviorMap[name];
}
shapeDef.actions = [].concat(shapeDef.parentShapeDef.actions);
for (var name in shapeDef.parentShapeDef.actionMap) {
shapeDef.actionMap[name] = shapeDef.parentShapeDef.actionMap[name];
}
};
/* public Shortcut */ ShapeDefCollectionParser.prototype.parseShortcut = function (shortcutNode, collection) {
var shortcut = new Shortcut();
shortcut.displayName = shortcutNode.getAttribute("displayName");
shortcut.system = shortcutNode.getAttribute("system") == "true";
shortcut.collection = collection;
var iconPath = shortcutNode.getAttribute("icon");
// if (iconPath.indexOf("data:image") != 0) {
// iconPath = collection.url.substring(0, collection.url.lastIndexOf("/") + 1) + iconPath;
// }
shortcut.iconPath = iconPath;
var to = shortcutNode.getAttribute("to");
var shapeId = (to.indexOf(":") > 0) ? to : collection.id + ":" + to;
var shapeDef = (to.indexOf(":") > 0) ?
CollectionManager.shapeDefinition.locateDefinition(shapeId) : collection.getShapeDefById(shapeId);
if (!shapeDef) throw Util.getMessage("bad.shortcut.target.not.found", shapeId);
shortcut.shape = shapeDef;
shortcut.id = "system:ref:" + shortcut.displayName.replace(/[^a-z0-9]+/gi, "_").toLowerCase() + shortcut.shape.id;
//parse property values
Dom.workOn(".//p:PropertyValue", shortcutNode, function (propValueNode) {
var name = propValueNode.getAttribute("name");
var valueElement = Dom.getSingle("./p:*", propValueNode);
var spec = {};
if (valueElement) {
if (valueElement.localName == "E") {
var expression = Dom.getText(valueElement);
expression = expression.replace(/\$\$([a-z][a-z0-9]*)/gi, function (zero, one) {
return "collection.properties." + one + ".value";
});
spec.initialValueExpression = expression;
} else if (valueElement.localName == "Null") {
spec.initialValue = null;
}
} else {
var type = shapeDef.getProperty(name).type;
spec.initialValue = type.fromString(Dom.getText(propValueNode));
spec.collection = collection;
}
shortcut.propertyMap[name] = spec;
});
shortcut.propertyMap._collection = collection;
return shortcut;
};