forked from metaleap/go-xsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsd-schema.go
308 lines (287 loc) · 7.91 KB
/
xsd-schema.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
package xsd
import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-utils/ufs"
"github.com/go-utils/unet"
"github.com/go-utils/ustr"
"fmt"
)
const (
goPkgPrefix = ""
goPkgSuffix = "_go"
protSep = "://"
xsdNamespaceUri = "http://www.w3.org/2001/XMLSchema"
)
var (
loadedSchemas = map[string]*Schema{}
)
type Schema struct {
elemBase
XMLName xml.Name `xml:"schema"`
XMLNamespacePrefix string `xml:"-"`
XMLNamespaces map[string]string `xml:"-"`
XMLIncludedSchemas []*Schema `xml:"-"`
XSDNamespacePrefix string `xml:"-"`
XSDParentSchema *Schema `xml:"-"`
hasAttrAttributeFormDefault
hasAttrBlockDefault
hasAttrElementFormDefault
hasAttrFinalDefault
hasAttrLang
hasAttrId
hasAttrSchemaLocation
hasAttrTargetNamespace
hasAttrVersion
hasElemAnnotation
hasElemsAttribute
hasElemsAttributeGroup
hasElemsComplexType
hasElemsElement
hasElemsGroup
hasElemsInclude
hasElemsImport
hasElemsNotation
hasElemsRedefine
hasElemsSimpleType
loadLocalPath, loadUri string
}
func (me *Schema) allSchemas(loadedSchemas map[string]bool) (schemas []*Schema) {
schemas = append(schemas, me)
loadedSchemas[me.loadUri] = true
for _, ss := range me.XMLIncludedSchemas {
if v, ok := loadedSchemas[ss.loadUri]; ok && v {
continue
}
schemas = append(schemas, ss.allSchemas(loadedSchemas)...)
}
return
}
func (me *Schema) collectGlobals(bag *PkgBag, loadedSchemas map[string]bool) {
loadedSchemas[me.loadUri] = true
for _, att := range me.Attributes {
bag.allAtts = append(bag.allAtts, att)
}
for _, agr := range me.AttributeGroups {
bag.allAttGroups = append(bag.allAttGroups, agr)
}
for _, el := range me.Elements {
bag.allElems = append(bag.allElems, el)
}
for _, egr := range me.Groups {
bag.allElemGroups = append(bag.allElemGroups, egr)
}
for _, not := range me.Notations {
bag.allNotations = append(bag.allNotations, not)
}
for _, ss := range me.XMLIncludedSchemas {
if v, ok := loadedSchemas[ss.loadUri]; ok && v {
continue
}
ss.collectGlobals(bag, loadedSchemas)
}
}
func (me *Schema) globalComplexType(bag *PkgBag, name string, loadedSchemas map[string]bool) (ct *ComplexType) {
var imp string
for _, ct = range me.ComplexTypes {
if bag.resolveQnameRef(ustr.PrefixWithSep(me.XMLNamespacePrefix, ":", ct.Name.String()), "T", &imp) == name {
return
}
}
loadedSchemas[me.loadUri] = true
for _, ss := range me.XMLIncludedSchemas {
if v, ok := loadedSchemas[ss.loadUri]; ok && v {
//fmt.Printf("Ignoring processed schema: %s\n", ss.loadUri)
continue
}
if ct = ss.globalComplexType(bag, name, loadedSchemas); ct != nil {
return
}
}
ct = nil
return
}
func (me *Schema) globalElement(bag *PkgBag, name string) (el *Element) {
var imp string
if len(name) > 0 {
var rname = bag.resolveQnameRef(name, "", &imp)
for _, el = range me.Elements {
if bag.resolveQnameRef(ustr.PrefixWithSep(me.XMLNamespacePrefix, ":", el.Name.String()), "", &imp) == rname {
return
}
}
for _, ss := range me.XMLIncludedSchemas {
if el = ss.globalElement(bag, name); el != nil {
return
}
}
}
el = nil
return
}
func (me *Schema) globalSubstitutionElems(el *Element, loadedSchemas map[string]bool) (els []*Element) {
var elName = el.Ref.String()
if len(elName) == 0 {
elName = el.Name.String()
}
for _, tle := range me.Elements {
if (tle != el) && (len(tle.SubstitutionGroup) > 0) {
if tle.SubstitutionGroup.String()[(strings.Index(tle.SubstitutionGroup.String(), ":")+1):] == elName {
els = append(els, tle)
}
}
}
loadedSchemas[me.loadUri] = true
for _, inc := range me.XMLIncludedSchemas {
if v, ok := loadedSchemas[inc.loadUri]; ok && v {
//fmt.Printf("Ignoring processed schema: %s\n", inc.loadUri)
continue
}
els = append(els, inc.globalSubstitutionElems(el, loadedSchemas)...)
}
return
}
func (me *Schema) MakeGoPkgSrcFile() (goOutFilePath string, err error) {
var goOutDirPath = filepath.Join(filepath.Dir(me.loadLocalPath), goPkgPrefix+filepath.Base(me.loadLocalPath)+goPkgSuffix)
goOutFilePath = filepath.Join(goOutDirPath, path.Base(me.loadUri)+".go")
var bag = newPkgBag(me)
loadedSchemas := make(map[string]bool)
for _, inc := range me.allSchemas(loadedSchemas) {
bag.Schema = inc
inc.makePkg(bag)
}
bag.Schema = me
me.hasElemAnnotation.makePkg(bag)
bag.appendFmt(true, "")
me.makePkg(bag)
if err = ufs.EnsureDirExists(filepath.Dir(goOutFilePath)); err == nil {
err = ufs.WriteTextFile(goOutFilePath, bag.assembleSource())
}
return
}
func (me *Schema) onLoad(rootAtts []xml.Attr, loadUri, localPath string) (err error) {
var tmpUrl string
var sd *Schema
loadedSchemas[loadUri] = me
me.loadLocalPath, me.loadUri = localPath, loadUri
me.XMLNamespaces = map[string]string{}
for _, att := range rootAtts {
if att.Name.Space == "xmlns" {
me.XMLNamespaces[att.Name.Local] = att.Value
} else if len(att.Name.Space) > 0 {
} else if att.Name.Local == "xmlns" {
me.XMLNamespaces[""] = att.Value
}
}
for k, v := range me.XMLNamespaces {
if v == xsdNamespaceUri {
me.XSDNamespacePrefix = k
} else if v == me.TargetNamespace.String() {
me.XMLNamespacePrefix = k
}
}
if len(me.XMLNamespaces["xml"]) == 0 {
me.XMLNamespaces["xml"] = "http://www.w3.org/XML/1998/namespace"
}
me.XMLIncludedSchemas = []*Schema{}
for _, inc := range me.Includes {
if tmpUrl = inc.SchemaLocation.String(); strings.Index(tmpUrl, protSep) < 0 {
tmpUrl = path.Join(path.Dir(loadUri), tmpUrl)
}
var ok bool
var toLoadUri string
if pos := strings.Index(tmpUrl, protSep); pos >= 0 {
toLoadUri = tmpUrl[pos+len(protSep):]
} else {
toLoadUri = tmpUrl
}
if sd, ok = loadedSchemas[toLoadUri]; !ok {
if sd, err = LoadSchema(tmpUrl, len(localPath) > 0); err != nil {
return
}
}
sd.XSDParentSchema = me
me.XMLIncludedSchemas = append(me.XMLIncludedSchemas, sd)
}
me.initElement(nil)
return
}
func (me *Schema) RootSchema(pathSchemas []string) *Schema {
if me.XSDParentSchema != nil {
for _, sch := range pathSchemas {
if me.XSDParentSchema.loadUri == sch {
fmt.Printf("schema loop detected %+v - > %s!\n", pathSchemas, me.XSDParentSchema.loadUri)
return me
}
}
pathSchemas = append(pathSchemas, me.loadUri)
return me.XSDParentSchema.RootSchema(pathSchemas)
}
return me
}
func ClearLoadedSchemasCache() {
loadedSchemas = map[string]*Schema{}
}
func loadSchema(r io.Reader, loadUri, localPath string) (sd *Schema, err error) {
var data []byte
var rootAtts []xml.Attr
if data, err = ioutil.ReadAll(r); err == nil {
var t xml.Token
sd = new(Schema)
for xd := xml.NewDecoder(bytes.NewReader(data)); err == nil; {
if t, err = xd.Token(); err == nil {
if startEl, ok := t.(xml.StartElement); ok {
rootAtts = startEl.Attr
break
}
}
}
if err = xml.Unmarshal(data, sd); err == nil {
err = sd.onLoad(rootAtts, loadUri, localPath)
}
if err != nil {
sd = nil
}
}
return
}
func loadSchemaFile(filename string, loadUri string) (sd *Schema, err error) {
var file *os.File
if file, err = os.Open(filename); err == nil {
defer file.Close()
sd, err = loadSchema(file, loadUri, filename)
}
return
}
func LoadSchema(uri string, localCopy bool) (sd *Schema, err error) {
var protocol, localPath string
var rc io.ReadCloser
if pos := strings.Index(uri, protSep); pos < 0 {
protocol = "http" + protSep
} else {
protocol = uri[:pos+len(protSep)]
uri = uri[pos+len(protSep):]
}
if localCopy {
if localPath = filepath.Join(PkgGen.BaseCodePath, uri); !ufs.FileExists(localPath) {
if err = ufs.EnsureDirExists(filepath.Dir(localPath)); err == nil {
err = unet.DownloadFile(protocol+uri, localPath)
}
}
if err == nil {
if sd, err = loadSchemaFile(localPath, uri); sd != nil {
sd.loadLocalPath = localPath
}
}
} else if rc, err = unet.OpenRemoteFile(protocol + uri); err == nil {
defer rc.Close()
sd, err = loadSchema(rc, uri, "")
}
return
}