-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutil.go
306 lines (255 loc) · 6.39 KB
/
util.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
package main
import (
"bufio"
"compress/bzip2"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"os"
"sort"
//"strconv"
"strings"
"unicode"
"github.com/xi2/xz"
)
func genericReader(filename string) (io.Reader, *os.File, error) {
file, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
if strings.HasSuffix(filename, "xz") {
reader, err := xz.NewReader(bufio.NewReader(file), 0)
if err != nil {
return nil, nil, err
}
return bufio.NewReader(reader), file, err
}
if strings.HasSuffix(filename, "bz2") {
return bufio.NewReader(bzip2.NewReader(bufio.NewReader(file))), file, err
}
if strings.HasSuffix(filename, "gz") {
reader, err := gzip.NewReader(bufio.NewReader(file))
if err != nil {
return nil, nil, err
}
return bufio.NewReader(reader), file, err
}
return bufio.NewReader(file), file, err
}
func cleanName(name string) string {
for old, new := range nameMapper {
parts := strings.Split(name, old)
if len(parts) == 1 {
continue
} else {
name := ""
l := len(parts)
for i := 0; i < l; i++ {
if i == 0 {
name += parts[i]
} else {
name += capitalizeFirstLetter(parts[i])
}
if i+1 < l {
name += new
}
}
}
}
//return capitalizeFirstLetter(name)
return name
}
const BoolType = "bool"
const StringType = "string"
const IntType = "int"
const Int8Type = "int8"
const Int16Type = "int16"
const Int32Type = "int32"
const Int64Type = "int64"
const Float32Type = "float32"
const Float64Type = "float64"
func findType(nti *NodeTypeInfo, useType bool) string {
if !useType {
return StringType
}
if nti.alwaysBool {
return BoolType
}
if nti.alwaysInt08 {
return Int8Type
}
if nti.alwaysInt16 {
return Int16Type
}
if nti.alwaysInt32 {
return Int32Type
}
if nti.alwaysInt64 {
return Int64Type
}
if nti.alwaysInt0 {
return IntType
}
if nti.alwaysFloat32 {
return Float32Type
}
if nti.alwaysFloat64 {
return Float64Type
}
return StringType
}
type fqnSorter []*FQN
func isStringOnlyField(n *Node, nattributes int) bool {
return (len(n.children) == 0 && nattributes == 0)
}
func makeAttributes(writer io.Writer, attributes []*FQN, nameSpaceTagMap map[string]string) {
sort.Sort(fqnSorter(attributes))
for _, fqn := range attributes {
name := fqn.name
nameSpace := fqn.space
nameSpaceTag, ok := nameSpaceTagMap[nameSpace]
if ok && nameSpaceTag != "" {
nameSpaceTag = nameSpaceTag + "Space"
} else {
nameSpaceTag = nameSpace
}
nameSpaceTag = goVariableNameSanitize(nameSpaceTag)
if len(nameSpace) > 0 {
nameSpace = nameSpace + " "
}
//variableName := attributePrefix + capitalizeFirstLetter(nameSpaceTag) + cleanName(name)
variableName := goVariableNameSanitize(attributePrefix + capitalizeFirstLetter(nameSpaceTag) + cleanName(name))
variableType := "string"
//lineChannel <- "\t" + variableName + " " + variableType + "`xml:\"" + nameSpace + name + ",attr\" json:\",omitempty\"`" + " // maxLength=" + strconv.Itoa(fqn.maxLength)
//fmt.Fprintln(writer, "\t"+variableName+" "+variableType+"`xml:\""+nameSpace+name+",attr\" json:\",omitempty\"`"+" // maxLength="+strconv.Itoa(fqn.maxLength))
fmt.Fprintln(writer, "\t"+variableName+" "+variableType+"`xml:\""+nameSpace+name+",attr\" json:\",omitempty\"`")
}
}
func goVariableNameSanitize(s string) string {
s = strings.Replace(s, ":", "_colon_", -1)
s = strings.Replace(s, "/", "_slash_", -1)
s = strings.Replace(s, ".", "_dot_", -1)
s = strings.Replace(s, "-", "_dash_", -1)
s = strings.Replace(s, " ", "_space_", -1)
s = strings.Replace(s, "-", "_dash_", -1)
return s
}
// Len is part of sort.Interface.
func (s fqnSorter) Len() int {
return len(s)
}
// Swap is part of sort.Interface.
func (s fqnSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s fqnSorter) Less(i, j int) bool {
return strings.Compare(cleanName(s[i].name), cleanName(s[j].name)) < 0
}
// node key
func nk(n *Node) string {
return nks(n.space, n.name)
}
func nks(space, name string) string {
return space + "NS" + name
}
func getFullPaths(filenames []string) ([]string, error) {
fps := make([]string, len(filenames))
var err error
for i, _ := range filenames {
fps[i], err = getFullPath(filenames[i])
if err != nil {
return nil, err
}
}
return fps, nil
}
func getFullPath(filename string) (string, error) {
if filename == "" {
return "", nil
}
file, err := os.Open(filename) // For read access.
if err != nil {
log.Print("Error opening: " + filename)
return "", err
}
return file.Name(), nil
}
type alterer func(string) string
func alterFirstLetter(s string, f alterer) string {
switch len(s) {
case 0:
return s
case 1:
v := f(s[0:1])
return v
default:
return f(s[0:1]) + s[1:]
}
}
func capitalizeFirstLetter(s string) string {
return alterFirstLetter(s, strings.ToUpper)
}
func lowerFirstLetter(s string) string {
return alterFirstLetter(s, strings.ToLower)
}
func findThisAttribute(local, nameSpace string, attrs []*FQN) *FQN {
for _, attr := range attrs {
if attr.name == local && attr.space == nameSpace {
return attr
}
}
return nil
}
func makeAttributeName(key, namespace, local string) string {
return key + "_" + local + "_" + namespace
}
func containsUnicodeSpace(s string) bool {
if s == "" {
return false
}
for _, rune := range s {
//log.Printf("*** %#U ****", rune)
if unicode.IsSpace(rune) {
return true
}
}
return false
}
func isIgnoredTag(tag string) bool {
var ignored bool
if ignoredXmlTagsMap == nil {
return false
}
_, ignored = (*ignoredXmlTagsMap)[tag]
if ignored || (ignoreLowerCaseXmlTags && tag == strings.ToLower(tag)) {
return true
}
return false
}
func extractExcludedTags(tagsString string) (*map[string]struct{}, error) {
ignoredMap := make(map[string]struct{})
if tagsString == "" {
return &ignoredMap, nil
}
tags := strings.Split(tagsString, ",")
for i, _ := range tags {
tag := strings.TrimSpace(tags[i])
if containsUnicodeSpace(tag) {
return nil, errors.New("Excluded tag contains space: [" + tag + "] in list of excluded tags:" + tagsString + "]")
}
ignoredMap[tag] = struct{}{}
}
return &ignoredMap, nil
}
func findFieldNameFromTypeInfo(t string) string {
switch t {
case IntType, Int8Type, Int16Type, Int32Type, Int64Type, Float32Type, Float64Type:
return cdataNumberName
case BoolType:
return cdataBooleanName
}
return "string"
}