1 package se.perfectfools.localizer.parser.impl;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.Iterator;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import se.perfectfools.localizer.LocalizationFile;
19 import se.perfectfools.localizer.Message;
20 import se.perfectfools.localizer.i18n.Messages;
21 import se.perfectfools.localizer.impl.LocalizationFileImpl;
22 import se.perfectfools.localizer.impl.MessageImpl;
23 import se.perfectfools.localizer.parser.XMLParser;
24
25 /***
26 *
27 *
28 * @author Pål Brattberg, pal@eminds.se
29 */
30 public class XMLParserImpl implements XMLParser {
31
32 private static final String[] typeName = { "none", "Element", "Attr",
33 "Text", "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment",
34 "Document", "DocType", "DocFragment", "Notation", };
35
36 public LocalizationFile parseFile(InputStream inputStream) {
37
38 LocalizationFile locFile = new LocalizationFileImpl();
39
40 Document document = parseXML(inputStream);
41 NodeList nodeList = document.getElementsByTagName("trans");
42 int size = nodeList.getLength();
43 for (int i = 0; i < size; i++) {
44
45 Node node = nodeList.item(i);
46 Node idNode = node.getAttributes().item(0);
47 String id = idNode.getNodeValue();
48 String text = node.getFirstChild().getNodeValue();
49 Message msg = new MessageImpl();
50 msg.setId(id);
51 msg.setText(text);
52 locFile.addMessage(msg);
53 }
54
55 return locFile;
56 }
57
58 private Document parseXML(InputStream in) {
59 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60 Document document = null;
61 try {
62
63 DocumentBuilder builder = factory.newDocumentBuilder();
64 document = builder.parse(in);
65
66 } catch (Exception e) {
67 throw new RuntimeException(Messages.getString("XMLParser.err.couldnotparse"), e);
68 }
69 return document;
70 }
71
72 public void serializeToFile(LocalizationFile model, OutputStream os) {
73
74
75 StringBuffer buf = new StringBuffer(
76 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
77 buf.append("<inData>\n");
78
79
80 Iterator iter = model.getMessages().iterator();
81 ArrayList sortedList = new ArrayList();
82 while (iter.hasNext()) {
83 sortedList.add(iter.next());
84 }
85 Collections.sort(sortedList, new Comparator() {
86 public boolean equals(Object arg0) {
87 return super.equals(arg0);
88 }
89
90 public int compare(Object arg0, Object arg1) {
91 int aId = Integer.parseInt(((Message) arg0).getId());
92 int bId = Integer.parseInt(((Message) arg1).getId());
93 return aId - bId;
94 }
95 });
96 iter = sortedList.iterator();
97
98 while (iter.hasNext()) {
99 Message msg = (Message) iter.next();
100 buf.append("<trans id=\"");
101 buf.append(msg.getId());
102 buf.append("\"><![CDATA[");
103 buf.append(msg.getText());
104 buf.append("]]></trans>\n");
105 }
106
107 buf.append("</inData>\n");
108
109
110 try {
111 os.write(buf.toString().getBytes());
112 } catch (IOException e) {
113 throw new RuntimeException(Messages.getString("XMLParser.err.couldnotwrite"), e);
114 }
115 }
116
117 }