/
WriteXml.groovy
181 lines (162 loc) · 6.19 KB
/
WriteXml.groovy
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
package org.pandawarrior
import groovy.xml.MarkupBuilder
/**
* Created by jt on 5/9/15.
*/
class WriteXml {
final static String ARRAY_FILE = "arrays"
final static String PLURALS_FILE = "plurals"
private static final String REGEX = /,(?=([^\"]*\"[^\"]*\")*[^\"]*$)/
static boolean parse(String csvPath, String moduleFolder) {
//load and split the file
List rows = getRows(csvPath)
List head = getHead(rows)
Map mainDict = getMainMap(rows, head)
Map transDict = getTransMap(rows)
return writeFile(moduleFolder, mainDict, transDict)
}
static boolean parseArray(String csvPath, String moduleFolder, String type) {
//load and split the file
List rows = getRows(csvPath)
List head = getHead(rows)
Map transDict = getTransMap(rows)
List nameList = getArrayNameList(transDict)
Map mainDict = getMainArrayMap(rows, head, nameList)
return writeArrayFile(moduleFolder, mainDict, transDict, type)
}
protected static List getRows(String csvPath) {
String csv = new File(csvPath).getText()
String[] lines = csv.split('\n')
List<String[]> rows = lines.collect{
it.split(REGEX, -1)
}
return rows
}
protected static List getHead(List rows) {
List head = rows.get(0)
head = head - "\"name\""
head = head - "name"
head = head - "\"translatable\""
head = head - "translatable"
// println head
return head
}
protected static Map getTransMap(List rows) {
Map transMap = [:]
for (int i = 1; i < rows.size(); i++) {
transMap[rows[i][0]] = rows[i][1].replaceAll("\"", "")
}
return transMap
}
protected static Map getMainMap(List rows, List head) throws ArrayIndexOutOfBoundsException {
Map mainDict = [:]
// println rows
for (int i = 0; i < head.size(); i++) {
def tempMap = [:]
for (int j = 1; j < rows.size(); j++) {
def column = rows.get(j)
def name = column[0]
tempMap[name] = column[i + 2]
}
mainDict[head[i].replaceAll("\"", "")] = tempMap
}
return mainDict
}
protected static List getArrayNameList(Map transMap) {
return transMap.collect { it.key }
}
protected static Map getMainArrayMap(List rows, List head, List nameList) {
Map mainArrayMap = [:]
for (int i = 0; i < head.size(); i++) {
def tempMap = [:]
nameList.each { String name ->
def tempList = []
for (int j = 1; j < rows.size(); j++) {
def column = rows.get(j)
def tempName = column[0]
if (tempName == name) {
tempList.add column[i + 2]
tempMap[name] = tempList
}
}
}
mainArrayMap[head[i].replaceAll("\"", "")] = tempMap
}
return mainArrayMap
}
private static boolean writeFile(String destination, Map mainDict, Map transDict) {
mainDict.each {
def stringWriter = new StringWriter()
def xml = new MarkupBuilder(stringWriter)
def mainDictValue = it.value
def fileName = it.key
String dir = "${destination}/res/${fileName}/"
File file = new File(dir, 'strings.xml')
File folder = new File(dir)
if (!folder.exists()) {
folder.mkdirs()
}
xml.resources {
mainDictValue.each {
def key = it.key
def value = it.value.replaceAll("\"", "")
if (fileName.equals("values") && transDict[key].equals("false") ||
fileName.equals("values") && !transDict[key]) {
string(name: key.replaceAll("\"", ""), translatable: transDict[key], value)
} else if (transDict[key].equals("true") && !value.equals("null") && !value.equals("") ||
transDict[key] && !value.equals("null") && !value.equals("")
) {
string(name: key.replaceAll("\"", ""), value)
}
}
}
def records = stringWriter.toString()
file.withWriter('utf-8') { writer ->
writer.write(records)
}
return true
}
}
private static boolean writeArrayFile(String destination, Map mainArrayMap, Map transDict, String type) {
mainArrayMap.each {
def stringWriter = new StringWriter()
def xml = new MarkupBuilder(stringWriter)
def mainDictValue = it.value
def fileName = it.key
def rowName
String dir = "${destination}/res/${fileName}/"
File file = new File(dir, "${type}.xml")
File folder = new File(dir)
if (!folder.exists()) {
folder.mkdirs()
}
rowName = type == ARRAY_FILE ? "string-array" : type
xml.resources {
mainDictValue.each {
def key = it.key
def value = it.value.collect()
if (fileName.equals("values") && transDict[key].equals("false")) {
"${rowName}"(name: key, translatable: transDict[key]) {
value.each {
item(it)
}
}
} else if (transDict[key].equals("true")) {
"${rowName}"(name: key) {
value.each {
if (!it.equals("null") || !it.equals(" ")) {
item(it)
}
}
}
}
}
}
def records = stringWriter.toString()
file.withWriter('utf-8') { writer ->
writer.write(records)
}
return true
}
}
}