-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
247 lines (229 loc) · 6.66 KB
/
parser.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
// 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"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"golang.org/x/net/html/charset"
)
// Options holds user-defined overrides and runtime data that are used when
// parsing from an XSD document.
type Options struct {
FilePath string
FileDir string
InputDir string
OutputDir string
Extract bool
Lang string
Package string
IncludeMap map[string]bool
LocalNameNSMap map[string]string
NSSchemaLocationMap map[string]string
ParseFileList map[string]bool
ParseFileMap map[string][]interface{}
ProtoTree []interface{}
RemoteSchema map[string][]byte
InElement string
CurrentEle string
InGroup int
InUnion bool
InAttributeGroup bool
SimpleType *Stack
ComplexType *Stack
Element *Stack
Attribute *Stack
Group *Stack
AttributeGroup *Stack
Choice *Stack
}
// NewParser creates a new parser options for the Parse. Useful for XML schema
// parsing.
func NewParser(options *Options) *Options {
return options
}
// Parse reads XML documents and return proto tree for every element in the
// documents by given options. If value of the property extract is false,
// parse will fetch schema used in <import> or <include> statements.
func (opt *Options) Parse() (err error) {
opt.FileDir = filepath.Dir(opt.FilePath)
var fi os.FileInfo
fi, err = os.Stat(opt.FilePath)
if err != nil {
return
}
if fi.IsDir() {
return
}
var xmlFile *os.File
xmlFile, err = os.Open(opt.FilePath)
if err != nil {
return
}
defer xmlFile.Close()
if !opt.Extract {
opt.ParseFileList[opt.FilePath] = true
opt.ParseFileMap[opt.FilePath] = opt.ProtoTree
}
opt.ProtoTree = make([]interface{}, 0)
opt.InElement = ""
opt.CurrentEle = ""
opt.InGroup = 0
opt.InUnion = false
opt.InAttributeGroup = false
opt.SimpleType = NewStack()
opt.ComplexType = NewStack()
opt.Element = NewStack()
opt.Attribute = NewStack()
opt.Group = NewStack()
opt.AttributeGroup = NewStack()
opt.Choice = NewStack()
decoder := xml.NewDecoder(xmlFile)
decoder.CharsetReader = charset.NewReaderLabel
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch element := token.(type) {
case xml.StartElement:
opt.InElement = element.Name.Local
funcName := fmt.Sprintf("On%s", MakeFirstUpperCase(opt.InElement))
if err = callFuncByName(opt, funcName, []reflect.Value{reflect.ValueOf(element), reflect.ValueOf(opt.ProtoTree)}); err != nil {
return
}
case xml.EndElement:
funcName := fmt.Sprintf("End%s", MakeFirstUpperCase(element.Name.Local))
if err = callFuncByName(opt, funcName, []reflect.Value{reflect.ValueOf(element), reflect.ValueOf(opt.ProtoTree)}); err != nil {
return
}
case xml.CharData:
if err = opt.OnCharData(string(element), opt.ProtoTree); err != nil {
return
}
default:
}
}
if !opt.Extract {
opt.ParseFileList[opt.FilePath] = true
opt.ParseFileMap[opt.FilePath] = opt.ProtoTree
path := filepath.Join(opt.OutputDir, strings.TrimPrefix(opt.FilePath, opt.InputDir))
if err := PrepareOutputDir(filepath.Dir(path)); err != nil {
fmt.Println(err)
os.Exit(1)
}
generator := &CodeGenerator{
Lang: opt.Lang,
Package: opt.Package,
File: path,
ProtoTree: opt.ProtoTree,
StructAST: map[string]string{},
}
funcName := fmt.Sprintf("Gen%s", MakeFirstUpperCase(opt.Lang))
if err = callFuncByName(generator, funcName, []reflect.Value{}); err != nil {
return
}
}
return
}
// GetValueType convert XSD schema value type to the build-in type for the
// given value and proto tree.
func (opt *Options) GetValueType(value string, XSDSchema []interface{}) (valueType string, err error) {
if buildType, ok := getBuildInTypeByLang(trimNSPrefix(value), opt.Lang); ok {
valueType = buildType
return
}
valueType = getBasefromSimpleType(trimNSPrefix(value), XSDSchema)
if valueType != trimNSPrefix(value) && valueType != "" {
return
}
if opt.Extract {
return
}
schemaLocation := opt.NSSchemaLocationMap[opt.parseNS(value)]
if isValidURL(schemaLocation) {
return
}
xsdFile := filepath.Join(opt.FileDir, schemaLocation)
var fi os.FileInfo
fi, err = os.Stat(xsdFile)
if err != nil {
return
}
if fi.IsDir() {
// extract type of value from include schema.
valueType = ""
for include := range opt.IncludeMap {
parser := NewParser(&Options{
FilePath: filepath.Join(opt.FileDir, include),
OutputDir: opt.OutputDir,
Extract: true,
Lang: opt.Lang,
IncludeMap: opt.IncludeMap,
LocalNameNSMap: opt.LocalNameNSMap,
NSSchemaLocationMap: opt.NSSchemaLocationMap,
ParseFileList: opt.ParseFileList,
ParseFileMap: opt.ParseFileMap,
ProtoTree: make([]interface{}, 0),
})
if parser.Parse() != nil {
return
}
if vt := getBasefromSimpleType(trimNSPrefix(value), parser.ProtoTree); vt != trimNSPrefix(value) {
valueType = vt
}
}
if valueType == "" {
valueType = trimNSPrefix(value)
}
return
}
depXSDSchema, ok := opt.ParseFileMap[xsdFile]
if !ok {
parser := NewParser(&Options{
FilePath: xsdFile,
OutputDir: opt.OutputDir,
Extract: false,
Lang: opt.Lang,
IncludeMap: opt.IncludeMap,
LocalNameNSMap: opt.LocalNameNSMap,
NSSchemaLocationMap: opt.NSSchemaLocationMap,
ParseFileList: opt.ParseFileList,
ParseFileMap: opt.ParseFileMap,
ProtoTree: make([]interface{}, 0),
})
if parser.Parse() != nil {
return
}
depXSDSchema = parser.ProtoTree
}
valueType = getBasefromSimpleType(trimNSPrefix(value), depXSDSchema)
if valueType != trimNSPrefix(value) && valueType != "" {
return
}
parser := NewParser(&Options{
FilePath: xsdFile,
OutputDir: opt.OutputDir,
Extract: true,
Lang: opt.Lang,
IncludeMap: opt.IncludeMap,
LocalNameNSMap: opt.LocalNameNSMap,
NSSchemaLocationMap: opt.NSSchemaLocationMap,
ParseFileList: opt.ParseFileList,
ParseFileMap: opt.ParseFileMap,
ProtoTree: make([]interface{}, 0),
})
if parser.Parse() != nil {
return
}
valueType = getBasefromSimpleType(trimNSPrefix(value), parser.ProtoTree)
return
}