forked from xuri/xgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlGroup.go
82 lines (75 loc) · 2.1 KB
/
xmlGroup.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
// Copyright 2020 - 2021 The xgen Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package xgen written in pure Go providing a set of functions that allow you
// to parse XSD (XML schema files). This library needs Go version 1.10 or
// later.
package xgen
import "encoding/xml"
// OnGroup handles parsing event on the group start elements. The group
// element is used to define a group of elements to be used in complex type
// definitions.
func (opt *Options) OnGroup(ele xml.StartElement, protoTree []interface{}) (err error) {
group := Group{}
for _, attr := range ele.Attr {
if attr.Name.Local == "name" {
group.Name = attr.Value
}
if attr.Name.Local == "ref" {
group.Name = attr.Value
group.Ref, err = opt.GetValueType(attr.Value, protoTree)
if err != nil {
return
}
}
if attr.Name.Local == "maxOccurs" {
if attr.Value != "0" {
group.Plural = true
}
}
}
if opt.Choice.Len() > 0 {
group.Plural = group.Plural || opt.Choice.Peek().(*Choice).Plural
}
if opt.ComplexType.Len() == 0 {
if opt.InGroup == 0 {
opt.InGroup++
opt.CurrentEle = opt.InElement
opt.Group.Push(&group)
return
}
if opt.InGroup > 0 {
opt.InGroup++
opt.Group.Peek().(*Group).Groups = append(opt.Group.Peek().(*Group).Groups, group)
return
}
}
if opt.ComplexType.Len() > 0 {
if !inGroups(&group, opt.ComplexType.Peek().(*ComplexType).Groups) {
opt.ComplexType.Peek().(*ComplexType).Groups = append(opt.ComplexType.Peek().(*ComplexType).Groups, group)
}
return
}
return
}
// EndGroup handles parsing event on the group end elements.
func (opt *Options) EndGroup(ele xml.EndElement, protoTree []interface{}) (err error) {
if ele.Name.Local == opt.CurrentEle && opt.InGroup == 1 {
opt.ProtoTree = append(opt.ProtoTree, opt.Group.Pop())
opt.CurrentEle = ""
opt.InGroup--
}
if ele.Name.Local == opt.CurrentEle {
opt.InGroup--
}
return
}
func inGroups(group *Group, groups []Group) bool {
for _, g := range groups {
if g.Name == group.Name {
return true
}
}
return false
}