1 /* 2 ***************************************************************************** 3 * Copyright (C) 2000-2007, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ***************************************************************************** 6 */ 7 package com.ibm.rbm; 8 9 import java.io.*; 10 11 import com.ibm.rbm.gui.RBManagerGUI; 12 13 import java.util.*; 14 15 /** 16 * This is the super class for all importer plug-in classes. As of yet, there 17 * is little contained in this class. 18 * 19 * @author Jared Jackson 20 * @see com.ibm.rbm.RBManager 21 */ 22 public class RBPropertiesImporter extends RBImporter { 23 24 boolean isRBMFile = true; 25 26 /** 27 * Constructs the importer given the parent data classes and a Dialog title. 28 */ 29 RBPropertiesImporter(String title, RBManager rbm, RBManagerGUI gui)30 public RBPropertiesImporter(String title, RBManager rbm, RBManagerGUI gui) { 31 super(title, rbm, gui); 32 } 33 setupFileChooser()34 protected void setupFileChooser() { 35 chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){ 36 public boolean accept(File f) { 37 if (f.isDirectory()) return true; 38 if (f.getName().toLowerCase().endsWith(".properties") && f.getName().indexOf("_") < 0) return true; 39 return false; 40 } 41 42 public String getDescription() { 43 return Resources.getTranslation("import_properties_file_description"); 44 } 45 }); 46 } 47 beginImport()48 protected void beginImport() throws IOException { 49 super.beginImport(); 50 File baseFile = getChosenFile(); 51 FileReader fr = new FileReader(baseFile); 52 BufferedReader br = new BufferedReader(fr); 53 54 // Test if this is an RBManager generated file or not 55 int count = 0; 56 String line = null; 57 isRBMFile = true; 58 while ((line = br.readLine()) != null) { 59 if (!line.trim().equals("")) count++; 60 if (count == 1 && !line.startsWith("# @file")) { 61 // Not generated by RBManager 62 isRBMFile = false; 63 } 64 } // end while 65 if (isRBMFile) { 66 // Treat the file as generated by RBManager 67 // Parse the resource bundle through RBManager 68 RBManager import_rbm = new RBManager(baseFile); 69 // Merge the two resource bundles 70 Vector bundles = import_rbm.getBundles(); 71 Vector encodings = new Vector(); 72 for (int i=0; i < bundles.size(); i++) { 73 Bundle b = (Bundle)bundles.elementAt(i); 74 encodings.addElement(b.encoding); 75 } 76 resolveEncodings(encodings); 77 for (int i=0; i < bundles.size(); i++) { 78 Bundle b = (Bundle)bundles.elementAt(i); 79 Enumeration keys = b.allItems.keys(); 80 while (keys.hasMoreElements()) { 81 String key = (String)keys.nextElement(); 82 BundleItem item = (BundleItem)b.allItems.get(key); 83 importResource(item, b.encoding, (item.getParentGroup() == null ? getDefaultGroup(): item.getParentGroup().getName())); 84 } 85 } 86 } else { 87 // Just treat it as a regular properties file 88 // Check if there are any missing target locale files 89 String baseName = baseFile.getName().substring(0,baseFile.getName().length()-11); // |'.properties'| == 11 90 File baseDir = new File(baseFile.getParent()); 91 String allChildren[] = baseDir.list(); 92 Vector children_v = new Vector(); 93 for (int i=0; i < allChildren.length; i++) { 94 if (allChildren[i].startsWith(baseName) && allChildren[i].toLowerCase().endsWith(".properties")) { 95 if (allChildren[i].length() == (baseName + ".properties").length()) children_v.addElement(""); 96 else children_v.addElement(allChildren[i].substring(baseName.length()+1, allChildren[i].indexOf(".properties"))); 97 } 98 } 99 showProgressBar(children_v.size()); 100 resolveEncodings(children_v); 101 // Run through each source locale file importing as necessary 102 for (int i=0; i < children_v.size(); i++) { 103 Properties p = new Properties(); 104 FileInputStream fis = new FileInputStream(new File(baseDir, baseName + 105 (children_v.elementAt(i).toString().equals("") ? "" : "_" + children_v.elementAt(i).toString()) + 106 ".properties")); 107 p.load(fis); 108 Enumeration keys = p.keys(); 109 while (keys.hasMoreElements()) { 110 String key = (String)keys.nextElement(); 111 BundleItem item = new BundleItem(null, key, p.getProperty(key)); 112 item.setTranslated(this.getDefaultTranslated()); 113 importResource(item, children_v.elementAt(i).toString(), getDefaultGroup()); 114 } 115 incrementProgressBar(); 116 } 117 hideProgressBar(); 118 } 119 } 120 }