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 javax.swing.*;
011: import java.util.*;
012:
013: import org.apache.xerces.dom.DocumentImpl;
014: import org.apache.xml.serialize.*;
015: import org.w3c.dom.*;
016:
017: /**
018: * This class is a plug-in to RBManager that allows the user to export Resource Bundles
019: * along with some of the meta-data associated by RBManager to the TMX specification.
020: * For more information on TMX visit the web site <a href="http://www.lisa.org/tmx/">http://www.lisa.org/tmx/</a>
021: *
022: * @author Jared Jackson
023: * @see com.ibm.rbm.RBManager
024: */
025: public class RBTMXExporter extends RBExporter {
026: private static final String VERSION = "0.5a";
027:
028: /**
029: * Default constructor for the TMX exporter.
030: */
031:
032: public RBTMXExporter() {
033: super ();
034:
035: // Initialize the file chooser if necessary
036: if (chooser == null) {
037: chooser = new JFileChooser();
038: chooser
039: .setFileFilter(new javax.swing.filechooser.FileFilter() {
040: public String getDescription() {
041: return "TMX Files";
042: }
043:
044: public boolean accept(File f) {
045: if (f.isDirectory())
046: return true;
047: if (f.getName().endsWith(".tmx"))
048: return true;
049: return false;
050: }
051: });
052: } // end if
053: }
054:
055: private String convertToISO(Date d) {
056: GregorianCalendar gc = new GregorianCalendar();
057: gc.setTime(d);
058: return convertToISO(gc);
059: }
060:
061: private String convertToISO(GregorianCalendar gc) {
062: StringBuffer buffer = new StringBuffer();
063: buffer.append(String.valueOf(gc.get(Calendar.YEAR)));
064: int month = gc.get(Calendar.MONTH) + 1;
065: buffer
066: .append(((month < 10) ? "0" : "")
067: + String.valueOf(month));
068: int day = gc.get(Calendar.DAY_OF_MONTH);
069: buffer.append(((day < 10) ? "0" : "") + String.valueOf(day));
070: buffer.append("T");
071: int hour = gc.get(Calendar.HOUR_OF_DAY);
072: buffer.append(((hour < 10) ? "0" : "") + String.valueOf(hour));
073: int minute = gc.get(Calendar.MINUTE);
074: buffer.append(((minute < 10) ? "0" : "")
075: + String.valueOf(minute));
076: int second = gc.get(Calendar.SECOND);
077: buffer.append(((second < 10) ? "0" : "")
078: + String.valueOf(second));
079: buffer.append("Z");
080: return buffer.toString();
081: }
082:
083: private String convertEncoding(BundleItem item) {
084: if (item != null && item.getParentGroup() != null
085: && item.getParentGroup().getParentBundle() != null) {
086: String language = item.getParentGroup().getParentBundle()
087: .getLanguageEncoding();
088: String country = item.getParentGroup().getParentBundle()
089: .getCountryEncoding();
090: String variant = item.getParentGroup().getParentBundle()
091: .getVariantEncoding();
092: if (language != null && !language.equals("")) {
093: //language = language.toUpperCase();
094: if (country != null && !country.equals("")) {
095: //country = country.toUpperCase();
096: if (variant != null && !variant.equals("")) {
097: //variant = variant.toUpperCase();
098: return language + "-" + country + "-" + variant;
099: }
100: return language + "-" + country;
101: }
102: return language;
103: }
104: }
105: return "";
106: }
107:
108: private void appendTUV(Document xml, Element tu, BundleItem item) {
109: Element tuv = xml.createElement("tuv");
110: tuv.setAttribute("lang", convertEncoding(item));
111: tuv.setAttribute("creationdate",convertToISO(item.getCreatedDate()));
112: tuv.setAttribute("creationid",item.getCreator());
113: tuv.setAttribute("changedate",convertToISO(item.getModifiedDate()));
114: tuv.setAttribute("changeid",item.getModifier());
115: item.getComment();
116: item.isTranslated();
117:
118: Element comment_prop = xml.createElement("prop");
119: comment_prop.appendChild(xml.createTextNode(item.getComment()));
120: comment_prop.setAttribute("type","x-Comment");
121: tuv.appendChild(comment_prop);
122:
123: Element translated_prop = xml.createElement("prop");
124: translated_prop.appendChild(xml.createTextNode(String.valueOf(item.isTranslated())));
125: translated_prop.setAttribute("type","x-Translated");
126: tuv.appendChild(translated_prop);
127:
128: Hashtable lookups = item.getLookups();
129: Enumeration enum = lookups.keys();
130: while (enum.hasMoreElements()) {
131: String key = (String)enum.nextElement();
132: String value = (String)lookups.get(key);
133: Element lookup_prop = xml.createElement("prop");
134: lookup_prop.appendChild(xml.createTextNode(key + "=" + value));
135: lookup_prop.setAttribute("type","x-Lookup");
136: tuv.appendChild(lookup_prop);
137: }
138:
139: Element seg = xml.createElement("seg");
140: seg.appendChild(xml.createTextNode(item.getTranslation()));
141: tuv.appendChild(seg);
142:
143: tu.appendChild(tuv);
144: }
145:
146: public void export(RBManager rbm) throws IOException {
147: if (rbm == null)
148: return;
149: // Open the Save Dialog
150: int ret_val = chooser.showSaveDialog(null);
151: if (ret_val != JFileChooser.APPROVE_OPTION)
152: return;
153: // Retrieve basic file information
154: File file = chooser.getSelectedFile(); // The file(s) we will be working with
155: File directory = new File(file.getParent()); // The directory we will be writing to
156: String base_name = file.getName(); // The base name of the files we will write
157: if (base_name == null || base_name.equals(""))
158: base_name = rbm.getBaseClass();
159: if (base_name.endsWith(".tmx"))
160: base_name = base_name.substring(0, base_name.length() - 4);
161:
162: String file_name = base_name + ".tmx";
163:
164: Vector bundle_v = rbm.getBundles();
165: Bundle main_bundle = (Bundle) bundle_v.elementAt(0);
166:
167: Document xml = new DocumentImpl();
168: Element root = xml.createElement("tmx");
169: root.setAttribute("version", "1.2");
170: xml.appendChild(root);
171:
172: Element header = xml.createElement("header");
173: Element note = xml.createElement("note");
174: note
175: .appendChild(xml
176: .createTextNode("This document was created automatically by RBManager"));
177: header.appendChild(note);
178: header.setAttribute("creationtool", "RBManager");
179: header.setAttribute("creationtoolversion", VERSION);
180: header.setAttribute("datatype", "PlainText");
181: header.setAttribute("segtype", "sentance");
182: header.setAttribute("adminlang", "en-us");
183: header.setAttribute("srclang", "EN");
184: header.setAttribute("o-tmf", "none");
185: header.setAttribute("creationdate", convertToISO(new Date()));
186: root.appendChild(header);
187:
188: Element body = xml.createElement("body");
189: root.appendChild(body);
190:
191: Vector group_v = main_bundle.getGroupsAsVector();
192: // Loop through each bundle group in main_bundle
193: for (int i = 0; i < group_v.size(); i++) {
194: BundleGroup main_group = (BundleGroup) group_v.elementAt(i);
195: // Gather a group of groups of the same name as main_group
196: Vector all_groups_v = new Vector();
197: for (int j = 1; j < bundle_v.size(); j++) {
198: Bundle bundle = (Bundle) bundle_v.elementAt(j);
199: if (bundle.hasGroup(main_group.getName())) {
200: Vector groups = bundle.getGroupsAsVector();
201: for (int k = 0; k < groups.size(); k++) {
202: BundleGroup group = (BundleGroup) groups
203: .elementAt(k);
204: if (group.getName()
205: .equals(main_group.getName()))
206: all_groups_v.addElement(group);
207: }
208: }
209: } // end for - j
210: // Loop through each item in main_group
211: for (int j = 0; j < main_group.getItemCount(); j++) {
212: BundleItem main_item = main_group.getBundleItem(j);
213: Element tu = xml.createElement("tu");
214: tu.setAttribute("tuid", main_item.getKey());
215: tu.setAttribute("datatype", "Text");
216: // Insert the group name for the item
217: Element group_prop = xml.createElement("prop");
218: group_prop.appendChild(xml.createTextNode(main_group
219: .getName()));
220: group_prop.setAttribute("type", "x-Group");
221: tu.appendChild(group_prop);
222: // Add the main_item to the xml
223: appendTUV(xml, tu, main_item);
224: // Loop through the rest of the groups of the same name as main_group
225: for (int k = 0; k < all_groups_v.size(); k++) {
226: BundleGroup group = (BundleGroup) all_groups_v
227: .elementAt(k);
228: // Loop through the items in each group
229: for (int l = 0; l < group.getItemCount(); l++) {
230: BundleItem item = group.getBundleItem(l);
231: if (item.getKey().equals(main_item.getKey())) {
232: appendTUV(xml, tu, item);
233: break;
234: }
235: } // end for - l
236: } // end for - k
237: body.appendChild(tu);
238: } // end for - j
239: } // end for - i
240: FileWriter fw = new FileWriter(new File(directory, file_name));
241: OutputFormat of = new OutputFormat(xml);
242: of.setIndenting(true);
243: of.setEncoding("ISO-8859-1");
244: XMLSerializer serializer = new XMLSerializer(fw, of);
245: serializer.serialize(xml);
246: }
247: }
|