1/*
2 *****************************************************************************
3 * Copyright (C) 2000-2004, International Business Machines Corporation and  *
4 * others. All Rights Reserved.                                              *
5 *****************************************************************************
6 */
7package com.ibm.rbm;
8
9import java.io.*;
10import javax.swing.*;
11import 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 */
21public 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.setFileFilter(new javax.swing.filechooser.FileFilter(){
30                public String getDescription() {
31                    return "Base Class Properties Files";
32                }
33                public boolean accept(File f) {
34                    if (f.isDirectory()) return true;
35                    String name = f.getName();
36                    if (name.toLowerCase().endsWith(".properties") && f.getName().indexOf("_") < 0) return true;
37                    return false;
38                }
39            });
40        } // end if
41    }
42
43    public void export(RBManager rbm) throws IOException {
44        if (rbm == null) return;
45        // Open the Save Dialog
46        int ret_val = chooser.showSaveDialog(null);
47        if (ret_val != JFileChooser.APPROVE_OPTION) return;
48        // Retrieve basic file information
49        File file = chooser.getSelectedFile();                  // The file(s) we will be working with
50        File directory = new File(file.getParent());            // The directory we will be writing to
51        String base_name = file.getName();                      // The base name of the files we will write
52        if (base_name == null || base_name.equals("")) base_name = rbm.getBaseClass();
53        if (base_name.toLowerCase().endsWith(".properties"))
54            base_name = base_name.substring(0,base_name.length()-11);
55
56        Vector bundle_v = rbm.getBundles();
57        for (int i=0; i < bundle_v.size(); i++) {
58            Properties prop = new Properties();
59            Bundle bundle = (Bundle)bundle_v.elementAt(i);
60            String base_enc = base_name;
61            if (bundle.encoding != null && !bundle.encoding.equals("")) base_enc = base_enc + "_" + bundle.encoding;
62            String file_name = base_enc + ".properties";
63            String header = "Resource Bundle: " + file_name + " - File automatically generated by RBManager at " + (new Date());
64
65            Vector group_v = bundle.getGroupsAsVector();
66            for (int j=0; j < group_v.size(); j++) {
67                BundleGroup group = (BundleGroup)group_v.elementAt(j);
68                Vector item_v = group.getItemsAsVector();
69                for (int k=0; k < item_v.size(); k++) {
70                    BundleItem item = (BundleItem)item_v.elementAt(k);
71                    prop.setProperty(item.getKey(), item.getTranslation());
72                } // end for - k
73            } // end for - j
74
75            // Write out the file
76            File write_file = new File(directory, file_name);
77            FileOutputStream fos = new FileOutputStream(write_file);
78            prop.store(fos, header);
79        } // end for - i
80    }
81}