-
Notifications
You must be signed in to change notification settings - Fork 35
/
extractor.go
280 lines (244 loc) · 6.7 KB
/
extractor.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
package main
import (
"encoding/xml"
"errors"
"io"
"log"
"strconv"
"strings"
)
const XML_NAMESPACE_ACRONYM = "xmlns"
var nameMapper = map[string]string{
"-": "Hyphen",
".": "Dot",
}
var DiscoveredOrder = 0
type Extractor struct {
globalTagAttributes map[string]([]*FQN)
globalTagAttributesMap map[string]bool
globalNodeMap map[string]*Node
namePrefix string
nameSpaceTagMap map[string]string
nameSuffix string
root *Node
firstNode *Node
hasStartElements bool
useType bool
progress bool
ignoreXmlDecodingErrors bool
initted bool
tokenChannel chan xml.Token
handleTokensDoneChannel chan bool
}
const RootName = "ChidleyRoot314159"
func (ex *Extractor) init() {
ex.globalTagAttributes = make(map[string]([]*FQN))
ex.globalTagAttributesMap = make(map[string]bool)
ex.nameSpaceTagMap = make(map[string]string)
ex.globalNodeMap = make(map[string]*Node)
ex.root = new(Node)
//ex.root.initialize(RootName, "", "", nil)
ex.root.initialize("", "", "", nil)
ex.hasStartElements = false
ex.initted = true
ex.tokenChannel = make(chan xml.Token, 100)
ex.handleTokensDoneChannel = make(chan bool)
go handleTokens(ex)
}
func (ex *Extractor) done() {
close(ex.tokenChannel)
_ = <-ex.handleTokensDoneChannel
}
func (ex *Extractor) extract(reader io.Reader) error {
if ex.initted == false {
return errors.New("extractor not properly initted: must run extractor.init() first")
}
decoder := xml.NewDecoder(reader)
for {
token, err := decoder.Token()
if err != nil {
if err.Error() == "EOF" {
// OK
break
}
log.Println(err)
if !ex.ignoreXmlDecodingErrors {
return err
}
}
if token == nil {
log.Println("Empty token")
break
}
ex.tokenChannel <- xml.CopyToken(token)
}
return nil
}
func handleTokens(ex *Extractor) {
tChannel := ex.tokenChannel
handleTokensDoneChannel := ex.handleTokensDoneChannel
depth := 0
thisNode := ex.root
first := true
var progressCounter int64 = 0
for token := range tChannel {
//log.Println(token)
switch element := token.(type) {
case xml.Comment:
if DEBUG {
//log.Print(thisNode.name)
//log.Printf("Comment: %+v\n", string(element))
}
case xml.ProcInst:
if DEBUG {
//log.Println("ProcInst: Target=" + element.Target + " Inst=[" + string(element.Inst) + "]")
}
case xml.Directive:
if DEBUG {
//log.Printf("Directive: %+v\n", string(element))
}
case xml.StartElement:
progressCounter += 1
if DEBUG {
//log.Printf("StartElement: %+v\n", element)
}
ex.hasStartElements = true
if element.Name.Local == "" {
continue
}
thisNode = ex.handleStartElement(element, thisNode)
thisNode.tempCharData = ""
thisNode.ignoredTag = isIgnoredTag(element.Name.Local)
if first {
first = false
ex.firstNode = thisNode
}
depth += 1
if progress {
if progressCounter%50000 == 0 {
log.Print(progressCounter)
}
}
case xml.CharData:
if DEBUG {
//log.Print(thisNode.name)
//log.Printf("CharData: [%+v]\n", string(element))
}
//if !thisNode.hasCharData {
charData := string(element)
thisNode.tempCharData += charData //strings.TrimSpace(charData)
thisNode.charDataCount += int64(len(charData))
//}
case xml.EndElement:
//if ignoredTag(element.Name.Local) {
//continue
//}
thisNode.nodeTypeInfo.checkFieldType(thisNode.tempCharData)
thisNode.nodeTypeInfo.addFieldLength(thisNode.charDataCount)
thisNode.charDataCount = 0
if DEBUG {
//log.Printf("EndElement: %+v\n", element)
//log.Printf("[[" + thisNode.tempCharData + "]]")
//log.Println("Char is empty: ", isJustSpacesAndLinefeeds(thisNode.tempCharData))
}
if !thisNode.hasCharData && !isJustSpacesAndLinefeeds(thisNode.tempCharData) {
thisNode.hasCharData = true
} else {
}
thisNode.tempCharData = ""
depth -= 1
for key, c := range thisNode.childCount {
if c > 1 {
thisNode.children[key].repeats = true
}
thisNode.childCount[key] = 0
}
if thisNode.peekParent() != nil {
thisNode = thisNode.popParent()
}
}
}
handleTokensDoneChannel <- true
close(handleTokensDoneChannel)
}
func space(n int) string {
s := strconv.Itoa(n) + "Space"
for i := 0; i < n; i++ {
s += " "
}
return s
}
func (ex *Extractor) findNewNameSpaces(attrs []xml.Attr) {
for _, attr := range attrs {
if strings.HasPrefix(attr.Name.Space, XML_NAMESPACE_ACRONYM) {
//log.Println("mmmmmmmmmmmmmmmmmmmmmmm", attr)
//log.Println("+++++++++++++++++++++++++++", attr.Value, "|", attr.Name.Local, "|", attr.Name.Space)
ex.nameSpaceTagMap[attr.Value] = attr.Name.Local
}
}
}
var full struct{}
func (ex *Extractor) handleStartElement(startElement xml.StartElement, thisNode *Node) *Node {
name := startElement.Name.Local
space := startElement.Name.Space
ex.findNewNameSpaces(startElement.Attr)
var child *Node
var attributes []*FQN
key := nks(space, name)
child, ok := thisNode.children[key]
// Does thisNode node already exist as child
//fmt.Println(space, name)
if ok {
thisNode.childCount[key] += 1
attributes, ok = ex.globalTagAttributes[key]
} else {
// if thisNode node does not already exist as child, it may still exist as child on other node:
child, ok = ex.globalNodeMap[key]
if !ok {
child = new(Node)
DiscoveredOrder += 1
child.discoveredOrder = DiscoveredOrder
ex.globalNodeMap[key] = child
spaceTag, _ := ex.nameSpaceTagMap[space]
child.initialize(name, space, spaceTag, thisNode)
thisNode.childCount[key] = 1
attributes = make([]*FQN, 0, 2)
ex.globalTagAttributes[key] = attributes
} else {
attributes = ex.globalTagAttributes[key]
}
thisNode.children[key] = child
}
child.pushParent(thisNode)
// Extract attributes
for _, attr := range startElement.Attr {
bigKey := key + "_" + attr.Name.Space + "_" + attr.Name.Local
_, ok := ex.globalTagAttributesMap[bigKey]
if ok {
fqn := findThisAttribute(attr.Name.Local, attr.Name.Space, ex.globalTagAttributes[key])
if fqn == nil {
log.Println("This should not be happening: fqn is nil")
continue
}
lenValue := len(attr.Value)
if lenValue > fqn.maxLength {
fqn.maxLength = lenValue
}
} else {
fqn := new(FQN)
fqn.name = attr.Name.Local
fqn.space = attr.Name.Space
fqn.maxLength = len(attr.Value)
//log.Println(name, "|", fqn.name, "||", fqn.space)
attributes = append(attributes, fqn)
ex.globalTagAttributesMap[bigKey] = true
}
}
ex.globalTagAttributes[key] = attributes
return child
}
func isJustSpacesAndLinefeeds(s string) bool {
s = strings.Replace(s, "\\n", "", -1)
s = strings.Replace(s, "\n", "", -1)
return len(strings.TrimSpace(s)) == 0
}