-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathXMLMerge.java
More file actions
446 lines (407 loc) · 17.5 KB
/
XMLMerge.java
File metadata and controls
446 lines (407 loc) · 17.5 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
public class XMLMerge {
private static Map<File, Document> sSrc = new HashMap<File, Document>();
private static Map<File, Document> sDes = new HashMap<File, Document>();
private static Map<String, HashSet<String>> sIncludeRule = new HashMap<String, HashSet<String>>();
private static Map<String, HashSet<String>> sExcludeRule = new HashMap<String, HashSet<String>>();
private static Map<String, HashSet<String>> sInsertRule = new HashMap<String, HashSet<String>>();
private static final int MERGE_DONT_NEED_MERGE = 1;
private static final int MERGE_DONT_FIND_DEST_FILE = 2;
private static final int MERGE_ADD_ELEMENT = 3;
private static final int MERGE_MODIFY_ELEMENT = 4;
private static final int MERGE_INCERT_ITEMS = 5;
private static final int MERGE_INVALID_NODE = 6;
private static final String RESOURCES_TAG = "resources";
private static final String STRING_ARRAY_TAG = "string-array";
private static final String STRING_TAG = "string";
private static final String XLIFF_TAG = "xliff:g";
private static final String ITEM_TAG = "item";
private static final String NAME_TAG = "name";
private static final String LOG_TAG = "merge xml";
private static final String MSGID_TAG = "msgid";
public XMLMerge(ArrayList<File> srcFiles, ArrayList<File> desFiles, ArrayList<File> configFiles) {
for (File srcFile : srcFiles) {
try {
sSrc.put(srcFile,
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(srcFile));
} catch (Exception e) {
e.printStackTrace();
Log.w(LOG_TAG, "src file:" + srcFile.getName() + " is invalid xml file");
}
}
for (File desFile : desFiles) {
try {
sDes.put(desFile,
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(desFile));
} catch (Exception e) {
e.printStackTrace();
Log.w(LOG_TAG, "dest file:" + desFile.getName() + " is invalid xml file");
}
}
parseConfigFile(configFiles);
printConfig();
}
private void parseConfigFile(ArrayList<File> configFiles) {
for (File f : configFiles) {
String l;
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(f));
while ((l = r.readLine()) != null) {
l = l.trim();
if (l.length() == 0 || l.startsWith("#")) {
continue;
}
String[] item = l.split(" ");
if (item.length > 1
&& ("-".equals(item[0]) || "+".equals(item[0]) || "I"
.equalsIgnoreCase(item[0]))) {
String nodeName = item[1];
HashSet<String> nameAttrs;
if ("-".equals(item[0])) {
nameAttrs = sExcludeRule.get(nodeName);
if (nameAttrs == null) {
nameAttrs = new HashSet<String>();
sExcludeRule.put(nodeName, nameAttrs);
}
} else if ("+".equals(item[0])) {
nameAttrs = sIncludeRule.get(nodeName);
if (nameAttrs == null) {
nameAttrs = new HashSet<String>();
sIncludeRule.put(nodeName, nameAttrs);
}
} else {
nameAttrs = sInsertRule.get(nodeName);
if (nameAttrs == null) {
nameAttrs = new HashSet<String>();
sInsertRule.put(nodeName, nameAttrs);
}
}
if (item.length == 2) {
nameAttrs.add("*");
} else {
String nameAttrValue = item[2];
nameAttrs.add(nameAttrValue);
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (r != null) {
try {
r.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
private void printConfig() {
Log.i("------------------------------------------------");
Log.i(LOG_TAG, "Include:");
for (Map.Entry<String, HashSet<String>> e : sIncludeRule.entrySet()) {
Log.i(LOG_TAG, "-->" + e.getKey());
for (String s : e.getValue()) {
Log.i(LOG_TAG, "----->" + s);
}
}
Log.i("");
Log.i(LOG_TAG, "Exclude:");
for (Map.Entry<String, HashSet<String>> e : sExcludeRule.entrySet()) {
Log.i(LOG_TAG, "-->" + e.getKey());
for (String s : e.getValue()) {
Log.i(LOG_TAG, "----->" + s);
}
}
Log.i("");
Log.i(LOG_TAG, "Insert:");
for (Map.Entry<String, HashSet<String>> e : sInsertRule.entrySet()) {
Log.i(LOG_TAG, "-->" + e.getKey());
for (String s : e.getValue()) {
Log.i(LOG_TAG, "----->" + s);
}
}
Log.i("");
Log.i("------------------------------------------------");
}
private boolean chcekFile() {
for (Map.Entry<File, Document> src : sSrc.entrySet()) {
if (src.getValue().getElementsByTagName(RESOURCES_TAG).getLength() != 1) {
Log.w(LOG_TAG, "src file:" + src.getKey().getName() + " don't include a valid \""
+ RESOURCES_TAG + "\" node");
sSrc.remove(src.getKey());
}
}
for (Map.Entry<File, Document> des : sDes.entrySet()) {
if (des.getValue().getElementsByTagName(RESOURCES_TAG).getLength() != 1) {
Log.w(LOG_TAG, "dest file:" + des.getKey().getName() + " don't include a valid \""
+ RESOURCES_TAG + "\" node");
sDes.remove(des.getKey());
}
}
if (sSrc.isEmpty()) {
Log.w(LOG_TAG, "no vaild src files");
return false;
}
if (sDes.isEmpty()) {
Log.w(LOG_TAG, "no valid dest files");
return false;
}
return true;
}
public void merge() {
if (!chcekFile()) {
return;
}
for (Map.Entry<File, Document> entry : sSrc.entrySet()) {
Log.i("------------------------------------------------");
Log.i(LOG_TAG, "merge file: " + entry.getKey().getName());
mergeFile(entry);
Log.i("------------------------------------------------");
Log.i("");
}
saveDestFile();
}
private void saveDestFile() {
for (Map.Entry<File, Document> des : sDes.entrySet()) {
writeXML(des.getValue(), des.getKey());
}
}
private void mergeFile(Map.Entry<File, Document> entry) {
Document doc = entry.getValue();
formatDoc(doc);
Node resNode = doc.getElementsByTagName(RESOURCES_TAG).item(0);
NodeList subTypes = resNode.getChildNodes();
for (int i = 0; i < subTypes.getLength(); i++) {
if (Node.ELEMENT_NODE == subTypes.item(i).getNodeType()) {
mergeNode(subTypes.item(i));
}
}
return;
}
private int mergeNode(Node node) {
if (!checkNode(node)) {
Log.i(LOG_TAG, "invalid src node: " + node.getNodeName());
return MERGE_INVALID_NODE;
}
if (!needMerge(node)) {
Log.i(LOG_TAG, "dont need merge src node: " + node.getNodeName() + "[" + "name="
+ ((Element) node).getAttribute(NAME_TAG) + "]");
return MERGE_DONT_NEED_MERGE;
}
if (tryInsertItemsToDesArrayNode(node)) {
Log.i(LOG_TAG, "inscert new items to dest array node: " + node.getNodeName() + "["
+ "name="
+ ((Element) node).getAttribute(NAME_TAG) + "]");
return MERGE_INCERT_ITEMS;
}
if (tryReplaceDesNode(node)) {
Log.i(LOG_TAG, "replace node to dest: " + node.getNodeName() + "[" + "name="
+ ((Element) node).getAttribute(NAME_TAG) + "]");
return MERGE_MODIFY_ELEMENT;
}
if (tryAddDesNode(node)) {
Log.i(LOG_TAG, "add node to dest: " + node.getNodeName() + "[" + "name="
+ ((Element) node).getAttribute(NAME_TAG) + "]");
return MERGE_ADD_ELEMENT;
}
Log.i(LOG_TAG, "can't find dest file for node: " + node.getNodeName() + "[" + "name="
+ ((Element) node).getAttribute(NAME_TAG) + "]");
return MERGE_DONT_FIND_DEST_FILE;
}
private void formatDoc(Document doc) {
removeXliffgNode(doc);
removeMsgidAttr(doc);
}
private boolean checkNode(Node node) {
// check if node has a "name" attribute
return !"".equals(((Element) node).getAttribute(NAME_TAG));
}
private boolean needMerge(Node node) {
// search Include and Insert config firstly
if (matchConfig(node, sIncludeRule) || matchConfig(node, sInsertRule)) {
return true;
}
// search Exclude config secondly
return !matchConfig(node, sExcludeRule);
}
private boolean matchConfig(Node node, Map<String, HashSet<String>> configs) {
HashSet<String> nameAttrValues;
if ((nameAttrValues = configs.get(node.getNodeName())) == null) {
return false;
}
if (nameAttrValues.contains("*")
|| nameAttrValues.contains(((Element) node).getAttribute(NAME_TAG))) {
return true;
}
return false;
}
// try insert the new items to array node
private boolean tryInsertItemsToDesArrayNode(Node node) {
String nodeName = node.getNodeName();
String nameAttrValue = ((Element) node).getAttribute(NAME_TAG);
if (matchConfig(node, sInsertRule)) {
for (Map.Entry<File, Document> des : sDes.entrySet()) {
Document desDoc = des.getValue();
NodeList desNodes = desDoc.getElementsByTagName(nodeName);
for (int i = 0; i < desNodes.getLength(); i++) {
Node desNode = desNodes.item(i);
try {
if (nameAttrValue.equals(((Element) desNode).getAttribute(NAME_TAG))) {
NodeList desItems = ((Element) desNode).getElementsByTagName(ITEM_TAG);
NodeList srcItems = ((Element) node).getElementsByTagName(ITEM_TAG);
if (desItems.getLength() == 0 || srcItems.getLength() == 0) {
return false;
}
ArrayList<String> desItemTexts = new ArrayList<String>();
for (int j = 0; j < desItems.getLength(); j++) {
desItemTexts.add(desItems.item(j).getTextContent());
}
for (int j = 0; j < srcItems.getLength(); j++) {
String srcText = srcItems.item(j).getTextContent();
if (!desItemTexts.contains(srcText)) {
Element item = desDoc.createElement(ITEM_TAG);
item.appendChild(desDoc.createTextNode(srcText));
desNode.insertBefore(item, desItems.item(0));
}
}
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
}
return false;
}
// if dest node with same node name and "name" attribute
// just to replace the dest node with src node
private boolean tryReplaceDesNode(Node node) {
String nodeName = node.getNodeName();
String nameAttrValue = ((Element) node).getAttribute(NAME_TAG);
for (Map.Entry<File, Document> des : sDes.entrySet()) {
Document desDoc = des.getValue();
NodeList desNodes = des.getValue().getElementsByTagName(nodeName);
for (int i = 0; i < desNodes.getLength(); i++) {
Node desNode = desNodes.item(i);
try {
if (nameAttrValue.equals(((Element) desNode).getAttribute(NAME_TAG))) {
desNode.getParentNode().replaceChild(desDoc.importNode(node, true),
desNode);
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
return false;
}
// add node to the doc which include same node name
private boolean tryAddDesNode(Node node) {
String nodeName = node.getNodeName();
for (Map.Entry<File, Document> des : sDes.entrySet()) {
Document desDoc = des.getValue();
if (existNode(des.getValue(), nodeName)) {
try {
desDoc.getElementsByTagName(nodeName).item(0).getParentNode()
.appendChild(desDoc.importNode(node, true));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
return false;
}
private boolean existNode(Document doc, String nodeName) {
NodeList nodes = doc.getElementsByTagName(nodeName);
return nodes.getLength() > 0;
}
private void removeXliffgNodeFromStringArray(Document doc) {
NodeList stringArrays = doc.getElementsByTagName(STRING_ARRAY_TAG);
for (int i = 0; i < stringArrays.getLength(); i++) {
Element stringArray = (Element) stringArrays.item(i);
NodeList xliffs = stringArray.getElementsByTagName(XLIFF_TAG);
if (xliffs.getLength() > 0) {
Node papa = xliffs.item(0).getParentNode().getParentNode();
ArrayList<String> vals = new ArrayList<String>();
while (stringArray.getElementsByTagName(XLIFF_TAG).getLength() != 0) {
vals.add(xliffs.item(0).getTextContent());
xliffs.item(0).getParentNode().getParentNode()
.removeChild(xliffs.item(0).getParentNode());
}
for (int j = 0; j < vals.size(); j++) {
Element e = doc.createElement(ITEM_TAG);
e.appendChild(doc.createTextNode(vals.get(j)));
papa.appendChild(e);
}
}
}
}
private void removeXliffgNodeFromString(Document doc) {
Element res = (Element) doc.getElementsByTagName(RESOURCES_TAG).item(0);
NodeList xliffs = res.getElementsByTagName(XLIFF_TAG);
int len = xliffs.getLength();
while (len > 0) {
Node xliff = xliffs.item(0);
Text text = doc.createTextNode(xliff.getTextContent());
xliff.getParentNode().replaceChild(text, xliff);
xliffs = res.getElementsByTagName(XLIFF_TAG);
len = xliffs.getLength();
}
}
private void removeXliffgNode(Document doc) {
removeXliffgNodeFromStringArray(doc);
removeXliffgNodeFromString(doc);
}
private void removeMsgidAttr(Document doc) {
Node resNode = doc.getElementsByTagName(RESOURCES_TAG).item(0);
NodeList nodes = resNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (Node.ELEMENT_NODE == node.getNodeType()) {
((Element) node).removeAttribute(MSGID_TAG);
}
}
}
private void writeXML(Document doc, File file) {
try {
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(4);
Writer output = new BufferedWriter(new FileWriter(file));
XMLSerializer serializer = new XMLSerializer(output, format);
serializer.serialize(doc);
} catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "write xml file " + file.getName() + " failed");
}
}
}