Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 105   Methods: 3
NCLOC: 55   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConfigWriter.java 100% 93,1% 100% 95%
coverage coverage
 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 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  3 public static ConfigWriter getInstance() {
 43  3 if (m_cWriter == null) {
 44  1 m_cWriter = new ConfigWriter();
 45    }
 46  3 return m_cWriter;
 47    }
 48   
 49    /**
 50    * Constructor for the ConfigWriter Singleton
 51    *
 52    * @author Siddhartha Azad
 53    * @since 2.3
 54    */
 55  1 private ConfigWriter() {
 56  1 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  1 public void write(final IConfigInfo a_cInfo) {
 67  1 ConfigData cd = a_cInfo.getConfigData();
 68  1 String nsPrefix = cd.getNS() + ".";
 69  1 String name;
 70  1 List values;
 71    // construct name-value pairs from the information in the lists
 72  1 for (int i = 0; i < cd.getNumLists(); i++) {
 73  3 name = cd.getListNameAt(i);
 74  3 values = cd.getListValuesAt(i);
 75  3 int idx = 0;
 76  3 for (Iterator iter = values.iterator(); iter.hasNext(); idx++) {
 77    // append an index for same key elements
 78  9 String tmpName = name + "[" + idx + "]";
 79  9 tmpName = nsPrefix + tmpName;
 80  9 m_config.setProperty(tmpName, (String) iter.next());
 81    }
 82    }
 83  1 String value = "", tmpName = "";
 84  1 for (int i = 0; i < cd.getNumTexts(); i++) {
 85  3 name = cd.getTextNameAt(i);
 86  3 value = cd.getTextValueAt(i);
 87  3 tmpName = nsPrefix + name;
 88  3 m_config.setProperty(tmpName, value);
 89    }
 90  1 try {
 91  1 FileOutputStream out = new FileOutputStream(a_cInfo.getFileName());
 92  1 try {
 93  1 m_config.store(out, "---JGAP Configuration File---");
 94    } finally {
 95  1 out.close();
 96    }
 97    } catch (IOException ioEx) {
 98  0 JOptionPane.showMessageDialog(null,
 99    "Exception " + ioEx.getMessage(),
 100    "Configuration Exception",
 101    JOptionPane.INFORMATION_MESSAGE);
 102  0 ioEx.printStackTrace();
 103    }
 104    }
 105    }