//import java.io.BufferedReader; //import java.io.BufferedWriter; //import java.io.File; //import java.io.FileReader; //import java.io.FileWriter; //import java.io.PrintWriter; //import java.util.LinkedList; //import java.util.TreeMap; // Read an ini file (as outlined below) and allow the value to be // retrieved for a particular section/key pair // // ini file format: // // [sectionA] // keyAA=valueAA // keyAB=valueAB // // [sectionB] // keyBA=valueBA // keyBB=valueBB // // Internal layout // The IniFileInfo class contains a TreeMap and a LinkedList. the map // contains the ini [section] names as the map's key and an Integer that // points to the corresponding Section object, stored in the List. // // A Section object also contains a TreeMap and a LinkedList. Here, the // map holds the individual ini file's 'key' entries as the map's key and // holds an Integer as the map's value that points to the Key object in // the List. // public class IniFileInfo { private long filedate = 0L; private java.io.File iniFile = null; private java.util.LinkedList sectionList = new java.util.LinkedList(); private java.util.TreeMap sectionMap = new java.util.TreeMap(); public IniFileInfo(java.io.File iniFile) { this.iniFile = iniFile; refresh(); } public IniFileInfo(String filename) { this(new java.io.File(filename)); } public static void main(String[] args) { // main is used for testing only IniFileInfo iniFileA = new IniFileInfo(new java.io.File("e:/test.ini")); iniFileA.setKeyValue("A", "ccc", "new value"); iniFileA.setKeyValue("testme", "testkey", "testvalue"); } private Section addSection(String strSection) { Section section = null; if (!sectionMap.containsKey(strSection)) { section = new Section(strSection); sectionList.addLast(section); int index = sectionList.size() - 1; if (index < 0) { index = 0; } sectionMap.put(strSection, new Integer(index)); //result = true; } return section; } public String getKeyValue(String strSection, String strKey) { // Returns a specific value for a section/key pair // getKeyValue() first checks if the ini file has been changed since // the last time sectMap was built. If so, it calls refresh() String value = ""; strSection = strSection.trim(); strKey = strKey.trim(); if (strSection.equals("") || strKey.equals("")) { return ""; } if (hasFileChanged()) { refresh(); } if (sectionMap != null && sectionMap.containsKey(strSection)) { // get the section index from the TreeMap int index = ((Integer)sectionMap.get(strSection)).intValue(); // get the Section object from the sectionList Section section = (Section)sectionList.get(index); // find the key from the Section object's TreeMap value = section.getKeyValue(strKey); } if (value == null) { value = ""; } return value; } private Section getSection(String strSection) { // returns a Section object for a particular 'section' of the ini file Section section = null; if (sectionMap != null && sectionMap.containsKey(strSection)) { // get the section index from the TreeMap int index = ((Integer)sectionMap.get(strSection)).intValue(); // get the Section object from the sectionList section = (Section)sectionList.get(index); } return section; } public boolean hasKey(String strSection, String strKey) { boolean result = false; if (strSection == null) { strSection = ""; } if (strKey == null) { strKey = ""; } strSection = strSection.trim(); strKey = strKey.trim(); if (hasFileChanged()) { refresh(); } if (strSection != "" && strKey != "") { Section section = getSection(strSection); if (section != null) { Key key = section.getKey(strKey); if (key != null) { result = true; } else { result = false; } } } return result; } public boolean hasSection(String strSection) { boolean result = false; if (strSection == null) { strSection = ""; } strSection = strSection.trim(); if (hasFileChanged()) { refresh(); } Section section = getSection(strSection); if (section != null) { result = true; } else { result = false; } return result; } private boolean hasFileChanged() { long fdate = iniFile.lastModified(); return fdate != filedate; } private String iniToText(int style) { StringBuffer text = new StringBuffer(); StringBuffer sectText = new StringBuffer(); if (sectionList != null && sectionList.size() > 0) { for (int index = 0; index < sectionList.size(); index++) { sectText.setLength(0); Section section = (Section)sectionList.get(index); text.append("[" + section.name + "]\n"); int numOfKeys = section.numOfKeys(); for (int keysIndex = 0; keysIndex < numOfKeys; keysIndex++) { Key theKey = section.getKey(keysIndex); String key = theKey.getKey(); String value = theKey.getValue(); if (style == 0) { sectText.append(" " + key + "=" + value + "\n"); } if (style == 1) { sectText.append(key + "=" + value + "\n"); } } text.append(sectText); } } return new String(text); } private void refresh() { // Loads the [section] key/value pairs into the sectionMap object. // refresh() purges sectionMap first and then repopulates it int index = 0; String input = ""; String value = ""; java.io.BufferedReader in = null; Key key = null; Section section = null; filedate = iniFile.lastModified(); if (sectionMap != null) { sectionMap.clear(); } if (sectionList != null) { sectionList.clear(); } try { in = new java.io.BufferedReader(new java.io.FileReader(iniFile)); while ((input = in.readLine()) != null) { String strSection = ""; String strKey = ""; input = input.trim(); // don't want to store "empty" keys, then uncomment line below //if (input.equals("")) continue; // is this a "section"? denoted by being in brackets ie [xxxx] if (input.indexOf('[') == 0 && input.lastIndexOf(']') == (input.length() - 1)) { strSection = input.substring(1, input.length() - 1); strSection = strSection.trim(); if (!strSection.equals("")) { if (sectionMap.containsKey(strSection)) { index = ((Integer)sectionMap.get(strSection)).intValue(); } if (!sectionMap.containsKey(strSection)) { section = new Section(strSection); sectionList.addLast(section); index = sectionList.size() - 1; sectionMap.put(strSection, new Integer(index)); } } } else { if (section != null) { strKey = ""; value = ""; int equalPos; equalPos = input.indexOf('='); if (!section.equals("")) { if (equalPos > -1) { strKey = input.substring(0, equalPos); } if (equalPos < 0) { strKey = input; } strKey = strKey.trim(); if (strKey.equals("")) { strKey = " "; } if (equalPos > -1) { value = input.substring(equalPos + 1, input.length()); value = value.trim(); } if (equalPos < 0) { value = null; } if (!strKey.equals("")) { section.setKeyValue(strKey, value); } } } } } } catch (java.io.IOException e ) { sectionMap = null; } try { if (in != null) { in.close(); } } catch (java.io.IOException e) { } } public boolean setKeyValue(String strSection, String strKey, String value) { boolean status = false; java.io.PrintWriter out = null; if (strSection == null) { strSection = ""; } if (strKey == null) { strKey = ""; } if (value == null) { value = ""; } strSection = strSection.trim(); strKey = strKey.trim(); value = value.trim(); if (strSection.equals("") || strKey.equals("")) { return false; } if (hasFileChanged()) { refresh(); } try { out = new java.io.PrintWriter( new java.io.BufferedWriter(new java.io.FileWriter(iniFile))); if (!out.checkError()) { Section section = null; // if sectMap contain the section if (sectionMap.containsKey(strSection)) { section = getSection(strSection); } else { // create new section in sectionMap section = addSection(strSection); } // add key/value to the Section object section.setKeyValue(strKey, value); for (int index = 0; index < sectionList.size(); index++) { //sectText.setLength(0); section = (Section)sectionList.get(index); out.println("[" + section.name + "]"); int numOfKeys = section.numOfKeys(); for (int keysIndex = 0; keysIndex < numOfKeys; keysIndex++) { Key theKey = section.getKey(keysIndex); String key = theKey.getKey(); String val = theKey.getValue(); if (!(key.trim().equals("")) && val != null) { out.println(key + "=" + val); } if (key.trim().equals("") && val == null) { out.println(""); } } } } } catch (java.io.IOException e) { out = null; } if (out != null) { out.close(); } return status; } public String toString() { return "Ini file: " + iniFile + "\n" + iniToText(0); } private class Key { private String key; private String value; private Key(String key, String value) { this.key = key; this.value = value; } private String getKey() { return key; } private String getValue() { return value; } public String toString() { return ("key:" + key + " value:" + value); } } private class Section { // keysMap holds the 'keys' in the ini file's [section]. It is used // for quick lookup like an index/cross reference. // key = the key, value = Integer for the position in the List private String name; private java.util.LinkedList keys = new java.util.LinkedList(); private java.util.LinkedList values = new java.util.LinkedList(); private java.util.TreeMap keysMap = new java.util.TreeMap(); private Section(String name) { this.name = name; } private Key getKey(int index) { // returns a Key object for a particular key index Key theKey = null; if (keys.size() > 0 && index < keys.size()) { String key = (String)keys.get(index); String value = (String)values.get(index); theKey = new Key(key, value); } return theKey; } private Key getKey(String key) { // returns a Key object for a particular key string Key theKey = null; if (keysMap.containsKey(key)) { int index = ((Integer)keysMap.get(key)).intValue(); theKey = getKey(index); //String value = (String)values.get(index); //theKey = new Key(key, value); } return theKey; } private String getKeyValue(String key) { String value = ""; int index = keyIndex(key); if (index > -1) { value = (String)values.get(keyIndex(key)); } return value; } private int keyIndex(String key) { int index = -1; if (keysMap.containsKey(key)) { index = ((Integer)keysMap.get(key)).intValue(); } return index; } private int numOfKeys() { return values.size(); } private void setKeyValue(String key, String value) { // find the index for this key // if index == -1 then key not in TreeMap int index = keyIndex(key); // update value to values List if the key is already in the TreeMap if (index > -1) { values.set(index, value); } // if the key is not in the TreeMap, then add the value // to the values List and the keys List, and then use the index // from the values List to add the key to the Map if (index == -1) { index = values.size(); values.addLast(value); keysMap.put(key, new Integer(index)); keys.addLast(key); } } public String toString() { return ("Section: " + name + " #ofKeys: " + numOfKeys()); } } }