forked from ivarg/goxsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goxsd.go
297 lines (256 loc) · 6.92 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
// Things not yet implemented:
// - enforcing use="restricted" on attributes
// - namespaces
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
var (
output, pckg, prefix string
exported bool
usage = `Usage: goxsd [options] <xsd_file>
Options:
-o <file> Destination file [default: stdout]
-p <package> Package name [default: goxsd]
-e Generate exported structs [default: false]
-x <prefix> Struct name prefix [default: ""]
goxsd is a tool for generating XML decoding/encoding Go structs, according
to an XSD schema.
`
)
func main() {
flag.StringVar(&output, "o", "", "Name of output file")
flag.StringVar(&pckg, "p", "goxsd", "Name of the Go package")
flag.StringVar(&prefix, "x", "", "Name of the Go package")
flag.BoolVar(&exported, "e", false, "Generate exported structs")
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Println(usage)
os.Exit(1)
}
xsdFile := flag.Arg(0)
s, err := parseXSDFile(xsdFile)
if err != nil {
log.Fatal(err)
}
out := os.Stdout
if output != "" {
if out, err = os.Create(output); err != nil {
fmt.Println("Could not create or truncate output file:", output)
os.Exit(1)
}
}
bldr := builder{
schemas: s,
complTypes: make(map[string]xsdComplexType),
simplTypes: make(map[string]xsdSimpleType),
}
gen := generator{
pkg: pckg,
prefix: prefix,
exported: exported,
}
if err := gen.do(out, bldr.buildXML()); err != nil {
fmt.Println("Code generation failed unexpectedly:", err.Error())
os.Exit(1)
}
}
type xmlTree struct {
Name string
Type string
List bool
Cdata bool
Attribs []xmlAttrib
Children []*xmlTree
}
type xmlAttrib struct {
Name string
Type string
}
type builder struct {
schemas []xsdSchema
complTypes map[string]xsdComplexType
simplTypes map[string]xsdSimpleType
}
func (b builder) buildXML() []*xmlTree {
var roots []xsdElement
for _, s := range b.schemas {
for _, e := range s.Elements {
roots = append(roots, e)
}
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 = append(xelems, b.buildFromElement(e))
}
return xelems
}
// buildFromElement builds an xmlElem from an xsdElement, recursively
// traversing the XSD type information to build up an XML element hierarchy.
func (b builder) buildFromElement(e xsdElement) *xmlTree {
xelem := &xmlTree{Name: e.Name, Type: e.Name}
if e.isList() {
xelem.List = 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:
xelem.Type = t
}
return xelem
}
if e.ComplexType != nil { // inline complex type
b.buildFromComplexType(xelem, *e.ComplexType)
return xelem
}
if e.SimpleType != nil { // inline simple type
b.buildFromSimpleType(xelem, *e.SimpleType)
return xelem
}
return xelem
}
// buildFromComplexType takes an xmlElem and an xsdComplexType, containing
// XSD type information for xmlElem enrichment.
func (b builder) buildFromComplexType(xelem *xmlTree, t xsdComplexType) {
if t.Sequence != nil { // Does the element have children?
for _, e := range t.Sequence {
xelem.Children = append(xelem.Children, b.buildFromElement(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 = append(xelem.Children, b.buildFromElement(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 t := 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(t.Restriction.Base).(string)
case string:
// If empty, then simpleType is present as content, but we ignore
// that now
attr.Type = t
}
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":
return "string"
case "long", "short", "integer", "int":
return "int"
case "unsignedShort":
return "uint16"
case "decimal":
return "float64"
case "dateTime":
return "time.Time"
default:
return name
}
}
func stripNamespace(name string) string {
if s := strings.Split(name, ":"); len(s) > 1 {
return s[len(s)-1]
}
return name
}