-
Notifications
You must be signed in to change notification settings - Fork 35
/
chidley.go
488 lines (414 loc) · 13.7 KB
/
chidley.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package main
// Copyright 2014,2015,2016 Glen Newton
// glen.newton@gmail.com
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
"text/template"
"time"
)
func init() {
flag.BoolVar(&DEBUG, "d", DEBUG, "Debug; prints out much information")
flag.BoolVar(&addDbMetadata, "B", addDbMetadata, "Add database metadata to created Go structs")
flag.BoolVar(&classicStructNamesWithUnderscores, "C", classicStructNamesWithUnderscores, "Structs have underscores instead of CamelCase; how chidley used to produce output; includes name spaces (see -n)")
flag.BoolVar(&codeGenConvert, "W", codeGenConvert, "Generate Go code to convert XML to JSON or XML (latter useful for validation) and write it to stdout")
flag.BoolVar(&flattenStrings, "F", flattenStrings, "Assume complete representative XML and collapse tags with only a single string and no attributes")
flag.BoolVar(&ignoreXmlDecodingErrors, "I", ignoreXmlDecodingErrors, "If XML decoding error encountered, continue")
flag.BoolVar(&nameSpaceInJsonName, "n", nameSpaceInJsonName, "Use the XML namespace prefix as prefix to JSON name")
flag.BoolVar(&prettyPrint, "p", prettyPrint, "Pretty-print json in generated code (if applicable)")
flag.BoolVar(&progress, "r", progress, "Progress: every 50000 input tags (elements)")
flag.BoolVar(&readFromStandardIn, "c", readFromStandardIn, "Read XML from standard input")
flag.BoolVar(&sortByXmlOrder, "X", sortByXmlOrder, "Sort output of structs in Go code by order encounered in source XML (default is alphabetical order)")
flag.BoolVar(&structsToStdout, "G", structsToStdout, "Only write generated Go structs to stdout")
flag.BoolVar(&url, "u", url, "Filename interpreted as an URL")
flag.BoolVar(&useType, "t", useType, "Use type info obtained from XML (int, bool, etc); default is to assume everything is a string; better chance at working if XMl sample is not complete")
flag.BoolVar(&writeJava, "J", writeJava, "Generated Java code for Java/JAXB")
flag.BoolVar(&xmlName, "x", xmlName, "Add XMLName (Space, Local) for each XML element, to JSON")
flag.BoolVar(&keepXmlFirstLetterCase, "K", keepXmlFirstLetterCase, "Do not change the case of the first letter of the XML tag names")
flag.BoolVar(&validateFieldTemplate, "m", validateFieldTemplate, "Validate the field template. Useful to make sure the template defined with -T is valid")
flag.BoolVar(&ignoreLowerCaseXmlTags, "L", ignoreLowerCaseXmlTags, "Ignore lower case XML tags")
flag.StringVar(&attributePrefix, "a", attributePrefix, "Prefix to attribute names")
flag.StringVar(&baseJavaDir, "D", baseJavaDir, "Base directory for generated Java code (root of maven project)")
flag.StringVar(&cdataStringName, "M", cdataStringName, "Set name of CDATA string field")
flag.StringVar(&fieldTemplateString, "T", fieldTemplateString, "Field template for the struct field definition. Can include annotations. Default is for XML and JSON")
flag.StringVar(&javaAppName, "k", javaAppName, "App name for Java code (appended to ca.gnewton.chidley Java package name))")
flag.StringVar(&lengthTagAttribute, "A", lengthTagAttribute, "The tag name attribute to use for the max length Go annotations")
flag.StringVar(&lengthTagName, "N", lengthTagName, "The tag name to use for the max length Go annotations")
flag.StringVar(&lengthTagSeparator, "S", lengthTagSeparator, "The tag name separator to use for the max length Go annotations")
flag.StringVar(&namePrefix, "e", namePrefix, "Prefix to struct (element) names; must start with a capital")
flag.StringVar(&userJavaPackageName, "P", userJavaPackageName, "Java package name (rightmost in full package name")
flag.StringVar(&ignoredXmlTags, "h", ignoredXmlTags, "List of XML tags to ignore; comma separated")
flag.Int64Var(&lengthTagPadding, "Z", lengthTagPadding, "The padding on the max length tag attribute")
}
func handleParameters() error {
flag.Parse()
if codeGenConvert || writeJava {
structsToStdout = false
}
numBoolsSet := countNumberOfBoolsSet(outputs)
if numBoolsSet > 1 {
log.Print(" ERROR: Only one of -W -J -X -V -c can be set")
} else if numBoolsSet == 0 {
log.Print(" ERROR: At least one of -W -J -X -V -c must be set")
}
if sortByXmlOrder {
structSort = printStructsByXml
}
var err error
ignoredXmlTagsMap, err = extractExcludedTags(ignoredXmlTags)
if err != nil {
return err
}
if lengthTagName == "" && lengthTagAttribute == "" || lengthTagName != "" && lengthTagAttribute != "" {
return nil
}
return errors.New("Both lengthTagName and lengthTagAttribute must be set")
}
func main() {
//log.Println(fieldTemplateString)
//EXP()
log.SetFlags(log.LstdFlags | log.Lshortfile)
err := handleParameters()
if err != nil {
log.Println(err)
flag.Usage()
return
}
err = runValidateFieldTemplate(validateFieldTemplate)
if err != nil {
return
}
if validateFieldTemplate {
return
}
if len(flag.Args()) == 0 && !readFromStandardIn {
fmt.Println("chidley <flags> xmlFileName|url")
fmt.Println("xmlFileName can be .gz or .bz2: uncompressed transparently")
flag.Usage()
return
}
var sourceNames []string
if !readFromStandardIn {
sourceNames = flag.Args()
}
if !url && !readFromStandardIn {
for i, _ := range sourceNames {
sourceNames[i], err = filepath.Abs(sourceNames[i])
if err != nil {
log.Fatal("FATAL ERROR: " + err.Error())
}
}
}
sources, err := makeSourceReaders(sourceNames, url, readFromStandardIn)
if err != nil {
log.Fatal("FATAL ERROR: " + err.Error())
}
ex := Extractor{
namePrefix: namePrefix,
nameSuffix: nameSuffix,
useType: useType,
progress: progress,
ignoreXmlDecodingErrors: ignoreXmlDecodingErrors,
initted: false,
}
if DEBUG {
log.Print("extracting")
}
m := &ex
m.init()
for source := range sources {
if DEBUG {
log.Println("READER", source)
}
err = m.extract(source.getReader())
if err != nil {
log.Println("ERROR: " + err.Error())
if !ignoreXmlDecodingErrors {
log.Fatal("FATAL ERROR: " + err.Error())
}
}
if DEBUG {
log.Println("DONE READER", source)
}
}
ex.done()
switch {
case codeGenConvert:
generateGoCode(os.Stdout, sourceNames, &ex)
case structsToStdout:
generateGoStructs(os.Stdout, sourceNames[0], &ex)
case writeJava:
if len(userJavaPackageName) > 0 {
javaAppName = userJavaPackageName
}
javaPackage := javaBasePackage + "." + javaAppName
javaDir := baseJavaDir + "/" + mavenJavaBase + "/" + javaBasePackagePath + "/" + javaAppName
os.RemoveAll(baseJavaDir)
os.MkdirAll(javaDir+"/xml", 0755)
date := time.Now()
printJavaJaxbVisitor := PrintJavaJaxbVisitor{
alreadyVisited: make(map[string]bool),
globalTagAttributes: ex.globalTagAttributes,
nameSpaceTagMap: ex.nameSpaceTagMap,
useType: useType,
javaDir: javaDir,
javaPackage: javaPackage,
namePrefix: namePrefix,
Date: date,
}
var onlyChild *Node
for _, child := range ex.root.children {
printJavaJaxbVisitor.Visit(child)
// Bad: assume only one base element
onlyChild = child
}
fullPath, err := getFullPath(sourceNames[0])
if err != nil {
log.Fatal(err)
}
printJavaJaxbMain(onlyChild.makeJavaType(namePrefix, ""), javaDir, javaPackage, fullPath, date)
printPackageInfo(onlyChild, javaDir, javaPackage, ex.globalTagAttributes, ex.nameSpaceTagMap)
printMavenPom(baseJavaDir+"/pom.xml", javaAppName)
}
}
func printPackageInfo(node *Node, javaDir string, javaPackage string, globalTagAttributes map[string][]*FQN, nameSpaceTagMap map[string]string) {
//log.Printf("%+v\n", node)
if node.space != "" {
_ = findNameSpaces(globalTagAttributes[nk(node)])
//attributes := findNameSpaces(globalTagAttributes[nk(node)])
t := template.Must(template.New("package-info").Parse(jaxbPackageInfoTemplage))
packageInfoPath := javaDir + "/xml/package-info.java"
fi, err := os.Create(packageInfoPath)
if err != nil {
log.Print("Problem creating file: " + packageInfoPath)
panic(err)
}
defer fi.Close()
writer := bufio.NewWriter(fi)
packageInfo := JaxbPackageInfo{
BaseNameSpace: node.space,
//AdditionalNameSpace []*FQN
PackageName: javaPackage + ".xml",
}
err = t.Execute(writer, packageInfo)
if err != nil {
log.Println("executing template:", err)
}
bufio.NewWriter(writer).Flush()
}
}
const XMLNS = "xmlns"
func findNameSpaces(attributes []*FQN) []*FQN {
if attributes == nil || len(attributes) == 0 {
return nil
}
xmlns := make([]*FQN, 0)
return xmlns
}
func printMavenPom(pomPath string, javaAppName string) {
t := template.Must(template.New("mavenPom").Parse(mavenPomTemplate))
fi, err := os.Create(pomPath)
if err != nil {
log.Print("Problem creating file: " + pomPath)
panic(err)
}
defer fi.Close()
writer := bufio.NewWriter(fi)
maven := JaxbMavenPomInfo{
AppName: javaAppName,
}
err = t.Execute(writer, maven)
if err != nil {
log.Println("executing template:", err)
}
bufio.NewWriter(writer).Flush()
}
func printJavaJaxbMain(rootElementName string, javaDir string, javaPackage string, sourceXMLFilename string, date time.Time) {
t := template.Must(template.New("chidleyJaxbGenClass").Parse(jaxbMainTemplate))
writer, f, err := javaClassWriter(javaDir, javaPackage, "Main")
defer f.Close()
classInfo := JaxbMainClassInfo{
PackageName: javaPackage,
BaseXMLClassName: rootElementName,
SourceXMLFilename: sourceXMLFilename,
Date: date,
}
err = t.Execute(writer, classInfo)
if err != nil {
log.Println("executing template:", err)
}
bufio.NewWriter(writer).Flush()
}
//func makeSourceReaders(sourceNames []string, url bool, standardIn bool) ([]Source, error) {
func makeSourceReaders(sourceNames []string, url bool, standardIn bool) (chan Source, error) {
var err error
//sources := make([]Source, len(sourceNames))
sources := make(chan Source, 1)
go func() {
var newSource Source
for i, _ := range sourceNames {
if url {
newSource = new(UrlSource)
if DEBUG {
log.Print("Making UrlSource")
}
} else {
if standardIn {
newSource = new(StdinSource)
if DEBUG {
log.Print("Making StdinSource")
}
} else {
newSource = new(FileSource)
if DEBUG {
//log.Print("Making FileSource")
}
}
}
err = newSource.newSource(sourceNames[i])
if err != nil {
log.Fatal(err)
}
sources <- newSource
if DEBUG {
log.Print("Making Source:[" + sourceNames[i] + "]")
}
}
close(sources)
}()
return sources, err
}
func attributes(atts map[string]bool) string {
ret := ": "
for k, _ := range atts {
ret = ret + k + ", "
}
return ret
}
func indent(d int) string {
indent := ""
for i := 0; i < d; i++ {
indent = indent + "\t"
}
return indent
}
func countNumberOfBoolsSet(a []*bool) int {
counter := 0
for i := 0; i < len(a); i++ {
if *a[i] {
counter += 1
}
}
return counter
}
func makeOneLevelDown(node *Node, globalTagAttributes map[string]([]*FQN)) []*XMLType {
var children []*XMLType
for _, np := range node.children {
if np == nil {
continue
}
for _, n := range np.children {
if n == nil {
continue
}
if flattenStrings && isStringOnlyField(n, len(globalTagAttributes[nk(n)])) {
continue
}
x := XMLType{NameType: n.makeType(namePrefix, nameSuffix),
XMLName: n.name,
XMLNameUpper: capitalizeFirstLetter(n.name),
XMLSpace: n.space}
children = append(children, &x)
}
}
return children
}
func printChildrenChildren(node *Node) {
for k, v := range node.children {
log.Print(k)
log.Printf("children: %+v\n", v.children)
}
}
// Order Xml is encountered
func printStructsByXml(v *PrintGoStructVisitor) error {
orderNodes := make(map[int]*Node)
var order []int
for k := range v.alreadyVisitedNodes {
nodeOrder := v.alreadyVisitedNodes[k].discoveredOrder
orderNodes[nodeOrder] = v.alreadyVisitedNodes[k]
order = append(order, nodeOrder)
}
sort.Ints(order)
for o := range order {
err := print(v, orderNodes[o])
if err != nil {
return err
}
}
return nil
}
// Alphabetical order
func printStructsAlphabetical(v *PrintGoStructVisitor) error {
var keys []string
for k := range v.alreadyVisitedNodes {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
err := print(v, v.alreadyVisitedNodes[k])
if err != nil {
return err
}
}
return nil
}
func generateGoStructs(out io.Writer, sourceName string, ex *Extractor) {
printGoStructVisitor := new(PrintGoStructVisitor)
printGoStructVisitor.init(os.Stdout, 999, ex.globalTagAttributes, ex.nameSpaceTagMap, useType, nameSpaceInJsonName)
printGoStructVisitor.Visit(ex.root)
structSort(printGoStructVisitor)
}
//Writes structs to a string then uses this in a template to generate Go code
func generateGoCode(out io.Writer, sourceNames []string, ex *Extractor) error {
buf := bytes.NewBufferString("")
printGoStructVisitor := new(PrintGoStructVisitor)
printGoStructVisitor.init(buf, 9999, ex.globalTagAttributes, ex.nameSpaceTagMap, useType, nameSpaceInJsonName)
printGoStructVisitor.Visit(ex.root)
structSort(printGoStructVisitor)
xt := XMLType{NameType: ex.firstNode.makeType(namePrefix, nameSuffix),
XMLName: ex.firstNode.name,
XMLNameUpper: capitalizeFirstLetter(ex.firstNode.name),
XMLSpace: ex.firstNode.space,
}
fullPath, err := getFullPath(sourceNames[0])
if err != nil {
return err
}
fullPaths, err := getFullPaths(sourceNames)
if err != nil {
return err
}
x := XmlInfo{
BaseXML: &xt,
OneLevelDownXML: makeOneLevelDown(ex.root, ex.globalTagAttributes),
Filenames: fullPaths,
Filename: fullPath,
Structs: buf.String(),
}
x.init()
t := template.Must(template.New("chidleyGen").Parse(codeTemplate))
err = t.Execute(out, x)
if err != nil {
log.Println("executing template:", err)
return err
}
return err
}