forked from corburn/goxsd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
goxsd.go
316 lines (271 loc) · 7.66 KB
/
goxsd.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
// Things not yet implemented:
// - enforcing use="restricted" on attributes
// - namespaces
package goxsd
import (
"strings"
)
// xmlTree is the representation of an XML element node in a tree. It
// contains information about whether
// - it is of a basic data type or a composite type (in which case its
// type equals its name)
// - if it represents a list of children to its parent
// - if it has children of its own
// - any attributes
// - if the element contains any character data
type xmlTree struct {
Name string
Type string
StructName string
Annotation string
List bool
Cdata bool
OmitEmpty bool
Attribs []xmlAttrib
Children []*xmlTree
}
type xmlAttrib struct {
Name string
Type string
OmitEmpty bool
}
type builder struct {
schemas []xsdSchema
complTypes map[string]xsdComplexType
simplTypes map[string]xsdSimpleType
elements map[string]struct{}
}
// NewBuilder creates a new initialized builder populated with the given
// xsdSchema slice.
func NewBuilder(schemas []xsdSchema) *builder {
return &builder{
schemas: schemas,
complTypes: make(map[string]xsdComplexType),
simplTypes: make(map[string]xsdSimpleType),
elements: make(map[string]struct{}),
}
}
// BuildXML generates and returns a tree of xmlTree objects based on a set of
// parsed XSD schemas.
func (b *builder) BuildXML() []*xmlTree {
var roots []xsdElement
for _, s := range b.schemas {
roots = append(roots, s.Elements...)
for _, t := range s.ComplexTypes {
b.complTypes[t.Name] = t
}
for _, t := range s.SimpleTypes {
b.simplTypes[t.Name] = t
}
}
var xelems []*xmlTree
for _, e := range roots {
xelems = b.appendElement(xelems, e)
}
return xelems
}
// appendElement builds an xmlTree from an xsdElement, recursively
// traversing the XSD type information to build up an XML element hierarchy.
func (b *builder) appendElement(xmlElements []*xmlTree, e xsdElement) []*xmlTree {
if _, ok := b.elements[e.Name+e.Type]; ok {
return xmlElements
}
b.elements[e.Name+e.Type] = struct{}{}
if e.Ref != "" {
e.Name, e.Type = e.Ref, e.Ref
}
typ := e.Name
if e.Type != "" {
typ = e.Type
}
xelem := &xmlTree{Name: e.Name, Type: typ, Annotation: e.Annotation}
if e.isList() {
xelem.List = true
}
if e.omittable() {
xelem.OmitEmpty = true
}
if !e.inlineType() {
switch t := b.findType(e.Type).(type) {
case xsdComplexType:
b.buildFromComplexType(xelem, t)
case xsdSimpleType:
b.buildFromSimpleType(xelem, t)
case string:
b.buildFromSimpleType(xelem, xsdSimpleType{
Restriction: xsdRestriction{Base: t},
})
}
return append(xmlElements, xelem)
}
if e.ComplexType != nil { // inline complex type
b.buildFromComplexType(xelem, *e.ComplexType)
return append(xmlElements, xelem)
}
if e.SimpleType != nil { // inline simple type
b.buildFromSimpleType(xelem, *e.SimpleType)
return append(xmlElements, xelem)
}
return append(xmlElements, xelem)
}
// buildFromComplexType takes an xmlTree and an xsdComplexType, containing
// XSD type information for xmlTree enrichment.
func (b *builder) buildFromComplexType(xelem *xmlTree, t xsdComplexType) {
if t.Choice != nil {
for _, e := range t.Choice {
e.Choice = true
xelem.Children = b.appendElement(xelem.Children, e)
}
}
if t.Sequence != nil { // Does the element have children?
for _, e := range t.Sequence {
xelem.Children = b.appendElement(xelem.Children, e)
}
}
if t.SeqChoice != nil {
for _, e := range t.SeqChoice {
e.Choice = true
xelem.Children = b.appendElement(xelem.Children, e)
}
}
if t.Attributes != nil {
b.buildFromAttributes(xelem, t.Attributes)
}
if t.ComplexContent != nil {
b.buildFromComplexContent(xelem, *t.ComplexContent)
}
if t.SimpleContent != nil {
b.buildFromSimpleContent(xelem, *t.SimpleContent)
}
}
// buildFromSimpleType assumes restriction child and fetches the base value,
// assuming that value is of a XSD built-in data type.
func (b *builder) buildFromSimpleType(xelem *xmlTree, t xsdSimpleType) {
xelem.Type = b.findType(t.Restriction.Base).(string)
}
func (b *builder) buildFromComplexContent(xelem *xmlTree, c xsdComplexContent) {
if c.Extension != nil {
b.buildFromExtension(xelem, c.Extension)
}
}
// A simple content can refer to a text-only complex type
func (b *builder) buildFromSimpleContent(xelem *xmlTree, c xsdSimpleContent) {
if c.Extension != nil {
b.buildFromExtension(xelem, c.Extension)
}
if c.Restriction != nil {
b.buildFromRestriction(xelem, c.Restriction)
}
}
// buildFromExtension extends an existing type, simple or complex, with a
// sequence.
func (b *builder) buildFromExtension(xelem *xmlTree, e *xsdExtension) {
switch t := b.findType(e.Base).(type) {
case xsdComplexType:
b.buildFromComplexType(xelem, t)
case xsdSimpleType:
b.buildFromSimpleType(xelem, t)
// If element is of simpleType and has attributes, it must collect
// its value as chardata.
if e.Attributes != nil {
xelem.Cdata = true
}
default:
xelem.Type = t.(string)
// If element is of built-in type but has attributes, it must collect
// its value as chardata.
if e.Attributes != nil {
xelem.Cdata = true
}
}
if e.Sequence != nil {
for _, e := range e.Sequence {
xelem.Children = b.appendElement(xelem.Children, e)
}
}
if e.All != nil {
for _, e := range e.All {
xelem.Children = b.appendElement(xelem.Children, e)
}
}
if e.Attributes != nil {
b.buildFromAttributes(xelem, e.Attributes)
}
}
func (b *builder) buildFromRestriction(xelem *xmlTree, r *xsdRestriction) {
switch t := b.findType(r.Base).(type) {
case xsdSimpleType:
b.buildFromSimpleType(xelem, t)
case xsdComplexType:
b.buildFromComplexType(xelem, t)
case xsdComplexContent:
panic("Restriction on complex content is not implemented")
default:
panic("Unexpected base type to restriction")
}
}
func (b *builder) buildFromAttributes(xelem *xmlTree, attrs []xsdAttribute) {
for _, a := range attrs {
attr := xmlAttrib{Name: a.Name}
switch typ := b.findType(a.Type).(type) {
case xsdSimpleType:
// Get type name from simpleType
// If Restriction.Base is a simpleType or complexType, we panic
attr.Type = b.findType(typ.Restriction.Base).(string)
case string:
if a.SimpleType != nil {
if t, ok := b.findType(a.SimpleType.Restriction.Base).(string); ok {
typ = t
}
}
// If empty, then simpleType is present as content, but we ignore that now.
attr.Type = typ
}
if a.Use == "optional" {
attr.OmitEmpty = true
}
xelem.Attribs = append(xelem.Attribs, attr)
}
}
// findType takes a type name and checks if it is a registered XSD type
// (simple or complex), in which case that type is returned. If no such
// type can be found, the XSD specific primitive types are mapped to their
// Go correspondents. If no XSD type was found, the type name itself is
// returned.
func (b *builder) findType(name string) interface{} {
name = stripNamespace(name)
if t, ok := b.complTypes[name]; ok {
return t
}
if t, ok := b.simplTypes[name]; ok {
return t
}
switch name {
case "boolean":
return "bool"
case "language", "Name", "token", "duration", "anyURI", "normalizedString":
// FIXME: these types have additional constraints over string.
// For example, a token cannot have leading/trailing whitespace.
return "string"
case "long", "short", "integer", "int":
return "int"
case "positiveInteger", "nonNegativeInteger":
return "uint"
case "unsignedShort":
return "uint16"
case "decimal":
return "float64"
case "dateTime", "date":
return "time.Time"
case "time":
return "time.Duration"
default:
return name
}
}
func stripNamespace(name string) string {
if s := strings.Split(name, ":"); len(s) > 1 {
return s[len(s)-1]
}
return name
}