KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > data > config > ConfigWriter


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.data.config;
11
12 import java.util.*;
13 import java.io.*;
14 import javax.swing.*;
15
16 /**
17  * This class is a Singleton that generates a properties file from
18  * classes implementing IConfigInfo.
19  *
20  * @author Siddhartha Azad
21  * @since 2.3
22  * */

23 public final class ConfigWriter {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
26
27   /**
28    * Singleton instance of ConfigWriter
29    */

30   private static ConfigWriter m_cWriter;
31
32   // The configuration stored as Properties
33
private Properties m_config;
34
35   /**
36    * Method to create and access the Singleton ConfigWriter instance.
37    * @return a ConfigWriter Singleton instance.
38    *
39    * @author Siddhartha Azad
40    * @since 2.3
41    * */

42   public static ConfigWriter getInstance() {
43     if (m_cWriter == null) {
44       m_cWriter = new ConfigWriter();
45     }
46     return m_cWriter;
47   }
48
49   /**
50    * Constructor for the ConfigWriter Singleton
51    *
52    * @author Siddhartha Azad
53    * @since 2.3
54    */

55   private ConfigWriter() {
56     m_config = new Properties();
57   }
58
59   /**
60    * Persist the configuration information as selected by the user.
61    * @param a_cInfo configuration Information to persist
62    *
63    * @author Siddhartha Azad
64    * @since 2.3
65    * */

66   public void write(final IConfigInfo a_cInfo) {
67     ConfigData cd = a_cInfo.getConfigData();
68     String JavaDoc nsPrefix = cd.getNS() + ".";
69     String JavaDoc name;
70     List values;
71     // construct name-value pairs from the information in the lists
72
for (int i = 0; i < cd.getNumLists(); i++) {
73       name = cd.getListNameAt(i);
74       values = cd.getListValuesAt(i);
75       int idx = 0;
76       for (Iterator iter = values.iterator(); iter.hasNext(); idx++) {
77         // append an index for same key elements
78
String JavaDoc tmpName = name + "[" + idx + "]";
79         tmpName = nsPrefix + tmpName;
80         m_config.setProperty(tmpName, (String JavaDoc) iter.next());
81       }
82     }
83     String JavaDoc value = "", tmpName = "";
84     for (int i = 0; i < cd.getNumTexts(); i++) {
85       name = cd.getTextNameAt(i);
86       value = cd.getTextValueAt(i);
87       tmpName = nsPrefix + name;
88       m_config.setProperty(tmpName, value);
89     }
90     try {
91       FileOutputStream out = new FileOutputStream(a_cInfo.getFileName());
92       try {
93         m_config.store(out, "---JGAP Configuration File---");
94       } finally {
95         out.close();
96       }
97     } catch (IOException ioEx) {
98       JOptionPane.showMessageDialog(null,
99                                     "Exception " + ioEx.getMessage(),
100                                     "Configuration Exception",
101                                     JOptionPane.INFORMATION_MESSAGE);
102       ioEx.printStackTrace();
103     }
104   }
105 }
106
Popular Tags