-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathgenJava.go
270 lines (247 loc) · 10.1 KB
/
genJava.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
// Copyright 2020 - 2024 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 (
"fmt"
"os"
"reflect"
"strings"
)
var javaBuildInType = map[string]bool{
"Boolean": true,
"Byte": true,
"Character": true,
"List<String>": true,
"List<Byte>": true,
"Float": true,
"Integer": true,
"Short": true,
"String": true,
"QName": true,
"Long": true,
}
// GenJava generate Java programming language source code for XML schema
// definition files.
func (gen *CodeGenerator) GenJava() error {
fieldNameCount = make(map[string]int)
for _, ele := range gen.ProtoTree {
if ele == nil {
continue
}
funcName := fmt.Sprintf("Java%s", reflect.TypeOf(ele).String()[6:])
callFuncByName(gen, funcName, []reflect.Value{reflect.ValueOf(ele)})
}
f, err := os.Create(gen.FileWithExtension(".java"))
if err != nil {
return err
}
defer f.Close()
packageName := gen.Package
if packageName == "" {
packageName = "schema"
}
var importPackage = `import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;`
f.Write([]byte(fmt.Sprintf("%s\n\npackage %s;\n\n%s\n%s", copyright, packageName, importPackage, gen.Field)))
return err
}
func genJavaFieldName(name string, unique bool) (fieldName string) {
for _, str := range strings.Split(name, ":") {
fieldName += MakeFirstUpperCase(str)
}
var tmp string
for _, str := range strings.Split(fieldName, ".") {
tmp += MakeFirstUpperCase(str)
}
fieldName = tmp
fieldName = strings.Replace(fieldName, "-", "", -1)
if unique {
fieldNameCount[fieldName]++
if count := fieldNameCount[fieldName]; count != 1 {
fieldName = fmt.Sprintf("%s%d", fieldName, count)
}
}
return
}
func genJavaFieldType(name string) string {
if _, ok := javaBuildInType[name]; ok {
return name
}
var fieldType string
for _, str := range strings.Split(name, ".") {
fieldType += MakeFirstUpperCase(str)
}
fieldType = MakeFirstUpperCase(strings.Replace(fieldType, "-", "", -1))
if fieldType != "" {
return fieldType
}
return "void"
}
// JavaSimpleType generates code for simple type XML schema in Java language
// syntax.
func (gen *CodeGenerator) JavaSimpleType(v *SimpleType) {
if v.List {
if _, ok := gen.StructAST[v.Name]; !ok {
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree))
content := fmt.Sprintf("\tprotected List<%s> %s;\n", fieldType, genJavaFieldName(v.Name, false))
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("\n@XmlAccessorType(XmlAccessType.FIELD)\n@XmlAttribute(required = true, name = \"%s\")\npublic class %s {\n%s}\n", v.Name, genJavaFieldName(v.Name, true), gen.StructAST[v.Name])
return
}
}
if v.Union && len(v.MemberTypes) > 0 {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, member := range toSortedPairs(v.MemberTypes) {
memberName := member.key
memberType := member.value
if memberType == "" { // fix order issue
memberType = getBasefromSimpleType(memberName, gen.ProtoTree)
}
fieldType := genJavaFieldType(memberType)
content += fmt.Sprintf("\t@XmlElement(required = true)\n\tprotected %s %s;\n", fieldType, genJavaFieldName(memberName, false))
}
content += "}\n"
gen.StructAST[v.Name] = content
fieldName := genJavaFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%spublic class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
return
}
if _, ok := gen.StructAST[v.Name]; !ok {
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree))
content := fmt.Sprintf("\tprotected %s %s;\n", fieldType, genJavaFieldName(v.Name, false))
gen.StructAST[v.Name] = content
fieldName := genJavaFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%s@XmlAccessorType(XmlAccessType.FIELD)\n@XmlAttribute(required = true, name = \"%s\")\npublic class %s {\n%s}\n", genFieldComment(fieldName, v.Doc, "//"), v.Name, fieldName, gen.StructAST[v.Name])
}
}
// JavaComplexType generates code for complex type XML schema in Java language
// syntax.
func (gen *CodeGenerator) JavaComplexType(v *ComplexType) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, attrGroup := range v.AttributeGroup {
fieldType := getBasefromSimpleType(trimNSPrefix(attrGroup.Ref), gen.ProtoTree)
content += fmt.Sprintf("\t@XmlElement(required = true)\n\tprotected %s %s;\n", genJavaFieldType(fieldType), genJavaFieldName(attrGroup.Name, false))
}
for _, attribute := range v.Attributes {
var required = ", required = true"
if attribute.Optional {
required = ""
}
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
content += fmt.Sprintf("\t@XmlAttribute(name = \"%s\"%s)\n\tprotected %s %sAttr;\n", attribute.Name, required, fieldType, genJavaFieldName(attribute.Name, false))
}
for _, group := range v.Groups {
var fieldType = genJavaFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree))
if group.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content += fmt.Sprintf("\tprotected %s %s;\n", fieldType, genJavaFieldName(group.Name, false))
}
for _, element := range v.Elements {
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree))
if element.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content += fmt.Sprintf("\t@XmlElement(required = true, name = \"%s\")\n\tprotected %s %s;\n", element.Name, fieldType, genJavaFieldName(element.Name, false))
}
if len(v.Base) > 0 && isBuiltInJavaType(v.Base) {
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree))
content += fmt.Sprintf("\t@XmlValue\n\tprotected %s value;\n", fieldType)
}
content += "}\n"
gen.StructAST[v.Name] = content
fieldName := genJavaFieldName(v.Name, true)
typeExtension := ""
if len(v.Base) > 0 && !isBuiltInJavaType(v.Base) {
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Base), gen.ProtoTree))
typeExtension = fmt.Sprintf(" extends %s ", fieldType)
}
gen.Field += fmt.Sprintf("%spublic class %s%s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, typeExtension, gen.StructAST[v.Name])
}
}
func isBuiltInJavaType(typeName string) bool {
_, builtIn := javaBuildInType[typeName]
return builtIn
}
// JavaGroup generates code for group XML schema in Java language syntax.
func (gen *CodeGenerator) JavaGroup(v *Group) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, element := range v.Elements {
var fieldType = genJavaFieldType(getBasefromSimpleType(trimNSPrefix(element.Type), gen.ProtoTree))
if element.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content += fmt.Sprintf("\t@XmlElement(required = true, name = \"%s\")\n\tprotected %s %s;\n", element.Name, fieldType, genJavaFieldName(element.Name, false))
}
for _, group := range v.Groups {
var fieldType = genJavaFieldType(getBasefromSimpleType(trimNSPrefix(group.Ref), gen.ProtoTree))
if group.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content += fmt.Sprintf("\tprotected %s %s;\n", fieldType, genJavaFieldName(group.Name, false))
}
content += "}\n"
gen.StructAST[v.Name] = content
fieldName := genJavaFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%spublic class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// JavaAttributeGroup generates code for attribute group XML schema in Java language
// syntax.
func (gen *CodeGenerator) JavaAttributeGroup(v *AttributeGroup) {
if _, ok := gen.StructAST[v.Name]; !ok {
content := " {\n"
for _, attribute := range v.Attributes {
var required = ", required = true"
if attribute.Optional {
required = ""
}
fieldType := genJavaFieldType(getBasefromSimpleType(trimNSPrefix(attribute.Type), gen.ProtoTree))
content += fmt.Sprintf("\t@XmlAttribute(name = \"%s\"%s)\n\tprotected %sAttr %s;\n", attribute.Name, required, fieldType, genJavaFieldName(attribute.Name, false))
}
content += "}\n"
gen.StructAST[v.Name] = content
fieldName := genJavaFieldName(v.Name, true)
gen.Field += fmt.Sprintf("%spublic class %s%s", genFieldComment(fieldName, v.Doc, "//"), fieldName, gen.StructAST[v.Name])
}
}
// JavaElement generates code for element XML schema in Java language syntax.
func (gen *CodeGenerator) JavaElement(v *Element) {
if _, ok := gen.StructAST[v.Name]; !ok {
var fieldType = genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree))
if v.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content := fmt.Sprintf("\tprotected %s %s;\n", fieldType, genJavaFieldName(v.Name, false))
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("\n@XmlAccessorType(XmlAccessType.FIELD)\n@XmlElement(required = true, name = \"%s\")\npublic class %s {\n%s}\n", v.Name, genJavaFieldName(v.Name, true), gen.StructAST[v.Name])
}
}
// JavaAttribute generates code for attribute XML schema in Java language syntax.
func (gen *CodeGenerator) JavaAttribute(v *Attribute) {
if _, ok := gen.StructAST[v.Name]; !ok {
var fieldType = genJavaFieldType(getBasefromSimpleType(trimNSPrefix(v.Type), gen.ProtoTree))
if v.Plural {
fieldType = fmt.Sprintf("List<%s>", fieldType)
}
content := fmt.Sprintf("\tprotected %s %s;\n", fieldType, genJavaFieldName(v.Name, false))
gen.StructAST[v.Name] = content
gen.Field += fmt.Sprintf("\n@XmlAccessorType(XmlAccessType.FIELD)\n@XmlAttribute(required = true, name = \"%s\")\npublic class %s {\n%s}\n", v.Name, genJavaFieldName(v.Name, true), gen.StructAST[v.Name])
}
}