/
parse.go
1109 lines (1013 loc) · 28.7 KB
/
parse.go
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package xsd
import (
"encoding/xml"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"aqwari.net/xml/internal/dependency"
"aqwari.net/xml/xmltree"
)
func hasCycle(root *xmltree.Element, visited map[*xmltree.Element]struct{}) bool {
if visited == nil {
visited = make(map[*xmltree.Element]struct{})
}
visited[root] = struct{}{}
for i := range root.Children {
el := &root.Children[i]
if _, ok := visited[el]; ok {
return true
}
visited[el] = struct{}{}
if hasCycle(el, visited) {
return true
}
}
delete(visited, root)
return false
}
// A Ref contains the canonical namespace of a schema document, and
// possibly a URI to retrieve the document from. It is not required
// for XML Schema documents to provide the location of schema that
// they import; it is expected that all well-known schema namespaces
// are available to the consumer of a schema beforehand.
type Ref struct {
Namespace, Location string
}
// Imports reads an XML document containing one or more <schema>
// elements and returns a list of canonical XML name spaces that
// the schema imports or includes, along with a URL for the schema,
// if provided.
func Imports(data []byte) ([]Ref, error) {
var result []Ref
root, err := xmltree.Parse(data)
if err != nil {
return nil, err
}
for _, v := range root.Search(schemaNS, "import") {
s := Ref{v.Attr("", "namespace"), v.Attr("", "schemaLocation")}
result = append(result, s)
}
var schema []*xmltree.Element
if (root.Name == xml.Name{schemaNS, "schema"}) {
schema = []*xmltree.Element{root}
} else {
schema = root.Search(schemaNS, "schema")
}
for _, tree := range schema {
ns := tree.Attr("", "targetNamespace")
for _, v := range tree.Search(schemaNS, "include") {
s := Ref{ns, v.Attr("", "schemaLocation")}
result = append(result, s)
}
}
return result, nil
}
// Normalize reads XML schema documents and returns xml trees
// for each schema with the following properties:
//
// * various XSD shorthand, such as omitting <complexContent>,
// are expanded into their canonical forms.
// * all links are dereferenced by merging the linked element.
// * all types have names. For anonymous types, unique (per
// namespace) names of the form "_anon1", "_anon2", etc are
// generated, and the attribute "_isAnonymous" is set to
// "true".
//
// Because one document may contain more than one schema, the
// number of trees returned by Normalize may not equal the
// number of arguments.
func Normalize(docs ...[]byte) ([]*xmltree.Element, error) {
docs = append(docs, StandardSchema...)
result := make([]*xmltree.Element, 0, len(docs))
for _, data := range docs {
root, err := xmltree.Parse(data)
if err != nil {
return nil, err
}
if (root.Name == xml.Name{schemaNS, "schema"}) {
result = append(result, root)
} else {
result = append(result, root.Search(schemaNS, "schema")...)
}
}
for _, root := range result {
attributeDefaultType(root)
elementDefaultType(root)
copyEltNamesToAnonTypes(root)
}
typeCounter := 0
for _, root := range result {
if err := nameAnonymousTypes(root, &typeCounter); err != nil {
return nil, err
}
if err := setChoicesOptional(root); err != nil {
return nil, err
}
}
for _, root := range result {
expandComplexShorthand(root)
}
if err := flattenRef(result); err != nil {
return nil, err
}
return result, nil
}
// Parse reads XML documents containing one or more <schema>
// elements. The returned slice has one Schema for every <schema>
// element in the documents. Parse will not fetch schema used in
// <import> or <include> statements; use the Imports function to
// find any additional schema documents required for a schema.
func Parse(docs ...[]byte) ([]Schema, error) {
var (
result = make([]Schema, 0, len(docs))
parsed = make(map[string]Schema, len(docs))
types = make(map[xml.Name]Type)
)
schema, err := Normalize(docs...)
if err != nil {
return nil, err
}
for _, root := range schema {
tns := root.Attr("", "targetNamespace")
s := Schema{TargetNS: tns, Types: make(map[xml.Name]Type)}
if err := s.parse(root); err != nil {
return nil, err
}
parsed[tns] = s
}
for _, s := range parsed {
for _, t := range s.Types {
types[XMLName(t)] = t
}
}
for _, root := range schema {
s := parsed[root.Attr("", "targetNamespace")]
if err := s.resolvePartialTypes(types); err != nil {
return nil, err
}
err := s.addElementTypeAliases(root, types)
if err != nil {
return nil, err
}
s.propagateMixedAttr()
result = append(result, s)
}
result = append(result, builtinSchema)
return result, nil
}
func parseType(name xml.Name) Type {
builtin, err := ParseBuiltin(name)
if err != nil {
return linkedType(name)
}
return builtin
}
func anonTypeName(n int, ns string) xml.Name {
return xml.Name{ns, fmt.Sprintf("_anon%d", n)}
}
/* Convert
<element name="foo">
<complexType>
...
</complexType>
</element>
to
<element name="foo" type="foo">
<complexType name="foo">
...
</complexType>
</element>
*/
func copyEltNamesToAnonTypes(root *xmltree.Element) {
used := make(map[xml.Name]struct{})
tns := root.Attr("", "targetNamespace")
namedTypes := and(isType, hasAttr("", "name"))
for _, el := range root.SearchFunc(namedTypes) {
used[el.ResolveDefault(el.Attr("", "name"), tns)] = struct{}{}
}
eltWithAnonType := and(
or(isElem(schemaNS, "element"), isElem(schemaNS, "attribute")),
hasAttr("", "name"),
hasAnonymousType)
for _, el := range root.SearchFunc(eltWithAnonType) {
// Make sure we can use this element's name
xmlname := el.ResolveDefault(el.Attr("", "name"), tns)
if _, ok := used[xmlname]; ok {
continue
}
used[xmlname] = struct{}{}
for i, t := range el.Children {
if !isAnonymousType(&t) {
continue
}
t.SetAttr("", "name", el.Attr("", "name"))
el.SetAttr("", "type", el.Prefix(xmlname))
el.Children = append(el.Children[:i], el.Children[i+1:]...)
el.Content = nil
root.Children = append(root.Children, t)
break
}
}
}
// Inside a <xs:choice>, set all children to optional
// If child is a <xs:sequence> set its children to optional
func setChoicesOptional(root *xmltree.Element) error {
for _, el := range root.SearchFunc(isElem(schemaNS, "choice")) {
for i := 0; i < len(el.Children); i++ {
t := el.Children[i]
if t.Name.Space == schemaNS && t.Name.Local == "sequence" {
for j := 0; j < len(t.Children); j++ {
t2 := t.Children[j]
t2.SetAttr("", "minOccurs", "0")
t.Children[j] = t2
}
} else {
t.SetAttr("", "minOccurs", "0")
}
el.Children[i] = t
}
}
return nil
}
/*
Convert
<xs:complexType name="foo" base="xs:anyType"/>
<xs:sequence>
<xs:element name="a">
<xs:simpleType base="xs:int">
...
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
to
<xs:complexType name="foo" base="xs:anyType"/>
<xs:sequence>
<xs:element name="a">
<xs:simpleType name="_anon1" _isAnonymous="true" base="xs:int">
...
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
*/
func nameAnonymousTypes(root *xmltree.Element, typeCounter *int) error {
var (
updateAttr string
accum bool
)
targetNS := root.Attr("", "targetNamespace")
for _, el := range root.SearchFunc(hasAnonymousType) {
if el.Name.Space != schemaNS {
continue
}
switch el.Name.Local {
case "element", "attribute":
updateAttr = "type"
accum = false
case "list":
updateAttr = "itemType"
accum = false
case "restriction":
updateAttr = "base"
accum = false
case "union":
updateAttr = "memberTypes"
accum = true
default:
return fmt.Errorf("Did not expect <%s> to have an anonymous type",
el.Prefix(el.Name))
}
for i := 0; i < len(el.Children); i++ {
t := el.Children[i]
if !isAnonymousType(&t) {
continue
}
*typeCounter++
name := anonTypeName(*typeCounter, targetNS)
qname := el.Prefix(name)
t.SetAttr("", "name", name.Local)
t.SetAttr("", "_isAnonymous", "true")
if accum {
qname = el.Attr("", updateAttr) + " " + qname
}
el.SetAttr("", updateAttr, qname)
el.Children = append(el.Children[:i], el.Children[i+1:]...)
el.Content = nil
root.Children = append(root.Children, t)
if !accum {
break
}
}
}
return nil
}
/*
Dereference all ref= links within a document.
<attribute name="id" type="xsd:ID" />
<complexType name="MyType">
<attribute ref="tns:id" />
</complexType>
becomes
<complexType name="MyType">
<attribute name="id" type="xsd:ID" />
</complexType>
*/
func flattenRef(schema []*xmltree.Element) error {
var (
depends = new(dependency.Graph)
index = indexSchema(schema)
)
for id, el := range index.eltByID {
if el.Attr("", "ref") == "" {
continue
}
name := el.Resolve(el.Attr("", "ref"))
if dep, ok := index.ElementID(name, el.Name); !ok {
return fmt.Errorf("could not find ref %s in %s",
el.Attr("", "ref"), el)
} else {
depends.Add(id, dep)
}
}
depends.Flatten(func(id int) {
el := index.eltByID[id]
if el.Attr("", "ref") == "" {
return
}
ref := el.Resolve(el.Attr("", "ref"))
real, ok := index.ByName(ref, el.Name)
if !ok {
panic("bug building dep tree; missing " + el.Attr("", "ref"))
}
*el = *deref(el, real)
})
for ns, doc := range schema {
unpackGroups(doc)
if hasCycle(doc, nil) {
return fmt.Errorf("cycle detected after flattening references "+
"in schema %d:\n%s", ns, xmltree.MarshalIndent(doc, "", " "))
}
}
return nil
}
// Flatten a reference to an XML element, returning the full XML
// object.
func deref(ref, real *xmltree.Element) *xmltree.Element {
el := new(xmltree.Element)
el.Scope = ref.Scope
el.Name = real.Name
el.StartElement.Attr = append([]xml.Attr{}, real.StartElement.Attr...)
el.Content = append([]byte{}, real.Content...)
el.Children = append([]xmltree.Element{}, real.Children...)
// Some attributes can contain a qname, and must be converted to use the
// xmlns prefixes in ref's scope.
hasQName := map[xml.Name]bool{
xml.Name{"", "type"}: true,
}
for i, attr := range el.StartElement.Attr {
if hasQName[attr.Name] {
xmlname := real.Resolve(attr.Value)
attr.Value = ref.Prefix(xmlname)
el.StartElement.Attr[i] = attr
}
}
// If there are child elements, rather than checking all children
// for xmlns conversion, we can just import the xmlns prefixes
if len(el.Children) > 0 {
el.Scope = *real.JoinScope(&ref.Scope)
}
// Attributes added to the reference overwrite attributes in the
// referenced element.
for _, attr := range ref.StartElement.Attr {
if (attr.Name != xml.Name{"", "ref"}) {
el.SetAttr(attr.Name.Space, attr.Name.Local, attr.Value)
}
}
return el
}
// After dereferencing groups and attributeGroups, we need to
// unpack them within their parent elements.
func unpackGroups(doc *xmltree.Element) {
isGroup := or(isElem(schemaNS, "group"), isElem(schemaNS, "attributeGroup"))
hasGroups := hasChild(isGroup)
for _, el := range doc.SearchFunc(hasGroups) {
children := make([]xmltree.Element, 0, len(el.Children))
for _, c := range el.Children {
if isGroup(&c) {
children = append(children, c.Children...)
} else {
children = append(children, c)
}
}
el.Children = children
}
}
// a complex type defined without any simpleContent or
// complexContent is interpreted as shorthand for complex
// content that restricts anyType.
func expandComplexShorthand(root *xmltree.Element) {
isComplexType := isElem(schemaNS, "complexType")
Loop:
for _, el := range root.SearchFunc(isComplexType) {
newChildren := make([]xmltree.Element, 0, len(el.Children))
restrict := xmltree.Element{
Scope: el.Scope,
Children: make([]xmltree.Element, 0, len(el.Children)),
}
for _, child := range el.Children {
if child.Name.Space != schemaNS {
newChildren = append(newChildren, child)
continue
}
switch child.Name.Local {
case "annotation":
newChildren = append(newChildren, child)
continue
case "simpleContent", "complexContent":
continue Loop
}
restrict.Children = append(restrict.Children, child)
}
restrict.Name.Space = schemaNS
restrict.Name.Local = "restriction"
restrict.SetAttr("", "base", restrict.Prefix(AnyType.Name()))
content := xmltree.Element{
Scope: el.Scope,
Children: []xmltree.Element{restrict},
}
content.Name.Space = schemaNS
content.Name.Local = "complexContent"
el.Content = nil
el.Children = append(newChildren, content)
}
}
func (s *Schema) addElementTypeAliases(root *xmltree.Element, types map[xml.Name]Type) error {
for _, el := range root.Children {
if (el.Name != xml.Name{schemaNS, "element"}) {
continue
}
name := el.ResolveDefault(el.Attr("", "name"), s.TargetNS)
ref := el.Resolve(el.Attr("", "type"))
if ref.Local == "" || name.Local == "" {
continue
}
if _, ok := s.Types[name]; !ok {
if t, ok := s.lookupType(linkedType(ref), types); !ok {
return fmt.Errorf("could not lookup type %s for element %s",
el.Prefix(ref), el.Prefix(name))
} else {
s.Types[name] = t
}
}
}
return nil
}
// Propagate the "mixed" attribute of a type appropriately to
// all types derived from it. For the propagation rules, see
// https://www.w3.org/TR/xmlschema-1/#coss-ct. That Definition is written
// for a computer, so I've translated the relevant portion into plain
// English. My translation may be incorrect; check the reference if you
// think so. The rules are as follows:
//
// - When extending a complex type, the derived type *must* be mixed iff
// the base type is mixed.
// - When restricting a complex type, the derived type *may* be mixed iff
// the base type is mixed.
// - The builtin "xs:anyType" is mixed.
//
// This package extends the concept of "Mixed" to apply to complex types
// with simpleContent. This is done because Mixed is used as an indicator
// that the user should care about the chardata content in a type.
func (s *Schema) propagateMixedAttr() {
for _, t := range s.Types {
propagateMixedAttr(t, Base(t), 0)
}
}
func propagateMixedAttr(t, b Type, depth int) {
const maxDepth = 1000
if b == nil || depth > maxDepth {
return
}
// Mixed attr needs to "bubble up" from the bottom, so we
// recurse to do this backwards.
propagateMixedAttr(b, Base(b), depth+1)
c, ok := t.(*ComplexType)
if !ok || c.Mixed {
return
}
switch b := b.(type) {
case Builtin:
if b == AnyType {
c.Mixed = c.Mixed || c.Extends
}
case *ComplexType:
if c.Extends {
c.Mixed = b.Mixed
}
case *SimpleType:
c.Mixed = true
default:
panic(fmt.Sprintf("unexpected %T", b))
}
}
// 3.2.2 XML Representation of Attribute Declaration Schema Components
//
// Specifies that attributes without a type default to anySimpleType.
//
// http://www.w3.org/TR/xmlschema-1/#cAttribute_Declarations
func attributeDefaultType(root *xmltree.Element) {
var (
isAttr = isElem(schemaNS, "attribute")
hasNoType = hasAttrValue("", "type", "")
anyType = xml.Name{Space: schemaNS, Local: "anySimpleType"}
)
for _, el := range root.SearchFunc(and(isAttr, hasNoType)) {
el.SetAttr("", "type", el.Prefix(anyType))
}
}
// 3.3.2 XML Representation of Element Declaration Schema Components
//
// Elements types default to anyType
//
// https://www.w3.org/TR/xmlschema-1/#Element_Declaration_details
func elementDefaultType(root *xmltree.Element) {
var (
isElement = isElem(schemaNS, "element")
hasNoType = hasAttrValue("", "type", "")
anyType = xml.Name{Space: schemaNS, Local: "anyType"}
)
for _, el := range root.SearchFunc(and(isElement, hasNoType)) {
el.SetAttr("", "type", el.Prefix(anyType))
}
}
func (s *Schema) parse(root *xmltree.Element) error {
return s.parseTypes(root)
}
func (s *Schema) parseTypes(root *xmltree.Element) (err error) {
defer catchParseError(&err)
tns := root.Attr("", "targetNamespace")
for _, el := range root.Search(schemaNS, "complexType") {
t := s.parseComplexType(el)
s.Types[t.Name] = t
}
for _, el := range root.Search(schemaNS, "simpleType") {
t := s.parseSimpleType(el)
s.Types[t.Name] = t
}
s.Types[xml.Name{tns, "_self"}] = s.parseSelfType(root)
return err
}
func (s *Schema) parseSelfType(root *xmltree.Element) *ComplexType {
self := *root
self.Content = nil
self.Children = nil
for _, el := range root.Children {
if (el.Name == xml.Name{schemaNS, "element"}) {
self.Children = append(self.Children, el)
}
}
newdoc := self
self.Name.Local = "complexType"
self.SetAttr("", "name", "_self")
newdoc.Children = []xmltree.Element{self}
expandComplexShorthand(&newdoc)
return s.parseComplexType(&newdoc.Children[0])
}
// http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#element-complexType
func (s *Schema) parseComplexType(root *xmltree.Element) *ComplexType {
var t ComplexType
var doc annotation
t.Name = root.ResolveDefault(root.Attr("", "name"), s.TargetNS)
t.Abstract = parseBool(root.Attr("", "abstract"))
t.Mixed = parseBool(root.Attr("", "mixed"))
// We set this special attribute in a pre-processing step.
t.Anonymous = (root.Attr("", "_isAnonymous") == "true")
walk(root, func(el *xmltree.Element) {
switch el.Name.Local {
case "annotation":
doc = doc.append(parseAnnotation(el))
case "simpleContent":
t.parseSimpleContent(s.TargetNS, el)
case "complexContent":
t.parseComplexContent(s.TargetNS, el)
default:
stop("unexpected element " + el.Name.Local)
}
})
t.Doc += string(doc)
return &t
}
// simpleContent indicates that the content model of the new type
// contains only character data and no elements
func (t *ComplexType) parseSimpleContent(ns string, root *xmltree.Element) {
var doc annotation
t.Mixed = true
walk(root, func(el *xmltree.Element) {
switch el.Name.Local {
case "annotation":
doc = doc.append(parseAnnotation(el))
case "restriction":
t.Base = parseType(el.Resolve(el.Attr("", "base")))
case "extension":
t.Base = parseType(el.Resolve(el.Attr("", "base")))
t.Extends = true
for _, v := range el.Search(schemaNS, "attribute") {
t.Attributes = append(t.Attributes, parseAttribute(ns, v))
}
}
})
t.Doc += string(doc)
}
// The complexContent element signals that we intend to restrict or extend
// the content model of a complex type.
func (t *ComplexType) parseComplexContent(ns string, root *xmltree.Element) {
var doc annotation
if mixed := root.Attr("", "mixed"); mixed != "" {
t.Mixed = parseBool(mixed)
}
walk(root, func(el *xmltree.Element) {
switch el.Name.Local {
case "extension":
t.Extends = true
fallthrough
case "restriction":
t.Base = parseType(el.Resolve(el.Attr("", "base")))
for _, v := range el.Search(schemaNS, "any") {
t.Elements = append(t.Elements, parseAnyElement(ns, v))
break
}
usedElt := make(map[xml.Name]int)
for _, v := range el.Search(schemaNS, "element") {
elt := parseElement(ns, v)
if existing, ok := usedElt[elt.Name]; !ok {
usedElt[elt.Name] = len(t.Elements)
t.Elements = append(t.Elements, elt)
} else {
t.Elements[existing] = joinElem(t.Elements[existing], elt)
}
}
for _, v := range el.Search(schemaNS, "attribute") {
t.Attributes = append(t.Attributes, parseAttribute(ns, v))
}
case "annotation":
doc = doc.append(parseAnnotation(el))
default:
stop("unexpected element " + el.Name.Local)
}
})
t.Doc += string(doc)
}
func joinElem(a, b Element) Element {
if a.Doc != "" {
a.Doc += "\n"
}
a.Doc += b.Doc
a.Abstract = a.Abstract && b.Abstract
a.Plural = a.Plural || b.Plural
a.Optional = a.Optional || b.Optional
a.Nillable = a.Nillable || b.Nillable
if a.Default != b.Default {
a.Default = ""
}
return a
}
func parseInt(s string) int {
s = strings.TrimSpace(s)
switch s {
case "":
return 0
case "unbounded":
return -1
}
n, err := strconv.Atoi(s)
if err != nil {
stop(err.Error())
}
return n
}
// https://www.w3.org/TR/xmlschema-2/#decimal
func parseDecimal(s string) float64 {
s = strings.TrimSpace(s)
switch s {
case "":
return 0
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
stop(err.Error())
}
return n
}
func parseBool(s string) bool {
switch s {
case "", "0", "false":
return false
case "1", "true":
return true
}
stop("Invalid boolean value " + s)
return false
}
func parsePlural(el *xmltree.Element) bool {
if min := parseInt(el.Attr("", "minOccurs")); min > 1 {
return true
} else if max := parseInt(el.Attr("", "maxOccurs")); max < 0 || max > 1 {
return true
}
return false
}
func parseAnyElement(ns string, el *xmltree.Element) Element {
var base Type = AnyType
typeattr := el.Attr("", "type")
if typeattr != "" {
base = parseType(el.Resolve(typeattr))
}
return Element{
Plural: parsePlural(el),
Type: base,
Wildcard: true,
}
}
func parseElement(ns string, el *xmltree.Element) Element {
var doc annotation
e := Element{
Name: el.ResolveDefault(el.Attr("", "name"), ns),
Type: parseType(el.Resolve(el.Attr("", "type"))),
Default: el.Attr("", "default"),
Abstract: parseBool(el.Attr("", "abstract")),
Nillable: parseBool(el.Attr("", "nillable")),
Plural: parsePlural(el),
Scope: el.Scope,
}
if el.Attr("", "type") == "" {
e.Type = AnyType
}
if x := el.Attr("", "minOccurs"); x != "" && parseInt(x) == 0 {
e.Optional = true
} else if e.Default != "" {
e.Optional = true
}
walk(el, func(el *xmltree.Element) {
if el.Name.Local == "annotation" {
doc = doc.append(parseAnnotation(el))
}
})
t, ok := e.Type.(linkedType)
if ok {
e.Name.Space = t.Space
}
e.Doc = string(doc)
e.Attr = el.StartElement.Attr
return e
}
func parseAttribute(ns string, el *xmltree.Element) Attribute {
var a Attribute
var doc annotation
// Non-QName xml attributes explicitly do *not* have a namespace.
if name := el.Attr("", "name"); strings.Contains(name, ":") {
a.Name = el.Resolve(el.Attr("", "name"))
} else {
a.Name.Local = name
}
a.Name.Space = ns
a.Type = parseType(el.Resolve(el.Attr("", "type")))
a.Default = el.Attr("", "default")
a.Scope = el.Scope
a.Optional = el.Attr("", "use") != "required"
walk(el, func(el *xmltree.Element) {
if el.Name.Local == "annotation" {
doc = doc.append(parseAnnotation(el))
}
})
a.Doc = string(doc)
// Other attributes could be useful later. One such attribute is
// wsdl:arrayType.
a.Attr = el.StartElement.Attr
return a
}
func (s *Schema) parseSimpleType(root *xmltree.Element) *SimpleType {
var t SimpleType
var doc annotation
t.Name = root.ResolveDefault(root.Attr("", "name"), s.TargetNS)
t.Anonymous = (root.Attr("", "_isAnonymous") == "true")
walk(root, func(el *xmltree.Element) {
switch el.Name.Local {
case "restriction":
t.Base = parseType(el.Resolve(el.Attr("", "base")))
t.Restriction = parseSimpleRestriction(el, t.Base)
case "list":
t.Base = parseType(el.Resolve(el.Attr("", "itemType")))
t.List = true
case "union":
for _, name := range strings.Fields(el.Attr("", "memberTypes")) {
type_ := parseType(el.Resolve(name))
t.Union = append(t.Union, type_)
t.Base = AnySimpleType
}
case "annotation":
doc = doc.append(parseAnnotation(el))
}
})
t.Doc = string(doc)
return &t
}
func parseAnnotation(el *xmltree.Element) (doc annotation) {
if err := xmltree.Unmarshal(el, &doc); err != nil {
stop(err.Error())
}
return doc
}
func parseSimpleRestriction(root *xmltree.Element, base Type) Restriction {
var r Restriction
var doc annotation
// Most of the restrictions on a simpleType are suited for
// validating input. This package is not a validator; we assume
// that the server sends valid data, and that it will tell us if
// our data is wrong. As such, most of the fields here are
// ignored.
walk(root, func(el *xmltree.Element) {
switch el.Name.Local {
case "enumeration":
r.Enum = append(r.Enum, el.Attr("", "value"))
case "minExclusive", "minInclusive":
r.MinDate, r.Min = parseMinMaxRestriction(el, base)
case "maxExclusive", "maxInclusive":
r.MaxDate, r.Max = parseMinMaxRestriction(el, base)
case "length":
r.Length = parseInt(el.Attr("", "value"))
case "maxLength":
r.MaxLength = parseInt(el.Attr("", "value"))
case "minLength":
r.MinLength = parseInt(el.Attr("", "value"))
case "pattern":
// We don't fully implement XML Schema's pattern language, and
// we don't want to stop a parse because of this. Instead, if we
// cannot compile a regex, we'll add the error msg to the annotation
// for this restriction.
pat := el.Attr("", "value")
if r.Pattern != nil {
pat = r.Pattern.String() + "|" + pat
}
reg, err := parsePattern(pat)
if err != nil {
msg := fmt.Sprintf("This type must conform to the pattern %q, but the XSD library could not parse the regular expression. (%v)", pat, err)
doc = doc.append(annotation(msg))
}
r.Pattern = reg
case "whiteSpace":
break // TODO(droyo)
case "fractionDigits":
r.Precision = parseInt(el.Attr("", "value"))
if r.Precision < 0 {
stop("Invalid fractionDigits value " + el.Attr("", "value"))
}
case "annotation":
doc = doc.append(parseAnnotation(el))
case "totalDigits":
r.TotalDigits = parseInt(el.Attr("", "value"))
}
})
r.Doc = string(doc)
return r
}
// For minInclusive, minExclusive, maxInclusive and maxExclusive restrictions
func parseMinMaxRestriction(el *xmltree.Element, base Type) (time.Time, float64) {
var date time.Time
var decimal float64
if v, ok := base.(Builtin); ok {
var format string
if v == Date {
format = "2006-01-02"
} else if v == DateTime {
format = time.RFC3339
}
if format != "" {
d, err := time.Parse(format, el.Attr("", "value"))
if err != nil {
stop(err.Error())
}
date = d
} else {
decimal = parseDecimal(el.Attr("", "value"))
}
} else if _, ok := base.(linkedType); ok {
// The base of the linked type is unknown, try to parse dates and decimals
d, err := time.Parse("2006-01-02", el.Attr("", "value"))
if err != nil {
d, err := time.Parse(time.RFC3339, el.Attr("", "value"))
if err != nil {
decimal = parseDecimal(el.Attr("", "value"))
}
date = d
}
date = d
} else {
decimal = parseDecimal(el.Attr("", "value"))
}