-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.go
201 lines (163 loc) · 4.41 KB
/
splitter.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
package main
import (
"bufio"
"path/filepath"
"regexp"
"strings"
)
const (
defaultSkip = "(<\\?xml)|(<!DOCTYPE)"
openTagRegex = "<([a-zA-Z:_][a-zA-Z0-9:_.-]*)(\\s*>|\\s+([a-zA-Z0-9:_.-]+\\s*=\\s*(\"[^\"]*\"|'[^']*')\\s*)*>)"
emptyTagRegex = "<([a-zA-Z:_][a-zA-Z0-9:_.-]*)(\\s*/>|\\s+([a-zA-Z0-9:_.-]+\\s*=\\s*(\"[^\"]*\"|'[^']*')\\s*)*/>)"
closeTagRegex = "</\\s*([a-zA-Z:_]?[a-zA-Z0-9:_.-]*)\\s*>"
whitespaceRegex = "^\\s*$"
openTagStartRegex = "<([a-zA-Z:_][a-zA-Z0-9:_.-]*)(\\s*|\\s+([a-zA-Z0-9:_.-]+\\s*=\\s*(\"[^\"]*\"|'[^']*')\\s*)*)$"
openTagEndRegex = "^\\s*([a-zA-Z0-9:_.-]+\\s*=\\s*(\"[^\"]*\"|'[^']*')\\s*)*>"
)
var openingTag = regexp.MustCompile(openTagRegex)
var closingTag = regexp.MustCompile(closeTagRegex)
var emptyTag = regexp.MustCompile(emptyTagRegex)
var whitespace = regexp.MustCompile(whitespaceRegex)
var openTagStart = regexp.MustCompile(openTagStartRegex)
var openTagEnd = regexp.MustCompile(openTagEndRegex)
type TagType int
const (
Opening TagType = iota
Closing
Empty
)
type Tag struct {
Type TagType
Name string
Full string
Start int
End int
}
type XMLSplitter struct {
path string
conf Config
}
func (s *XMLSplitter) ProcessFile(scanner *bufio.Scanner, writer ioActionWriter) int {
var err error
// cache is used to keep track of files/folders and xml depth so we don't overwrite files.
cache := &processCache{
currentDirectory: []string{s.conf.out, filepath.Base(strings.TrimSuffix(s.path, filepath.Ext(s.path)))},
directoryCounter: make(map[string]int),
fileCounter: make(map[string]int),
}
isMultilineTag := false
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
skipMatches := s.conf.skip.FindStringSubmatch(line)
if len(skipMatches) > 0 {
continue
}
if openTagStart.MatchString(line) {
isMultilineTag = true
cache.line = line
continue
}
if openTagEnd.MatchString(line) {
line = cache.line + line
cache.line = ""
isMultilineTag = false
}
if isMultilineTag {
cache.line += " " + line
continue
}
if s.conf.strip.String() != "" {
line = s.conf.strip.ReplaceAllString(line, "")
}
s.processLine(line, cache)
if len(cache.ioActions) > s.conf.buffer {
cache.ioActions, err = writer.write(cache.ioActions)
handleError(err)
}
}
cache.ioActions, err = writer.write(cache.ioActions)
handleError(err)
return cache.totalFiles
}
func (s *XMLSplitter) processLine(line string, cache *processCache) {
lineStructure := s.getLineStructure(line)
for i := 0; i < len(line); {
if tag, ok := lineStructure[i]; ok {
if cache.file && !whitespace.MatchString(cache.innerText) {
cache.appendLine(strings.TrimSpace(cache.innerText))
cache.innerText = ""
}
switch tag.Type {
case Opening:
if cache.depth < s.conf.depth {
cache.newDirectory(tag.Name)
cache.appendFile("root", tag.Full[:len(tag.Full)-1]+"/>")
} else if !cache.file {
cache.openFile(tag.Name)
cache.appendLine(tag.Full)
} else {
cache.appendLine(tag.Full)
}
cache.depth++
case Closing:
if cache.depth > s.conf.depth+1 {
cache.appendLine(tag.Full)
} else if cache.depth == s.conf.depth+1 {
cache.appendLine(tag.Full)
cache.closeFile()
} else if tag.Name == cache.currentDirectory[len(cache.currentDirectory)-2] && cache.depth <= s.conf.depth {
cache.exitDirectory()
}
cache.depth--
case Empty:
if cache.depth < s.conf.depth {
cache.newDirectory(tag.Name)
cache.appendFile("root", tag.Full)
cache.exitDirectory()
} else if !cache.file {
cache.openFile(tag.Name)
cache.appendLine(tag.Full)
cache.closeFile()
} else {
cache.appendLine(tag.Full)
}
}
i = tag.End
} else {
if cache.file {
cache.innerText += string(line[i])
}
i++
if cache.file && i == len(line) {
cache.innerText += "\n"
}
}
}
}
func (s *XMLSplitter) getLineStructure(line string) map[int]Tag {
lineStructure := make(map[int]Tag)
args := []struct{
regex *regexp.Regexp
tagType TagType
}{
{openingTag, Opening},
{closingTag, Closing},
{emptyTag, Empty},
}
for _, arg := range args {
tags := arg.regex.FindAllStringSubmatchIndex(line, -1)
for _, tag := range tags {
lineStructure[tag[0]] = Tag{
Type: arg.tagType,
Name: line[tag[2]:tag[3]],
Full: line[tag[0]:tag[1]],
Start: tag[0],
End: tag[1],
}
}
}
return lineStructure
}