01: /*
02: *****************************************************************************
03: * Copyright (C) 2000-2004, International Business Machines Corporation and *
04: * others. All Rights Reserved. *
05: *****************************************************************************
06: */
07: package com.ibm.rbm;
08:
09: import java.io.*;
10: import javax.swing.*;
11: import java.util.*;
12:
13: /**
14: * This class provides a plug-in exporter utility for RBManager that outputs Java
15: * standard .properties files in the according to the file structure of Resource
16: * Bundles. Most all meta-data is lost in this export.
17: *
18: * @author Jared Jackson
19: * @see com.ibm.rbm.RBManager
20: */
21: public class RBPropertiesExporter extends RBExporter {
22:
23: public RBPropertiesExporter() {
24: super ();
25:
26: // Initialize the file chooser if necessary
27: if (chooser == null) {
28: chooser = new JFileChooser();
29: chooser
30: .setFileFilter(new javax.swing.filechooser.FileFilter() {
31: public String getDescription() {
32: return "Base Class Properties Files";
33: }
34:
35: public boolean accept(File f) {
36: if (f.isDirectory())
37: return true;
38: String name = f.getName();
39: if (name.toLowerCase().endsWith(
40: ".properties")
41: && f.getName().indexOf("_") < 0)
42: return true;
43: return false;
44: }
45: });
46: } // end if
47: }
48:
49: public void export(RBManager rbm) throws IOException {
50: if (rbm == null)
51: return;
52: // Open the Save Dialog
53: int ret_val = chooser.showSaveDialog(null);
54: if (ret_val != JFileChooser.APPROVE_OPTION)
55: return;
56: // Retrieve basic file information
57: File file = chooser.getSelectedFile(); // The file(s) we will be working with
58: File directory = new File(file.getParent()); // The directory we will be writing to
59: String base_name = file.getName(); // The base name of the files we will write
60: if (base_name == null || base_name.equals(""))
61: base_name = rbm.getBaseClass();
62: if (base_name.toLowerCase().endsWith(".properties"))
63: base_name = base_name.substring(0, base_name.length() - 11);
64:
65: Vector bundle_v = rbm.getBundles();
66: for (int i = 0; i < bundle_v.size(); i++) {
67: Properties prop = new Properties();
68: Bundle bundle = (Bundle) bundle_v.elementAt(i);
69: String base_enc = base_name;
70: if (bundle.encoding != null && !bundle.encoding.equals(""))
71: base_enc = base_enc + "_" + bundle.encoding;
72: String file_name = base_enc + ".properties";
73: String header = "Resource Bundle: "
74: + file_name
75: + " - File automatically generated by RBManager at "
76: + (new Date());
77:
78: Vector group_v = bundle.getGroupsAsVector();
79: for (int j = 0; j < group_v.size(); j++) {
80: BundleGroup group = (BundleGroup) group_v.elementAt(j);
81: Vector item_v = group.getItemsAsVector();
82: for (int k = 0; k < item_v.size(); k++) {
83: BundleItem item = (BundleItem) item_v.elementAt(k);
84: prop.setProperty(item.getKey(), item
85: .getTranslation());
86: } // end for - k
87: } // end for - j
88:
89: // Write out the file
90: File write_file = new File(directory, file_name);
91: FileOutputStream fos = new FileOutputStream(write_file);
92: prop.store(fos, header);
93: } // end for - i
94: }
95: }
|