michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.util; michael@0: michael@0: import java.io.BufferedReader; michael@0: import java.io.BufferedWriter; michael@0: import java.io.File; michael@0: import java.io.FileNotFoundException; michael@0: import java.io.FileReader; michael@0: import java.io.FileWriter; michael@0: import java.io.IOException; michael@0: import java.util.Enumeration; michael@0: import java.util.Hashtable; michael@0: michael@0: public final class INIParser extends INISection { michael@0: // default file to read and write to michael@0: private File mFile = null; michael@0: michael@0: // List of sections in the current iniFile. null if the file has not been parsed yet michael@0: private Hashtable mSections = null; michael@0: michael@0: // create a parser. The file will not be read until you attempt to michael@0: // access sections or properties inside it. At that point its read synchronously michael@0: public INIParser(File iniFile) { michael@0: super(""); michael@0: mFile = iniFile; michael@0: } michael@0: michael@0: // write ini data to the default file. Will overwrite anything current inside michael@0: public void write() { michael@0: writeTo(mFile); michael@0: } michael@0: michael@0: // write to the specified file. Will overwrite anything current inside michael@0: public void writeTo(File f) { michael@0: if (f == null) michael@0: return; michael@0: michael@0: FileWriter outputStream = null; michael@0: try { michael@0: outputStream = new FileWriter(f); michael@0: } catch (IOException e1) { michael@0: e1.printStackTrace(); michael@0: } michael@0: michael@0: BufferedWriter writer = new BufferedWriter(outputStream); michael@0: try { michael@0: write(writer); michael@0: writer.close(); michael@0: } catch (IOException e) { michael@0: e.printStackTrace(); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void write(BufferedWriter writer) throws IOException { michael@0: super.write(writer); michael@0: michael@0: if (mSections != null) { michael@0: for (Enumeration e = mSections.elements(); e.hasMoreElements();) { michael@0: INISection section = e.nextElement(); michael@0: section.write(writer); michael@0: writer.newLine(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // return all of the sections inside this file michael@0: public Hashtable getSections() { michael@0: if (mSections == null) { michael@0: try { michael@0: parse(); michael@0: } catch (IOException e) { michael@0: debug("Error parsing: " + e); michael@0: } michael@0: } michael@0: return mSections; michael@0: } michael@0: michael@0: // parse the default file michael@0: @Override michael@0: protected void parse() throws IOException { michael@0: super.parse(); michael@0: parse(mFile); michael@0: } michael@0: michael@0: // parse a passed in file michael@0: private void parse(File f) throws IOException { michael@0: // Set up internal data members michael@0: mSections = new Hashtable(); michael@0: michael@0: if (f == null || !f.exists()) michael@0: return; michael@0: michael@0: FileReader inputStream = null; michael@0: try { michael@0: inputStream = new FileReader(f); michael@0: } catch (FileNotFoundException e1) { michael@0: // If the file doesn't exist. Just return; michael@0: return; michael@0: } michael@0: michael@0: BufferedReader buf = new BufferedReader(inputStream); michael@0: String line = null; // current line of text we are parsing michael@0: INISection currentSection = null; // section we are currently parsing michael@0: michael@0: while ((line = buf.readLine()) != null) { michael@0: michael@0: if (line != null) michael@0: line = line.trim(); michael@0: michael@0: // blank line or a comment. ignore it michael@0: if (line == null || line.length() == 0 || line.charAt(0) == ';') { michael@0: debug("Ignore line: " + line); michael@0: } else if (line.charAt(0) == '[') { michael@0: debug("Parse as section: " + line); michael@0: currentSection = new INISection(line.substring(1, line.length()-1)); michael@0: mSections.put(currentSection.getName(), currentSection); michael@0: } else { michael@0: debug("Parse as property: " + line); michael@0: michael@0: String[] pieces = line.split("="); michael@0: if (pieces.length != 2) michael@0: continue; michael@0: michael@0: String key = pieces[0].trim(); michael@0: String value = pieces[1].trim(); michael@0: if (currentSection != null) { michael@0: currentSection.setProperty(key, value); michael@0: } else { michael@0: mProperties.put(key, value); michael@0: } michael@0: } michael@0: } michael@0: buf.close(); michael@0: } michael@0: michael@0: // add a section to the file michael@0: public void addSection(INISection sect) { michael@0: // ensure that we have parsed the file michael@0: getSections(); michael@0: mSections.put(sect.getName(), sect); michael@0: } michael@0: michael@0: // get a section from the file. will return null if the section doesn't exist michael@0: public INISection getSection(String key) { michael@0: // ensure that we have parsed the file michael@0: getSections(); michael@0: return mSections.get(key); michael@0: } michael@0: michael@0: // remove an entire section from the file michael@0: public void removeSection(String name) { michael@0: // ensure that we have parsed the file michael@0: getSections(); michael@0: mSections.remove(name); michael@0: } michael@0: michael@0: // rename a section; nuking any previous section with the new michael@0: // name in the process michael@0: public void renameSection(String oldName, String newName) { michael@0: // ensure that we have parsed the file michael@0: getSections(); michael@0: michael@0: mSections.remove(newName); michael@0: INISection section = mSections.get(oldName); michael@0: if (section == null) michael@0: return; michael@0: michael@0: section.setName(newName); michael@0: mSections.remove(oldName); michael@0: mSections.put(newName, section); michael@0: } michael@0: }