-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceMappingHelper.java
More file actions
182 lines (165 loc) · 7.16 KB
/
ResourceMappingHelper.java
File metadata and controls
182 lines (165 loc) · 7.16 KB
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
package com.example.testapp;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Helper Class which can create XML resource from CSV file
* where csv contain 2 column first is english word and second is translation in other language
* it maps source string from default xml and create new xml
*/
public class ResourceMappingHelper {
private static final String DEFAULT_FILE_PATH = "string_generated";
private String csvSourcePath;
private String defaultStringXmlPath;
private String xmlDestinationPath;
private Map<String, String> map;
private Map<String, List<String>> stringListMap;
private int totalTranslation;
/**
* Constructor
* @param csvSourcePath full path of csv file which contain translation
* @param defaultStringXmlPath full path of default string resource xml
* @param xmlDestinationPath full path where generated file will be saved
*/
public ResourceMappingHelper(String csvSourcePath, String defaultStringXmlPath, String xmlDestinationPath) {
this.csvSourcePath = csvSourcePath;
this.defaultStringXmlPath = defaultStringXmlPath;
this.xmlDestinationPath = xmlDestinationPath;
generateStringMapping();
}
/**
* Constructor in case destination path will be current directory
* @param csvSourcePath full path of csv file which contain translation
* @param defaultStringXmlPath full path of default string resource xml
*/
public ResourceMappingHelper(String csvSourcePath, String defaultStringXmlPath) {
this(csvSourcePath, defaultStringXmlPath, new File(DEFAULT_FILE_PATH).getAbsolutePath());
}
/**
* Create a mapping between default text and translated text
* for example english(key) --- spanish(value)
*/
private void generateStringMapping() {
map = new HashMap<>();
stringListMap = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(csvSourcePath));
String line;
while ((line = br.readLine()) !=null) {
Iterable<String> iterable = Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")).split(line);
String[] data = Iterables.toArray(iterable, String.class);
if (data.length < 2) {
throw new IllegalArgumentException("CSV should contain at least 2 comma separated values");
}
for (int i = 0; i < data.length; i++) {
data[i] = data[i].replaceAll("^\"|\"$", "");
}
totalTranslation = data.length > 2 ? data.length -1 : 1;
stringListMap.put(data[0], Arrays.asList(Arrays.copyOfRange(data, 1, data.length)));
}
br.close();
}catch (IOException ignored){ }
}
/**
* init resource string creation
*/
public void generateResource(){
try {
for (int i = 0; i < totalTranslation; i++) {
String generatedFilePath = new File(xmlDestinationPath, String.format("index_%d_.xml", i+1)).getAbsolutePath();
File fXmlFile = new File(defaultStringXmlPath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("resources");
Node nNode = nList.item(0);
for (int index = 0; index < nNode.getChildNodes().getLength(); index++) {
Node node = nNode.getChildNodes().item(index);
if (node.getNodeType() != Node.COMMENT_NODE && node.getAttributes() != null) {
String keyName = node.getAttributes().getNamedItem("name").getNodeValue();
boolean isTranslatable = isTranslatable(node.getAttributes().getNamedItem("translatable"));
if(isTranslatable){
updateThisNode(node, i);
}else {
nNode.removeChild(node);
}
}
}
// writing updated document
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(generatedFilePath));
transformer.transform(source, result);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* Update Text of nodes recursively (recursive when nested array is used which is rare ;))
* @param node node for which text is updated
*/
private void updateThisNode(final Node node, final int stringIndex){
if (node == null) return;
if (node.getNodeName().equals("string-array")){
// iterate every child node when xml tag is string
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
updateThisNode(node.getChildNodes().item(i), stringIndex);
}
}
if (node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element) node;
String key = element.getTextContent();
if (stringListMap.containsKey(key)) {
element.setTextContent(stringListMap.get(key).get(stringIndex));
}
}
}
/**
* Check weather node is Translatable or not
* @param node note to check
* @return true if Translatable false otherwise
*/
private boolean isTranslatable(final Node node) {
if (node != null && node.getNodeValue().equalsIgnoreCase("false")){
return false;
}
return true;
}
}