1/*
2 *****************************************************************************
3 * Copyright (C) 2000-2007, International Business Machines Corporation and  *
4 * others. All Rights Reserved.                                              *
5 *****************************************************************************
6 */
7package com.ibm.rbm;
8
9import java.io.*;
10import java.util.*;
11
12import javax.xml.parsers.*;
13
14import org.w3c.dom.*;
15import org.xml.sax.*;
16
17import com.ibm.rbm.gui.RBManagerGUI;
18
19
20/**
21 * This is the super class for all importer plug-in classes. This class defines the methods
22 * and functionality common to all importers. This includes setting up the options dialog and
23 * displaying it to the user, performing the actual insertions into the resource bundle manager,
24 * and managing any import conflicts.
25 *
26 * @author Jared Jackson
27 * @see com.ibm.rbm.RBManager
28 */
29public class RBTMXImporter extends RBImporter {
30
31    Document tmx_xml = null;
32
33    /**
34     * Basic constructor for the TMX importer from the parent RBManager data and a Dialog title.
35     */
36
37    public RBTMXImporter(String title, RBManager rbm, RBManagerGUI gui) {
38        super(title, rbm, gui);
39    }
40
41    protected void setupFileChooser() {
42        chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
43            public boolean accept(File f) {
44                if (f.isDirectory()) return true;
45                if (f.getName().endsWith(".tmx")) return true;
46                return false;
47            }
48
49            public String getDescription() {
50                return Resources.getTranslation("import_TMX_file_description");
51            }
52        });
53    }
54
55    protected void beginImport() throws IOException {
56        super.beginImport();
57        File tmx_file = getChosenFile();
58
59        try {
60        	FileInputStream fis = new FileInputStream(tmx_file);
61            InputSource is = new InputSource(fis);
62            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
63        	tmx_xml = builder.parse(is);
64            fis.close();
65        } catch (Exception e) {
66            e.printStackTrace(System.err);
67        	throw new IOException(e.getMessage());
68        }
69        if (tmx_xml == null)
70        	return;
71
72        importDoc();
73    }
74
75    private void importDoc() {
76        if (tmx_xml == null)
77        	return;
78        Element root = tmx_xml.getDocumentElement();
79        Node node = root.getFirstChild();
80        while (node != null && (node.getNodeType() != Node.ELEMENT_NODE || !(node.getNodeName().equalsIgnoreCase("header")))) {
81            node = node.getNextSibling();
82        }
83        //ElementImpl header = (ElementImpl)node;
84        node = root.getFirstChild();
85        while (node != null && (node.getNodeType() != Node.ELEMENT_NODE || !(node.getNodeName().equalsIgnoreCase("body")))) {
86            node = node.getNextSibling();
87        }
88        Element body = (Element)node;
89        resolveEncodings(getEncodingsVector(body));
90
91        // Now do the actual import resource by resource
92        NodeList tu_list = body.getElementsByTagName("tu");
93        for (int i=0; i < tu_list.getLength(); i++) {
94            Element tu_elem = (Element)tu_list.item(i);
95            // Get the key value
96            String name = tu_elem.getAttribute("tuid");
97            if (name == null || name.length() < 1)
98            	continue;
99            // Get the group if it exists
100            String group = null;
101            NodeList prop_list = tu_elem.getElementsByTagName("prop");
102            for (int j=0; j < prop_list.getLength(); j++) {
103                Element prop_elem = (Element)prop_list.item(j);
104                String type = prop_elem.getAttribute("type");
105                if (type != null && type.equals("x-Group")) {
106                    prop_elem.normalize();
107                    NodeList text_list = prop_elem.getChildNodes();
108                    if (text_list.getLength() < 1)
109                    	continue;
110                    Text text_elem = (Text)text_list.item(0);
111                    group = text_elem.getNodeValue();
112                }
113            }
114            if (group == null || group.length() < 1) group = getDefaultGroup();
115
116            NodeList tuv_list = tu_elem.getElementsByTagName("tuv");
117            // For each tuv element
118            for (int j=0; j < tuv_list.getLength(); j++) {
119                Element tuv_elem = (Element)tuv_list.item(j);
120                String encoding = tuv_elem.getAttribute("lang");
121                // Get the current encoding
122                if (encoding == null) continue;
123                char array[] = encoding.toCharArray();
124                for (int k=0; k < array.length; k++) {
125                    if (array[k] == '-')
126                    	array[k] = '_';
127                }
128                encoding = String.valueOf(array);
129                // Get the translation value
130                NodeList seg_list = tuv_elem.getElementsByTagName("seg");
131                if (seg_list.getLength() < 1)
132                	continue;
133                Element seg_elem = (Element)seg_list.item(0);
134                seg_elem.normalize();
135                NodeList text_list = seg_elem.getChildNodes();
136                if (text_list.getLength() < 1)
137                	continue;
138                Text text_elem = (Text)text_list.item(0);
139                String value = text_elem.getNodeValue();
140                if (value == null || value.length() < 1)
141                	continue;
142                // Create the bundle item
143                BundleItem item = new BundleItem(null, name, value);
144                // Get creation, modification values
145                item.setCreatedDate(tuv_elem.getAttribute("creationdate"));
146                item.setModifiedDate(tuv_elem.getAttribute("changedate"));
147                if (tuv_elem.getAttribute("changeid") != null)
148                	item.setModifier(tuv_elem.getAttribute("changeid"));
149                if (tuv_elem.getAttribute("creationid") != null)
150                	item.setCreator(tuv_elem.getAttribute("creationid"));
151                // Get properties specified
152                prop_list = tuv_elem.getElementsByTagName("prop");
153                Hashtable lookups = null;
154                for (int k=0; k < prop_list.getLength(); k++) {
155                    Element prop_elem = (Element)prop_list.item(k);
156                    String type = prop_elem.getAttribute("type");
157                    if (type != null && type.equals("x-Comment")) {
158                        // Get the comment
159                        prop_elem.normalize();
160                        text_list = prop_elem.getChildNodes();
161                        if (text_list.getLength() < 1) continue;
162                        text_elem = (Text)text_list.item(0);
163                        String comment = text_elem.getNodeValue();
164                        if (comment != null && comment.length() > 0)
165                        	item.setComment(comment);
166                    } else if (type != null && type.equals("x-Translated")) {
167                        // Get the translated flag value
168                        prop_elem.normalize();
169                        text_list = prop_elem.getChildNodes();
170                        if (text_list.getLength() < 1) continue;
171                        text_elem = (Text)text_list.item(0);
172                        if (text_elem.getNodeValue() != null) {
173                            if (text_elem.getNodeValue().equalsIgnoreCase("true"))
174                            	item.setTranslated(true);
175                            else if (text_elem.getNodeValue().equalsIgnoreCase("false"))
176                            	item.setTranslated(false);
177                            else
178                            	item.setTranslated(getDefaultTranslated());
179                        }
180                        else
181                        	item.setTranslated(getDefaultTranslated());
182                    } else if (type != null && type.equals("x-Lookup")) {
183                        // Get a lookup value
184                        prop_elem.normalize();
185                        text_list = prop_elem.getChildNodes();
186                        if (text_list.getLength() < 1)
187                        	continue;
188                        text_elem = (Text)text_list.item(0);
189                        if (text_elem.getNodeValue() != null) {
190                            String text = text_elem.getNodeValue();
191                            if (text.indexOf("=") > 0) {
192                                try {
193                                    if (lookups == null) lookups = new Hashtable();
194                                    String lkey = text.substring(0,text.indexOf("="));
195                                    String lvalue = text.substring(text.indexOf("=")+1,text.length());
196                                    lookups.put(lkey, lvalue);
197                                } catch (Exception ex) { /* String out of bounds - Ignore and go on */ }
198                            }
199                        }
200                        else
201                        	item.setTranslated(getDefaultTranslated());
202                    }
203                }
204                if (lookups != null) item.setLookups(lookups);
205                importResource(item, encoding, group);
206            }
207        }
208    }
209
210    private Vector getEncodingsVector(Element body) {
211        String empty = "";
212        if (body == null)
213        	return null;
214        Hashtable hash = new Hashtable();
215        NodeList tu_list = body.getElementsByTagName("tu");
216        for (int i=0; i < tu_list.getLength(); i++) {
217            Element tu_elem = (Element)tu_list.item(i);
218            NodeList tuv_list = tu_elem.getElementsByTagName("tuv");
219            for (int j=0; j < tuv_list.getLength(); j++) {
220                Element tuv_elem = (Element)tuv_list.item(j);
221                String encoding = tuv_elem.getAttribute("lang");
222                if (encoding == null)
223                	continue;
224                char array[] = encoding.toCharArray();
225                for (int k=0; k < array.length; k++) {
226                    if (array[k] == '-')
227                    	array[k] = '_';
228                }
229                encoding = String.valueOf(array);
230                if (!(hash.containsKey(encoding)))
231                	hash.put(encoding,empty);
232            }
233        }
234        Vector v = new Vector();
235        Enumeration keys = hash.keys();
236        while (keys.hasMoreElements()) {
237        	v.addElement(keys.nextElement());
238        }
239        return v;
240    }
241}