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:
011: import com.ibm.rbm.gui.RBManagerGUI;
012:
013: import java.util.*;
014:
015: /**
016: * This is the super class for all importer plug-in classes. As of yet, there
017: * is little contained in this class.
018: *
019: * @author Jared Jackson
020: * @see com.ibm.rbm.RBManager
021: */
022: public class RBPropertiesImporter extends RBImporter {
023:
024: boolean isRBMFile = true;
025:
026: /**
027: * Constructs the importer given the parent data classes and a Dialog title.
028: */
029:
030: public RBPropertiesImporter(String title, RBManager rbm,
031: RBManagerGUI gui) {
032: super (title, rbm, gui);
033: }
034:
035: protected void setupFileChooser() {
036: chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
037: public boolean accept(File f) {
038: if (f.isDirectory())
039: return true;
040: if (f.getName().toLowerCase().endsWith(".properties")
041: && f.getName().indexOf("_") < 0)
042: return true;
043: return false;
044: }
045:
046: public String getDescription() {
047: return Resources
048: .getTranslation("import_properties_file_description");
049: }
050: });
051: }
052:
053: protected void beginImport() throws IOException {
054: super .beginImport();
055: File baseFile = getChosenFile();
056: FileReader fr = new FileReader(baseFile);
057: BufferedReader br = new BufferedReader(fr);
058:
059: // Test if this is an RBManager generated file or not
060: int count = 0;
061: String line = null;
062: isRBMFile = true;
063: while ((line = br.readLine()) != null) {
064: if (!line.trim().equals("")) count++;
065: if (count == 1 && !line.startsWith("# @file")) {
066: // Not generated by RBManager
067: isRBMFile = false;
068: }
069: } // end while
070: if (isRBMFile) {
071: // Treat the file as generated by RBManager
072: // Parse the resource bundle through RBManager
073: RBManager import_rbm = new RBManager(baseFile);
074: // Merge the two resource bundles
075: Vector bundles = import_rbm.getBundles();
076: Vector encodings = new Vector();
077: for (int i=0; i < bundles.size(); i++) {
078: Bundle b = (Bundle)bundles.elementAt(i);
079: encodings.addElement(b.encoding);
080: }
081: resolveEncodings(encodings);
082: for (int i=0; i < bundles.size(); i++) {
083: Bundle b = (Bundle)bundles.elementAt(i);
084: Enumeration enum = b.allItems.keys();
085: while (enum.hasMoreElements()) {
086: String key = (String)enum.nextElement();
087: BundleItem item = (BundleItem)b.allItems.get(key);
088: importResource(item, b.encoding, (item.getParentGroup() == null ? getDefaultGroup(): item.getParentGroup().getName()));
089: }
090: }
091: } else {
092: // Just treat it as a regular properties file
093: // Check if there are any missing target locale files
094: String baseName = baseFile.getName().substring(0,baseFile.getName().length()-11); // |'.properties'| == 11
095: File baseDir = new File(baseFile.getParent());
096: String allChildren[] = baseDir.list();
097: Vector children_v = new Vector();
098: for (int i=0; i < allChildren.length; i++) {
099: if (allChildren[i].startsWith(baseName) && allChildren[i].toLowerCase().endsWith(".properties")) {
100: if (allChildren[i].length() == (baseName + ".properties").length()) children_v.addElement("");
101: else children_v.addElement(allChildren[i].substring(baseName.length()+1, allChildren[i].indexOf(".properties")));
102: }
103: }
104: showProgressBar(children_v.size());
105: resolveEncodings(children_v);
106: // Run through each source locale file importing as necessary
107: for (int i=0; i < children_v.size(); i++) {
108: Properties p = new Properties();
109: FileInputStream fis = new FileInputStream(new File(baseDir, baseName +
110: (children_v.elementAt(i).toString().equals("") ? "" : "_" + children_v.elementAt(i).toString()) +
111: ".properties"));
112: p.load(fis);
113: Enumeration enum = p.keys();
114: while (enum.hasMoreElements()) {
115: String key = (String)enum.nextElement();
116: BundleItem item = new BundleItem(null, key, p.getProperty(key));
117: item.setTranslated(this.getDefaultTranslated());
118: importResource(item, children_v.elementAt(i).toString(), getDefaultGroup());
119: }
120: incrementProgressBar();
121: }
122: hideProgressBar();
123: }
124: }
125: }
|