001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm;
008:
009: import java.io.*;
010: import java.util.*;
011:
012: import javax.xml.parsers.*;
013:
014: import org.w3c.dom.*;
015: import org.xml.sax.*;
016:
017: import com.ibm.rbm.gui.RBManagerGUI;
018:
019: /**
020: * This is the super class for all importer plug-in classes. This class defines the methods
021: * and functionality common to all importers. This includes setting up the options dialog and
022: * displaying it to the user, performing the actual insertions into the resource bundle manager,
023: * and managing any import conflicts.
024: *
025: * @author Jared Jackson
026: * @see com.ibm.rbm.RBManager
027: */
028: public class RBTMXImporter extends RBImporter {
029:
030: Document tmx_xml = null;
031:
032: /**
033: * Basic constructor for the TMX importer from the parent RBManager data and a Dialog title.
034: */
035:
036: public RBTMXImporter(String title, RBManager rbm, RBManagerGUI gui) {
037: super (title, rbm, gui);
038: }
039:
040: protected void setupFileChooser() {
041: chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
042: public boolean accept(File f) {
043: if (f.isDirectory())
044: return true;
045: if (f.getName().endsWith(".tmx"))
046: return true;
047: return false;
048: }
049:
050: public String getDescription() {
051: return Resources
052: .getTranslation("import_TMX_file_description");
053: }
054: });
055: }
056:
057: protected void beginImport() throws IOException {
058: super .beginImport();
059: File tmx_file = getChosenFile();
060:
061: try {
062: FileInputStream fis = new FileInputStream(tmx_file);
063: InputSource is = new InputSource(fis);
064: DocumentBuilder builder = DocumentBuilderFactory
065: .newInstance().newDocumentBuilder();
066: tmx_xml = builder.parse(is);
067: fis.close();
068: } catch (Exception e) {
069: e.printStackTrace(System.err);
070: throw new IOException(e.getMessage());
071: }
072: if (tmx_xml == null)
073: return;
074:
075: importDoc();
076: }
077:
078: private void importDoc() {
079: if (tmx_xml == null)
080: return;
081: Element root = tmx_xml.getDocumentElement();
082: Node node = root.getFirstChild();
083: while (node != null
084: && (node.getNodeType() != Node.ELEMENT_NODE || !(node
085: .getNodeName().equalsIgnoreCase("header")))) {
086: node = node.getNextSibling();
087: }
088: //ElementImpl header = (ElementImpl)node;
089: node = root.getFirstChild();
090: while (node != null
091: && (node.getNodeType() != Node.ELEMENT_NODE || !(node
092: .getNodeName().equalsIgnoreCase("body")))) {
093: node = node.getNextSibling();
094: }
095: Element body = (Element) node;
096: resolveEncodings(getEncodingsVector(body));
097:
098: // Now do the actual import resource by resource
099: NodeList tu_list = body.getElementsByTagName("tu");
100: for (int i = 0; i < tu_list.getLength(); i++) {
101: Element tu_elem = (Element) tu_list.item(i);
102: // Get the key value
103: String name = tu_elem.getAttribute("tuid");
104: if (name == null || name.length() < 1)
105: continue;
106: // Get the group if it exists
107: String group = null;
108: NodeList prop_list = tu_elem.getElementsByTagName("prop");
109: for (int j = 0; j < prop_list.getLength(); j++) {
110: Element prop_elem = (Element) prop_list.item(j);
111: String type = prop_elem.getAttribute("type");
112: if (type != null && type.equals("x-Group")) {
113: prop_elem.normalize();
114: NodeList text_list = prop_elem.getChildNodes();
115: if (text_list.getLength() < 1)
116: continue;
117: Text text_elem = (Text) text_list.item(0);
118: group = text_elem.getNodeValue();
119: }
120: }
121: if (group == null || group.length() < 1)
122: group = getDefaultGroup();
123:
124: NodeList tuv_list = tu_elem.getElementsByTagName("tuv");
125: // For each tuv element
126: for (int j = 0; j < tuv_list.getLength(); j++) {
127: Element tuv_elem = (Element) tuv_list.item(j);
128: String encoding = tuv_elem.getAttribute("lang");
129: // Get the current encoding
130: if (encoding == null)
131: continue;
132: char array[] = encoding.toCharArray();
133: for (int k = 0; k < array.length; k++) {
134: if (array[k] == '-')
135: array[k] = '_';
136: }
137: encoding = String.valueOf(array);
138: // Get the translation value
139: NodeList seg_list = tuv_elem
140: .getElementsByTagName("seg");
141: if (seg_list.getLength() < 1)
142: continue;
143: Element seg_elem = (Element) seg_list.item(0);
144: seg_elem.normalize();
145: NodeList text_list = seg_elem.getChildNodes();
146: if (text_list.getLength() < 1)
147: continue;
148: Text text_elem = (Text) text_list.item(0);
149: String value = text_elem.getNodeValue();
150: if (value == null || value.length() < 1)
151: continue;
152: // Create the bundle item
153: BundleItem item = new BundleItem(null, name, value);
154: // Get creation, modification values
155: item.setCreatedDate(tuv_elem
156: .getAttribute("creationdate"));
157: item.setModifiedDate(tuv_elem
158: .getAttribute("changedate"));
159: if (tuv_elem.getAttribute("changeid") != null)
160: item.setModifier(tuv_elem.getAttribute("changeid"));
161: if (tuv_elem.getAttribute("creationid") != null)
162: item
163: .setCreator(tuv_elem
164: .getAttribute("creationid"));
165: // Get properties specified
166: prop_list = tuv_elem.getElementsByTagName("prop");
167: Hashtable lookups = null;
168: for (int k = 0; k < prop_list.getLength(); k++) {
169: Element prop_elem = (Element) prop_list.item(k);
170: String type = prop_elem.getAttribute("type");
171: if (type != null && type.equals("x-Comment")) {
172: // Get the comment
173: prop_elem.normalize();
174: text_list = prop_elem.getChildNodes();
175: if (text_list.getLength() < 1)
176: continue;
177: text_elem = (Text) text_list.item(0);
178: String comment = text_elem.getNodeValue();
179: if (comment != null && comment.length() > 0)
180: item.setComment(comment);
181: } else if (type != null
182: && type.equals("x-Translated")) {
183: // Get the translated flag 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: if (text_elem.getNodeValue()
191: .equalsIgnoreCase("true"))
192: item.setTranslated(true);
193: else if (text_elem.getNodeValue()
194: .equalsIgnoreCase("false"))
195: item.setTranslated(false);
196: else
197: item
198: .setTranslated(getDefaultTranslated());
199: } else
200: item.setTranslated(getDefaultTranslated());
201: } else if (type != null && type.equals("x-Lookup")) {
202: // Get a lookup value
203: prop_elem.normalize();
204: text_list = prop_elem.getChildNodes();
205: if (text_list.getLength() < 1)
206: continue;
207: text_elem = (Text) text_list.item(0);
208: if (text_elem.getNodeValue() != null) {
209: String text = text_elem.getNodeValue();
210: if (text.indexOf("=") > 0) {
211: try {
212: if (lookups == null)
213: lookups = new Hashtable();
214: String lkey = text.substring(0,
215: text.indexOf("="));
216: String lvalue = text.substring(text
217: .indexOf("=") + 1, text
218: .length());
219: lookups.put(lkey, lvalue);
220: } catch (Exception ex) { /* String out of bounds - Ignore and go on */
221: }
222: }
223: } else
224: item.setTranslated(getDefaultTranslated());
225: }
226: }
227: if (lookups != null)
228: item.setLookups(lookups);
229: importResource(item, encoding, group);
230: }
231: }
232: }
233:
234: private Vector getEncodingsVector(Element body) {
235: String empty = "";
236: if (body == null)
237: return null;
238: Hashtable hash = new Hashtable();
239: NodeList tu_list = body.getElementsByTagName("tu");
240: for (int i=0; i < tu_list.getLength(); i++) {
241: Element tu_elem = (Element)tu_list.item(i);
242: NodeList tuv_list = tu_elem.getElementsByTagName("tuv");
243: for (int j=0; j < tuv_list.getLength(); j++) {
244: Element tuv_elem = (Element)tuv_list.item(j);
245: String encoding = tuv_elem.getAttribute("lang");
246: if (encoding == null)
247: continue;
248: char array[] = encoding.toCharArray();
249: for (int k=0; k < array.length; k++) {
250: if (array[k] == '-')
251: array[k] = '_';
252: }
253: encoding = String.valueOf(array);
254: if (!(hash.containsKey(encoding)))
255: hash.put(encoding,empty);
256: }
257: }
258: Vector v = new Vector();
259: Enumeration enum = hash.keys();
260: while (enum.hasMoreElements()) {
261: v.addElement(enum.nextElement());
262: }
263: return v;
264: }
265: }
|