-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_schemas.go
270 lines (225 loc) · 7.07 KB
/
parse_schemas.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
//go:build ignore
package main
import (
"encoding/xml"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/net/html/charset"
)
func parseSchema(schemaFile string, outFilePath string) error {
var numChildren int
replacer := strings.NewReplacer(".xsd", "", "schemas/", "")
var packageName = strings.ToLower(replacer.Replace(schemaFile))
xmlFile, err := os.Open(schemaFile)
if err != nil {
return fmt.Errorf("cannot open source file: %v", err)
}
defer xmlFile.Close()
decoder := xml.NewDecoder(xmlFile)
decoder.CharsetReader = charset.NewReaderLabel
outFile, err := os.OpenFile(outFilePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("cannot open dest file for writing: %v", err)
}
outFile.Truncate(0)
defer outFile.Close()
outFile.WriteString("// this file is generated by go generate. Please do not modify it manually!\n")
outFile.WriteString(fmt.Sprintf("package %s\n", packageName))
outFile.WriteString("\n")
outFile.WriteString(fmt.Sprintf("var %sChildrenOrder = map[string]map[string]int{\n", strings.Replace(filepath.Base(schemaFile), ".xsd", "", 1)))
for section, childrenNodes := range specNodeOrder(decoder) {
outFile.WriteString("\t" + fmt.Sprintf(`"%s": {`, section))
numChildren = len(childrenNodes)
for i, child := range childrenNodes {
outFile.WriteString(fmt.Sprintf(`"%s": %d`, child, i))
if i < numChildren-1 {
outFile.WriteString(",")
}
}
outFile.WriteString("},\n")
}
outFile.WriteString("}")
return nil
}
type NodeOrderTree struct {
complexTypes map[string]bool
sequenceOrder map[string][]string
nodeType map[string]string
}
func (not *NodeOrderTree) recurseResolveNodeOrder() {
for path, _type := range not.nodeType {
fmt.Printf("type of %s is %s\n", path, _type)
not.recurseResolveNodeOrderIterator(path, _type)
}
}
func (not *NodeOrderTree) recurseResolveNodeOrderIterator(rootPath string, _type string) {
order, exists := not.sequenceOrder[_type]
if !exists {
return
}
for _, field := range order {
fieldType, exists := not.nodeType[_type+"."+field]
if !exists {
continue
}
not.recurseResolveNodeOrderIterator(rootPath+"."+field, fieldType)
}
not.sequenceOrder[rootPath] = order
}
// func (not *NodeOrderTree) resolveRecurseReplacementIterator(path string, typeName string) {
// _, exists := not.complexTypes[typeName]
// if !exists {
// return
// }
// fields := not.sequenceOrder[typeName]
// for field := range fields {
// resolveRecurseReplacementIterator(path + "." + field, "")
// }
// not.sequenceOrder[path] =
// }
func specNodeOrder(decoder *xml.Decoder) map[string][]string {
var localPath = []string{}
var nodeName string
var fullPath string
var elementName string
var nodeOrderTree *NodeOrderTree = &NodeOrderTree{
complexTypes: make(map[string]bool),
sequenceOrder: make(map[string][]string),
nodeType: make(map[string]string),
}
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch element := token.(type) {
case xml.StartElement:
elementName = element.Name.Local
nodeName = elementName
if name := getAttrib(element.Attr, "name"); name != "" {
nodeName = name
}
// fmt.Printf("full path = %s\n", fullPath)
if elementName == "complexType" {
if name := getAttrib(element.Attr, "name"); name != "" {
nodeOrderTree.complexTypes[name] = true
}
}
// if nodeOrderTree.sequenceOrder[fullPath] == nil {
// // fmt.Printf("create list of strings for %s\n", fullPath)
// nodeOrderTree.sequenceOrder[fullPath] = make([]string, 0)
// }
if elementName == "sequence" {
// fmt.Printf("found sequence at %s\n", fullPath)
if nodeOrderTree.sequenceOrder[fullPath] == nil {
nodeOrderTree.sequenceOrder[fullPath] = make([]string, 0)
}
} else if elementName == "element" {
nodeName = getAttrib(element.Attr, "name")
// fmt.Printf("appending %s to %s\n", nodeName, fullPath)
nodeOrderTree.sequenceOrder[fullPath] = append(nodeOrderTree.sequenceOrder[fullPath], nodeName)
// let's check if this is a complex type by any chance
_type := getAttrib(element.Attr, "type")
if _type != "" {
_type = strings.Replace(_type, "tns:", "", 1)
nodeOrderTree.nodeType[fullPath+"."+nodeName] = _type
// append to the replacement array in case the actual type is defined
// later than the field itself.
// fmt.Printf("fullPAth=%s; node=%s; type=%s\n", fullPath, nodeName, _type)
// recurseReplacement[fullPath] = _type
// recurseReplacement[fullPath+"."+nodeName] = _type
}
// localPath = append(localPath, nodeName)
}
localPath = append(localPath, nodeName)
fullPath = getFullPath(localPath)
// fmt.Printf("currentPath = %s\n", strings.Join(localPath, "."))
case xml.EndElement:
localPath = localPath[0 : len(localPath)-1]
fullPath = getFullPath(localPath)
// fmt.Printf("currentPath = %s\n", strings.Join(localPath, "."))
default:
}
}
// fmt.Printf("complex types: %v\n", complexTypes)
// for name, _ := range complexTypes {
// fmt.Printf("order for type: %s = %v\n", name, sequenceOrder[name])
// }
// fmt.Printf("node types:\n")
// for name, _type := range nodeType {
// fmt.Printf("%s = %v\n", name, _type)
// }
// // check replacement sequences, if any:
// func foo(path string, _type string) {
// fields, exists := sequenceOrder[_type]
// if !exists {
// return
// }
// for field := range fields {
// foo(path + "." + field)
// }
// }
nodeOrderTree.recurseResolveNodeOrder()
// for path, _type := range recurseReplacement {
// if _, exists := sequenceOrder[_type]; !exists {
// continue
// }
// fmt.Printf("recursing trough %s (%s)\n", path, _type)
// // foo(path, _type)
// }
// // fmt.Printf("replace sequences: %+v\n", replaceSequences["AdresPol"])
// for path, _type := range replaceSequences {
// if sequenceOrder[_type] != nil {
// sequenceOrder[path] = sequenceOrder[_type]
// }
// }
return nodeOrderTree.sequenceOrder
}
func getAttrib(attribs []xml.Attr, name string) string {
for _, attr := range attribs {
if attr.Name.Local == name {
return attr.Value
}
}
return ""
}
func getFullPath(path []string) string {
var ignoredNodeNames = map[string]bool{
"schema": true,
"complexType": true,
"sequence": true,
"element": true,
"enumeration": true,
"annotation": true,
"choice": true,
"restriction": true,
"documentation": true,
}
var fullPath = strings.Join(path, ".")
for _, name := range path {
if _, exists := ignoredNodeNames[name]; exists {
fullPath = strings.ReplaceAll(fullPath, name+".", "")
fullPath = strings.ReplaceAll(fullPath, "."+name, "")
}
}
return fullPath
}
func main() {
var err error
files, err := os.ReadDir("schemas")
if err != nil {
log.Fatal(err)
}
var fileNameBase string
for _, file := range files {
if strings.HasSuffix(file.Name(), ".xsd") {
fileNameBase = strings.ToLower(strings.Replace(file.Name(), ".xsd", "", 1))
if err = parseSchema("schemas/"+file.Name(), "internal/sei/generators/"+fileNameBase+"/schema_ordering.go"); err != nil {
log.Fatal(err)
}
}
}
}