This repository was archived by the owner on Aug 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXmlComponent.hx
More file actions
967 lines (909 loc) · 33.4 KB
/
XmlComponent.hx
File metadata and controls
967 lines (909 loc) · 33.4 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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/*
XML Components for Feathers UI
Copyright 2021 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package com.feathersui.xml;
#if macro
import com.feathersui.xml.Xml176Parser;
import haxe.io.Path;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.PositionTools;
import haxe.macro.Type.ClassField;
import haxe.macro.Type.ClassType;
import haxe.xml.Parser.XmlParserException;
import sys.FileSystem;
import sys.io.File;
#end
/**
Creates Haxe classes and instances at compile-time using XML. The
`XmlComponent` class offers two static methods, `withMarkup()` and
`withFile()`.
Pass a markup string to `XmlComponent.withMarkup()` to create a component
instance from a string.
```haxe
var instance = XmlComponent.withMarkup(
'<f:LayoutGroup xmlns:f="http://ns.feathersui.com/xml">
<f:layout>
<f:HorizontalLayout gap="10" horizontalAlign="RIGHT"/>
</f:layout>
<f:Button id="okButton" text="OK"/>
<f:Button id="cancelButton" text="Cancel"/>
</f:LayoutGroup>'
);
container.addChild(instance);
instance.okButton.addEventListener(TriggerEvent.TRIGGER, (event) -> {
trace("triggered the OK button");
});
```
Pass a file path (relative to the current _.hx_ source file) to
`XmlComponent.withFile()` to create a component instance from markup saved
in an external file.
```haxe
var instance = XmlComponent.withFile("path/to/file.xml");
```
**/
@:access("com.feathersui.xml.Xml176Parser")
class XmlComponent {
#if macro
private static final FILE_PATH_TO_TYPE_DEFINITION:Map<String, TypeDefinition> = [];
private static final PROPERTY_ID = "id";
private static final PROPERTY_XMLNS = "xmlns";
private static final PROPERTY_XMLNS_PREFIX = "xmlns:";
private static final RESERVED_PROPERTIES = [PROPERTY_ID, PROPERTY_XMLNS];
private static var componentCounter = 0;
private static var objectCounter = 0;
private static var posInfos:{min:Int, max:Int, file:String};
private static final URI_HAXE = "http://ns.haxe.org/4/xml";
private static final MAPPINGS_HAXE = [
"Float" => "Float",
"Int" => "Int",
"UInt" => "UInt",
"Bool" => "Bool",
"String" => "String",
"Dynamic" => "Dynamic",
"Any" => "Any",
"Array" => "Array"
];
private static var uriToMappings:Map<String, Map<String, String>> = [URI_HAXE => MAPPINGS_HAXE];
/**
Adds a custom mapping from a namespace URI to a list of components in
the namespace.
**/
public static function addNamespaceMapping(uri:String, mappings:Map<String, String>):Void {
if (!uriToMappings.exists(uri)) {
uriToMappings.set(uri, mappings);
} else {
var existingMappings = uriToMappings.get(uri);
for (shortName => qualifiedName in mappings) {
existingMappings.set(shortName, qualifiedName);
}
}
}
#end
/**
Populates fields in a class using markup in a file. Similar to
`withFile()`, but it's a build macro instead — which gives developers
more control over the generated class. For instance, it's possible to
define additional fields and methods to the class, and to instantiate it
on demand.
**/
public macro static function buildWithFile(filePath:String):Array<Field> {
var xmlDocument = loadXmlFile(filePath);
var xml = xmlDocument.document;
var firstElement = xml.firstElement();
if (firstElement == null) {
Context.error('No root tag found in XML', Context.currentPos());
}
var prefixMap = getLocalPrefixMap(firstElement);
var componentTypeName = getComponentType(getXmlName(firstElement, prefixMap, xmlDocument), prefixMap);
var localClass = Context.getLocalClass().get();
var superClass = localClass.superClass;
if (superClass == null || Std.string(superClass.t) != componentTypeName) {
Context.error('Class ${localClass.name} must extend ${componentTypeName}', Context.currentPos());
}
var buildFields = Context.getBuildFields();
parseRootElement(firstElement, "XMLComponent_initXML", buildFields, xmlDocument);
return buildFields;
}
/**
Instantiates a component from a file containing markup.
Calling `withFile()` multiple times will re-use the same generated
class each time.
**/
public macro static function withFile(filePath:String):Expr {
filePath = resolveAsset(filePath);
var typeDef:TypeDefinition = null;
if (FILE_PATH_TO_TYPE_DEFINITION.exists(filePath)) {
// for duplicate files, re-use the existing type definition
typeDef = FILE_PATH_TO_TYPE_DEFINITION.get(filePath);
}
if (typeDef == null) {
var xmlDocument = loadXmlFile(filePath);
var nameSuffix = Path.withoutExtension(Path.withoutDirectory(filePath));
typeDef = createTypeDefinitionFromXml(xmlDocument, nameSuffix);
FILE_PATH_TO_TYPE_DEFINITION.set(filePath, typeDef);
}
var typePath = {name: typeDef.name, pack: typeDef.pack};
return macro new $typePath();
}
/**
Instantiates a component from markup.
**/
public macro static function withMarkup(input:ExprOf<String>):Expr {
posInfos = PositionTools.getInfos(input.pos);
var xmlString:String = null;
switch (input.expr) {
case EMeta({name: ":markup"}, {expr: EConst(CString(s))}):
xmlString = s;
case EConst(CString(s)):
xmlString = s;
case _:
throw new haxe.macro.Expr.Error("Expected markup or string literal", input.pos);
}
var xmlDocument = loadXmlString(xmlString);
var typeDef = createTypeDefinitionFromXml(xmlDocument, "Xml");
var typePath = {name: typeDef.name, pack: typeDef.pack};
return macro new $typePath();
}
#if macro
private static function resolveAsset(filePath:String):String {
if (Path.isAbsolute(filePath)) {
return filePath;
}
var modulePath = Context.getPosInfos(Context.currentPos()).file;
if (!Path.isAbsolute(modulePath)) {
modulePath = FileSystem.absolutePath(modulePath);
}
modulePath = Path.directory(modulePath);
return Path.join([modulePath, filePath]);
}
private static function loadXmlFile(filePath:String):Xml176Document {
filePath = resolveAsset(filePath);
if (!FileSystem.exists(filePath)) {
Context.error('Component XML file not found: ${filePath}', Context.currentPos());
}
var content = File.getContent(filePath);
posInfos = {file: filePath, min: 0, max: content.length};
return loadXmlString(content);
}
private static function loadXmlString(xmlString:String):Xml176Document {
try {
return Xml176Parser.parse(xmlString);
} catch (e:XmlParserException) {
errorAtXmlPosition('XML parse error: ${e.message}', {from: e.position});
} catch (e:String) {
errorAtXmlPosition('XML parse error: ${e}', {from: 0});
}
return null;
}
private static function xmlPosToPosition(xmlPos:Pos):Position {
var min = posInfos.min;
var from = 0;
var to = 0;
if (xmlPos != null) {
from = xmlPos.from;
to = (xmlPos.to != null) ? xmlPos.to : xmlPos.from;
}
return Context.makePosition({file: posInfos.file, min: min + from, max: min + to});
}
private static function fatalErrorAtXmlPosition(text:String, xmlPos:Pos):Void {
var errorPos = xmlPosToPosition(xmlPos);
Context.fatalError(text, errorPos);
}
private static function errorAtXmlPosition(text:String, xmlPos:Pos):Void {
var errorPos = xmlPosToPosition(xmlPos);
Context.error(text, errorPos);
}
private static function createTypeDefinitionFromXml(xmlDocument:Xml176Document, nameSuffix:String):TypeDefinition {
var root = xmlDocument.document;
var firstElement = root.firstElement();
if (firstElement == null) {
errorAtXmlPosition('No root tag found in XML', xmlDocument.getNodePosition(root));
}
var buildFields:Array<Field> = [];
var classType = parseRootElement(firstElement, "XMLComponent_initXML", buildFields, xmlDocument);
var componentName = 'FeathersUI_XMLComponent_${nameSuffix}_${componentCounter}';
componentCounter++;
var typeDef:TypeDefinition = null;
if (classType == null) {
typeDef = macro class $componentName {};
} else {
var superClassTypePath = {name: classType.name, pack: classType.pack};
typeDef = macro class $componentName extends $superClassTypePath {};
}
for (buildField in buildFields) {
typeDef.fields.push(buildField);
}
Context.defineType(typeDef);
return typeDef;
}
private static function parseRootElement(element:Xml, overrideName:String, buildFields:Array<Field>, xmlDocument:Xml176Document):ClassType {
objectCounter = 0;
var localPrefixMap = getLocalPrefixMap(element);
var xmlName = getXmlName(element, localPrefixMap, xmlDocument);
var classType = getClassType(xmlName, localPrefixMap, element, xmlDocument);
var generatedFields:Array<Field> = [];
var bodyExprs:Array<Expr> = [];
parseAttributes(element, classType, "this", localPrefixMap, bodyExprs, xmlDocument);
parseChildrenOfObject(element, classType, "this", xmlName.prefix, localPrefixMap, generatedFields, bodyExprs, xmlDocument);
buildFields.push({
name: overrideName,
pos: Context.currentPos(),
kind: FFun({
args: [],
ret: macro:Void,
expr: macro $b{bodyExprs}
}),
access: [APrivate],
meta: [
{
name: ":noCompletion",
pos: Context.currentPos()
}
]
});
var constructorExprs:Array<Expr> = [macro this.XMLComponent_initXML()];
if (classType != null) {
constructorExprs.unshift(macro super());
}
buildFields.push({
name: "new",
pos: Context.currentPos(),
kind: FFun({
args: [],
ret: macro:Void,
expr: macro $b{constructorExprs}
}),
access: [APublic]
});
for (field in generatedFields) {
buildFields.push(field);
}
return classType;
}
private static function findField(type:ClassType, fieldName:String):ClassField {
for (field in getAllFields(type)) {
if (field.name == fieldName) {
return field;
}
}
return null;
}
private static function findEvent(type:ClassType, eventName:String):MetadataEntry {
for (eventMeta in getAllEvents(type)) {
var otherEventName = getEventName(eventMeta);
if (otherEventName == eventName) {
return eventMeta;
}
}
return null;
}
private static function getEventName(eventMeta:MetadataEntry):String {
var typedExprDef = Context.typeExpr(eventMeta.params[0]).expr;
return switch (typedExprDef) {
case TCast(e, m):
switch (e.expr) {
case TConst(c):
switch (c) {
case TString(s): s;
default: null;
}
default: null;
}
default: null;
};
}
private static function parseChildrenOfObject(element:Xml, parentType:ClassType, targetIdentifier:String, parentPrefix:String,
prefixMap:Map<String, String>, parentFields:Array<Field>, initExprs:Array<Expr>, xmlDocument:Xml176Document):Void {
var defaultChildren:Array<Xml> = null;
var hasDefaultProperty = parentType == null || parentType.meta.has("defaultXmlProperty");
for (child in element.iterator()) {
switch (child.nodeType) {
case Element:
var childXmlName = getXmlName(child, prefixMap, xmlDocument);
if (isXmlLanguageElement("Binding", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Component", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Declarations", childXmlName, prefixMap)) {
if (targetIdentifier == "this") {
parseDeclarations(child, prefixMap, parentFields, initExprs, xmlDocument);
} else {
errorAtXmlPosition('The \'<${child.nodeName}>\' tag must be a child of the root element', xmlDocument.getNodePosition(child));
}
continue;
} else if (isXmlLanguageElement("Definition", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("DesignLayer", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Library", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Metadata", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Model", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Private", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Reparent", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Script", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
} else if (isXmlLanguageElement("Style", childXmlName, prefixMap)) {
errorTagNotSupported(child, xmlDocument);
}
var foundField:ClassField = null;
if (childXmlName.prefix == parentPrefix) {
var localName = childXmlName.localName;
foundField = findField(parentType, localName);
}
if (foundField == null && parentType != null) {
var isArray = parentType.name == "Array" && parentType.pack.length == 0;
if (!hasDefaultProperty && !isArray) {
errorAtXmlPosition('The \'<${child.nodeName}>\' tag is unexpected', xmlDocument.getNodePosition(child));
}
if (defaultChildren == null) {
defaultChildren = [];
}
defaultChildren.push(child);
continue;
}
checkForInvalidAttributes(child, false, xmlDocument);
var fieldType = foundField != null ? foundField.type : null;
parseChildrenForField(child, child.iterator(), targetIdentifier, foundField, childXmlName.localName, fieldType, prefixMap, parentFields,
initExprs, xmlDocument);
case PCData:
var str = StringTools.trim(child.nodeValue);
if (str.length == 0) {
continue;
}
if (!hasDefaultProperty) {
errorAtXmlPosition('The \'${child.nodeValue}\' value is unexpected', xmlDocument.getNodePosition(child));
}
if (defaultChildren == null) {
defaultChildren = [];
}
defaultChildren.push(child);
case CData:
if (!hasDefaultProperty) {
errorAtXmlPosition('The \'${child.nodeValue}\' value is unexpected', xmlDocument.getNodePosition(child));
}
if (defaultChildren == null) {
defaultChildren = [];
}
defaultChildren.push(child);
default:
}
}
if (defaultChildren == null) {
return;
}
if (parentType.name == "Array" && parentType.pack.length == 0) {
parseChildrenForField(element, defaultChildren.iterator(), targetIdentifier, null, null, Context.getType(parentType.name), prefixMap,
parentFields, initExprs, xmlDocument);
return;
}
var defaultXmlPropMeta = parentType.meta.extract("defaultXmlProperty")[0];
if (defaultXmlPropMeta.params.length != 1) {
Context.error('The defaultXmlProperty meta must have one property name', defaultXmlPropMeta.pos);
}
var param = defaultXmlPropMeta.params[0];
var propertyName:String = null;
switch (param.expr) {
case EConst(c):
switch (c) {
case CString(s, kind):
propertyName = s;
default:
}
default:
}
if (propertyName == null) {
Context.error('The defaultXmlProperty meta param must be a string', param.pos);
}
var defaultField = findField(parentType, propertyName);
if (defaultField == null) {
// the metadata is there, but it seems to be referencing a property
// that doesn't exist
Context.error('Invalid default property \'${propertyName}\' for the \'<${element.nodeName}>\' tag', defaultXmlPropMeta.pos);
}
var xmlName = getXmlName(element, prefixMap, xmlDocument);
parseChildrenForField(element, defaultChildren.iterator(), targetIdentifier, defaultField, defaultField.name, defaultField.type, prefixMap,
parentFields, initExprs, xmlDocument);
}
private static function errorTagNotSupported(element:Xml, xmlDocument:Xml176Document):Void {
errorAtXmlPosition('The \'<${element.nodeName}>\' tag is not supported', xmlDocument.getNodePosition(element));
}
private static function checkForInvalidAttributes(element:Xml, allowId:Bool, xmlDocument:Xml176Document):Void {
for (attribute in element.attributes()) {
if (allowId && attribute == "id") {
continue;
}
var attrPos = xmlDocument.getAttrPosition(element, attribute);
errorAtXmlPosition('Unknown field \'${attribute}\'', attrPos);
}
}
private static function parseDeclarations(element:Xml, prefixMap:Map<String, String>, parentFields:Array<Field>, initExprs:Array<Expr>,
xmlDocument:Xml176Document):Void {
checkForInvalidAttributes(element, false, xmlDocument);
for (child in element.iterator()) {
switch (child.nodeType) {
case Element:
var localPrefixMap = getLocalPrefixMap(child, prefixMap);
var childXmlName = getXmlName(child, localPrefixMap, xmlDocument);
var objectID = child.get(PROPERTY_ID);
var initExpr:Expr = null;
if (isBuiltIn(childXmlName, prefixMap) && childXmlName.localName != "Dynamic" && childXmlName.localName != "Any"
&& childXmlName.localName != "Array") {
checkForInvalidAttributes(child, true, xmlDocument);
for (grandChild in child.iterator()) {
var str = StringTools.trim(grandChild.nodeValue);
initExpr = createValueExprForDynamic(str);
var complexType = Context.toComplexType(Context.typeExpr(initExpr).t);
if (objectID != null) {
parentFields.push({
name: objectID,
pos: Context.currentPos(),
kind: FVar(complexType),
access: [APublic]
});
}
}
} else {
var functionName:String = parseChildElement(child, prefixMap, parentFields, xmlDocument);
initExpr = macro $i{functionName}();
}
if (objectID != null) {
initExpr = macro this.$objectID = $initExpr;
}
initExprs.push(initExpr);
case PCData:
var str = StringTools.trim(child.nodeValue);
if (str.length == 0) {
continue;
}
errorAtXmlPosition('The \'${child.nodeValue}\' value is unexpected', xmlDocument.getNodePosition(child));
default:
errorAtXmlPosition('Cannot parse XML child \'${child.nodeValue}\' of type \'${child.nodeType}\'', xmlDocument.getNodePosition(child));
}
}
}
private static function parseChildrenForField(parent:Xml, children:Iterator<Xml>, targetIdentifier:String, field:ClassField, fieldName:String,
fieldType:haxe.macro.Type, parentPrefixMap:Map<String, String>, parentFields:Array<Field>, initExprs:Array<Expr>,
xmlDocument:Xml176Document):Void {
var isArray = false;
while (fieldType != null) {
switch (fieldType) {
case TInst(t, params):
isArray = t.get().name == "Array";
fieldType = null;
case TLazy(f):
fieldType = f();
default:
fieldType = null;
}
}
var valueExprs:Array<Expr> = [];
var firstChildIsArray = false;
for (child in children) {
switch (child.nodeType) {
case Element:
if (!isArray && valueExprs.length > 0) {
errorAtXmlPosition('The \'<${child.nodeName}>\' tag is unexpected', xmlDocument.getNodePosition(child));
}
var childXmlName = getXmlName(child, parentPrefixMap, xmlDocument);
if (isBuiltIn(childXmlName, parentPrefixMap)
&& childXmlName.localName != "Dynamic"
&& childXmlName.localName != "Any"
&& childXmlName.localName != "Array") {
checkForInvalidAttributes(child, true, xmlDocument);
for (grandChild in child.iterator()) {
if (!isArray && valueExprs.length > 0) {
errorAtXmlPosition('The child of type \'${grandChild.nodeType}\' is unexpected', xmlDocument.getNodePosition(child));
}
var str = StringTools.trim(grandChild.nodeValue);
if (isArray) {
var exprType = Context.getType(childXmlName.localName);
var valueExpr = createValueExprForType(exprType, str, child, xmlDocument);
valueExprs.push(valueExpr);
} else if (field == null) {
var valueExpr = createValueExprForDynamic(str);
valueExprs.push(valueExpr);
} else {
var valueExpr = createValueExprForField(field, str, child, xmlDocument);
valueExprs.push(valueExpr);
}
}
} else {
if (valueExprs.length == 0 && childXmlName.localName == "Array") {
firstChildIsArray = true;
}
var functionName:String = parseChildElement(child, parentPrefixMap, parentFields, xmlDocument);
var valueExpr = macro $i{functionName}();
valueExprs.push(valueExpr);
}
case PCData:
var str = StringTools.trim(child.nodeValue);
if (str.length == 0) {
// skip any children that are entirely whitespace
continue;
}
if (!isArray && valueExprs.length > 0) {
errorAtXmlPosition('The child of type \'${child.nodeType}\' is unexpected', xmlDocument.getNodePosition(child));
}
if (field == null) {
var valueExpr = createValueExprForDynamic(str);
valueExprs.push(valueExpr);
} else {
var valueExpr = createValueExprForField(field, str, child, xmlDocument);
valueExprs.push(valueExpr);
}
case CData:
if (!isArray && valueExprs.length > 0) {
errorAtXmlPosition('The child of type \'${child.nodeType}\' is unexpected', xmlDocument.getNodePosition(child));
}
var str = child.nodeValue;
if (field == null) {
var valueExpr = createValueExprForDynamic(str);
valueExprs.push(valueExpr);
} else {
var valueExpr = createValueExprForField(field, str, child, xmlDocument);
valueExprs.push(valueExpr);
}
default:
errorAtXmlPosition('Cannot parse XML child \'${child.nodeValue}\' of type \'${child.nodeType}\'', xmlDocument.getNodePosition(child));
}
}
if (valueExprs.length == 0) {
errorAtXmlPosition('Value for field \'${fieldName}\' must not be empty', xmlDocument.getNodePosition(parent));
}
if (!isArray) {
var valueExpr = valueExprs[0];
var setExpr = macro $i{targetIdentifier}.$fieldName = ${valueExpr};
initExprs.push(setExpr);
return;
}
if (isArray && fieldName == null) {
for (i in 0...valueExprs.length) {
var valueExpr = valueExprs[i];
initExprs.push(macro $i{targetIdentifier}[$v{i}] = ${valueExpr});
}
} else if (isArray && fieldName != null && valueExprs.length == 1 && firstChildIsArray) {
var valueExpr = valueExprs[0];
initExprs.push(macro $i{targetIdentifier}.$fieldName = ${valueExpr});
} else {
var localVarName = "array_" + fieldName;
initExprs.push(macro var $localVarName:Array<Dynamic> = []);
for (i in 0...valueExprs.length) {
var valueExpr = valueExprs[i];
initExprs.push(macro $i{localVarName}[$v{i}] = ${valueExpr});
}
initExprs.push(macro $i{targetIdentifier}.$fieldName = cast($i{localVarName}));
}
}
private static function parseChildElement(element:Xml, parentPrefixMap:Map<String, String>, parentFields:Array<Field>, xmlDocument:Xml176Document):String {
var localPrefixMap = getLocalPrefixMap(element, parentPrefixMap);
var xmlName = getXmlName(element, localPrefixMap, xmlDocument);
var classType = getClassType(xmlName, localPrefixMap, element, xmlDocument);
var localVarName = "object";
var childTypePath:TypePath = null;
if (classType == null) {
childTypePath = {name: xmlName.localName, pack: []};
} else {
childTypePath = {name: classType.name, pack: classType.pack};
if (classType.name == "Array" && classType.pack.length == 0) {
var paramTypePath:TypePath = {name: "Dynamic", pack: []};
childTypePath.params = [TPType(TPath(paramTypePath))];
}
}
var returnTypePath = childTypePath;
if (classType != null && classType.params.length > 0) {
returnTypePath = {name: "Dynamic", pack: []}
}
var objectID:String = element.get(PROPERTY_ID);
var setIDExpr:Expr = null;
if (objectID != null) {
parentFields.push({
name: objectID,
pos: Context.currentPos(),
kind: FVar(TPath(childTypePath)),
access: [APublic]
});
setIDExpr = macro this.$objectID = $i{localVarName};
} else {
objectID = Std.string(objectCounter);
objectCounter++;
}
var setFieldExprs:Array<Expr> = [];
parseAttributes(element, classType, localVarName, localPrefixMap, setFieldExprs, xmlDocument);
parseChildrenOfObject(element, classType, localVarName, xmlName.prefix, localPrefixMap, parentFields, setFieldExprs, xmlDocument);
if (setIDExpr != null) {
setFieldExprs.push(setIDExpr);
}
var bodyExpr:Expr = null;
if (classType == null) {
bodyExpr = macro {
var $localVarName:Dynamic = {};
$b{setFieldExprs};
return $i{localVarName};
}
} else {
bodyExpr = macro {
var $localVarName = new $childTypePath();
$b{setFieldExprs};
return $i{localVarName};
}
}
var functionName = "createXmlObject_" + objectID;
parentFields.push({
name: functionName,
pos: Context.currentPos(),
kind: FFun({
args: [],
ret: TPath(returnTypePath),
expr: bodyExpr
}),
access: [APrivate],
meta: [
{
name: ":noCompletion",
pos: Context.currentPos()
}
]
});
return functionName;
}
private static function getAllFields(type:ClassType):Array<ClassField> {
if (type == null) {
return [];
}
var fields = type.fields.get();
var superClass = type.superClass;
if (superClass != null) {
for (field in getAllFields(superClass.t.get())) {
fields.push(field);
}
}
return fields;
}
private static function getAllEvents(type:ClassType):Array<MetadataEntry> {
if (type == null) {
return [];
}
var events = type.meta.extract(":event").filter(eventMeta -> eventMeta.params.length == 1);
var superClass = type.superClass;
if (superClass != null) {
for (event in getAllEvents(superClass.t.get())) {
events.push(event);
}
}
return events;
}
private static function parseAttributes(element:Xml, parentType:ClassType, targetIdentifier:String, prefixMap:Map<String, String>, initExprs:Array<Expr>,
xmlDocument:Xml176Document):Void {
for (attribute in element.attributes()) {
if (StringTools.startsWith(attribute, "xmlns:")) {
continue;
}
var foundField:ClassField = findField(parentType, attribute);
if (foundField == null && attribute == "id") {
// set id if it's available, otherwise skip it
continue;
}
if (foundField != null) {
var fieldValue = element.get(attribute);
var valueExpr = createValueExprForField(foundField, fieldValue, element, xmlDocument);
var setExpr = macro $i{targetIdentifier}.$attribute = ${valueExpr};
initExprs.push(setExpr);
} else if (parentType == null) {
var fieldValue = element.get(attribute);
var valueExpr = createValueExprForDynamic(fieldValue);
var setExpr = macro Reflect.setField($i{targetIdentifier}, $v{attribute}, ${valueExpr});
initExprs.push(setExpr);
} else {
var foundEvent = findEvent(parentType, attribute);
if (foundEvent != null) {
var eventName = getEventName(foundEvent);
var eventText = element.get(attribute);
var eventExpr = Context.parse(eventText, Context.currentPos());
var addEventExpr = macro $i{targetIdentifier}.addEventListener($v{eventName}, (event) -> ${eventExpr});
initExprs.push(addEventExpr);
} else if (RESERVED_PROPERTIES.indexOf(attribute) == -1 && !StringTools.startsWith(attribute, PROPERTY_XMLNS_PREFIX)) {
var attrPos = xmlDocument.getAttrPosition(element, attribute);
errorAtXmlPosition('Unknown field \'${attribute}\'', attrPos);
}
}
}
}
private static function createValueExprForField(field:ClassField, value:String, element:Xml, xmlDocument:Xml176Document):Expr {
var fieldName = field.name;
if (!field.isPublic) {
errorAtXmlPosition('Cannot set field \'${fieldName}\' because it is not public', xmlDocument.getNodePosition(element));
}
if (value.length == 0) {
errorAtXmlPosition('The attribute \'${fieldName}\' cannot be empty', xmlDocument.getNodePosition(element));
}
switch (field.kind) {
case FVar(read, write):
default:
errorAtXmlPosition('Cannot set field \'${fieldName}\'', xmlDocument.getNodePosition(element));
}
return createValueExprForType(field.type, value, element, xmlDocument);
}
private static function createValueExprForType(fieldType:haxe.macro.Type, value:String, element:Xml, xmlDocument:Xml176Document):Expr {
var fieldTypeName:String = null;
while (fieldTypeName == null) {
switch (fieldType) {
case TInst(t, params):
fieldTypeName = t.get().name;
break;
case TAbstract(t, params):
var abstractType = t.get();
if (abstractType.name == "Null") {
fieldType = params[0];
} else {
fieldTypeName = abstractType.name;
break;
}
case TEnum(t, params):
var enumType = t.get();
fieldTypeName = enumType.name;
if (enumType.names.indexOf(value) != -1) {
return macro $i{value};
}
break;
case TLazy(f):
fieldType = f();
default:
errorAtXmlPosition('Cannot parse a value of type \'${fieldTypeName}\' from \'${value}\'', xmlDocument.getNodePosition(element));
}
}
switch (fieldTypeName) {
case "Bool":
if (~/^true|false$/.match(value)) {
var boolValue = value == "true";
return macro $v{boolValue};
}
case "Float":
if (~/^-?[0-9]+(\.[0-9]+)?$/.match(value)) {
var floatValue = Std.parseFloat(value);
return macro $v{floatValue};
}
case "Int":
var intValue = Std.parseInt(value);
if (intValue != null) {
return macro $v{intValue};
}
case "UInt":
var uintValue = Std.parseInt(value);
if (uintValue != null) {
return macro $v{uintValue};
}
case "String":
return macro $v{value};
default:
}
errorAtXmlPosition('Cannot parse a value of type \'${fieldTypeName}\' from \'${value}\'', xmlDocument.getNodePosition(element));
return null;
}
private static function createValueExprForDynamic(value:String):Expr {
if (~/^true|false$/.match(value)) {
var boolValue = value == "true";
return macro $v{boolValue};
}
if (~/^-?[0-9]+(\.[0-9]+)?$/.match(value)) {
var floatValue = Std.parseFloat(value);
return macro $v{floatValue};
}
if (~/^-?[0-9]+?$/.match(value)) {
var intValue = Std.parseInt(value);
return macro $v{intValue};
}
// it can always be parsed as a string
return macro $v{value};
}
private static function getClassType(xmlName:XmlName, prefixMap:Map<String, String>, element:Xml, xmlDocument:Xml176Document):ClassType {
var componentTypeName = getComponentType(xmlName, prefixMap);
if (componentTypeName == null) {
errorAtXmlPosition('Class not found for \'<${xmlName.prefix}:${xmlName.localName}>\' tag', xmlDocument.getNodePosition(element));
return null;
}
var componentType:haxe.macro.Type = null;
try {
componentType = Context.getType(componentTypeName);
} catch (e:Dynamic) {
errorAtXmlPosition('Cannot create object \'<${xmlName.prefix}:${xmlName.localName}>\'\n${e}', xmlDocument.getNodePosition(element));
}
var classType:ClassType = null;
switch (componentType) {
case TInst(t, _):
return t.get();
case TAbstract(t, params):
var abstractType = t.get();
if (abstractType.name == "Dynamic" && abstractType.pack.length == 0) {
return null;
}
if (abstractType.name == "Any" && abstractType.pack.length == 0) {
return null;
}
errorAtXmlPosition('Cannot create object \'<${xmlName.prefix}:${xmlName.localName}>\'', xmlDocument.getNodePosition(element));
return null;
default:
errorAtXmlPosition('Cannot create object \'<${xmlName.prefix}:${xmlName.localName}>\'', xmlDocument.getNodePosition(element));
return null;
}
}
private static function getLocalPrefixMap(element:Xml, ?parentPrefixMap:Map<String, String>):Map<String, String> {
var localPrefixMap:Map<String, String> = null;
if (parentPrefixMap == null) {
localPrefixMap = [];
} else {
localPrefixMap = parentPrefixMap.copy();
}
for (attribute in element.attributes()) {
if (attribute == "xmlns") {
var uri = element.get(attribute);
localPrefixMap.set("", uri);
} else if (StringTools.startsWith(attribute, "xmlns:")) {
var prefix = attribute.substr(6);
var uri = element.get(attribute);
localPrefixMap.set(prefix, uri);
}
}
return localPrefixMap;
}
private static function getXmlName(element:Xml, prefixMap:Map<String, String>, xmlDocument:Xml176Document):XmlName {
var nameParts = element.nodeName.split(":");
if (nameParts.length == 1) {
return new XmlName("", nameParts[0]);
} else if (nameParts.length == 2) {
var prefix = nameParts[0];
if (!prefixMap.exists(prefix)) {
errorAtXmlPosition('Unknown XML namespace prefix \'${prefix}\'', xmlDocument.getNodePosition(element));
}
var localName = nameParts[1];
return new XmlName(prefix, localName);
}
errorAtXmlPosition('Invalid element name \'<${element.nodeName}>\'', xmlDocument.getNodePosition(element));
return null;
}
private static function getComponentType(xmlName:XmlName, prefixMap:Map<String, String>):String {
var prefix = xmlName.prefix;
var uri = prefixMap.get(prefix);
var localName = xmlName.localName;
if (uri != null && uriToMappings.exists(uri)) {
var mappings = uriToMappings.get(uri);
if (mappings.exists(localName)) {
return mappings.get(localName);
}
}
if (uri != null && StringTools.endsWith(uri, ".*")) {
return uri.substr(0, uri.length - 1) + localName;
}
return null;
}
private static function isBuiltIn(xmlName:XmlName, prefixMap:Map<String, String>):Bool {
var prefix = xmlName.prefix;
var uri = prefixMap.get(prefix);
var localName = xmlName.localName;
if (uri == URI_HAXE && MAPPINGS_HAXE.exists(localName)) {
return true;
}
return false;
}
private static function isXmlLanguageElement(elementName:String, xmlName:XmlName, prefixMap:Map<String, String>):Bool {
var prefix = xmlName.prefix;
return xmlName.localName == elementName && prefixMap.get(prefix) == URI_HAXE;
}
#end
}
private class XmlName {
public function new(prefix:String, localName:String) {
this.prefix = prefix;
this.localName = localName;
}
public var prefix:String;
public var localName:String;
}