Back to index

lightning-sunbird  0.9+nobinonly
INIParser.java
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Java XPCOM Bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Javier Pedemonte (jhpedemonte@gmail.com)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 package org.mozilla.xpcom;
00039 
00040 import java.io.BufferedReader;
00041 import java.io.File;
00042 import java.io.FileInputStream;
00043 import java.io.FileNotFoundException;
00044 import java.io.IOException;
00045 import java.io.InputStreamReader;
00046 import java.nio.charset.Charset;
00047 import java.util.Enumeration;
00048 import java.util.HashMap;
00049 import java.util.Iterator;
00050 import java.util.Properties;
00051 import java.util.StringTokenizer;
00052 
00053 
00057 public class INIParser {
00058 
00059   private HashMap mSections;
00060 
00071   public INIParser(String aFilename, Charset aCharset)
00072           throws FileNotFoundException, IOException {
00073     initFromFile(new File(aFilename), aCharset);
00074   }
00075 
00084   public INIParser(String aFilename) throws FileNotFoundException, IOException {
00085     initFromFile(new File(aFilename), Charset.forName("UTF-8"));
00086   }
00087 
00097   public INIParser(File aFile, Charset aCharset)
00098           throws FileNotFoundException, IOException {
00099     initFromFile(aFile, aCharset);
00100   }
00101 
00110   public INIParser(File aFile) throws FileNotFoundException, IOException {
00111     initFromFile(aFile, Charset.forName("UTF-8"));
00112   }
00113 
00122   private void initFromFile(File aFile, Charset aCharset)
00123           throws FileNotFoundException, IOException {
00124     FileInputStream fileStream = new FileInputStream(aFile);
00125     InputStreamReader inStream = new InputStreamReader(fileStream, aCharset);
00126     BufferedReader reader = new BufferedReader(inStream);
00127 
00128     mSections = new HashMap();
00129     String currSection = null;
00130 
00131     String line;
00132     while ((line = reader.readLine()) != null) {
00133       // skip empty lines and comment lines
00134       String trimmedLine = line.trim();
00135       if (trimmedLine.length() == 0 || trimmedLine.startsWith("#")
00136               || trimmedLine.startsWith(";")) {
00137         continue;
00138       }
00139 
00140       // Look for section headers (i.e. "[Section]").
00141       if (line.startsWith("[")) {
00142         /*
00143          * We are looking for a well-formed "[Section]".  If this header is
00144          * malformed (i.e. "[Section" or "[Section]Moretext"), just skip it
00145          * and go on to next well-formed section header.
00146          */
00147         if (!trimmedLine.endsWith("]") ||
00148             trimmedLine.indexOf("]") != (trimmedLine.length() - 1)) {
00149           currSection = null;
00150           continue;
00151         }
00152 
00153         // remove enclosing brackets
00154         currSection = trimmedLine.substring(1, trimmedLine.length() - 1);
00155         continue;
00156       }
00157 
00158       // If we haven't found a valid section header, continue to next line
00159       if (currSection == null) {
00160         continue;
00161       }
00162 
00163       StringTokenizer tok = new StringTokenizer(line, "=");
00164       if (tok.countTokens() != 2) { // looking for value pairs
00165         continue;
00166       }
00167 
00168       Properties props = (Properties) mSections.get(currSection);
00169       if (props == null) {
00170         props = new Properties();
00171         mSections.put(currSection, props);
00172       }
00173       props.setProperty(tok.nextToken(), tok.nextToken());
00174     }
00175 
00176     reader.close();
00177   }
00178 
00184   public Iterator getSections() {
00185     return mSections.keySet().iterator();
00186   }
00187 
00195   public Iterator getKeys(String aSection) {
00196     /*
00197      * Simple wrapper class to convert Enumeration to Iterator
00198      */
00199     class PropertiesIterator implements Iterator {
00200       private Enumeration e;
00201 
00202       public PropertiesIterator(Enumeration aEnum) {
00203         e = aEnum;
00204       }
00205 
00206       public boolean hasNext() {
00207         return e.hasMoreElements();
00208       }
00209 
00210       public Object next() {
00211         return e.nextElement();
00212       }
00213 
00214       public void remove() {
00215         return;
00216       }
00217     }
00218 
00219     Properties props = (Properties) mSections.get(aSection);
00220     if (props == null) {
00221       return null;
00222     }
00223 
00224     return new PropertiesIterator(props.propertyNames());
00225   }
00226 
00234   public String getString(String aSection, String aKey) {
00235     Properties props = (Properties) mSections.get(aSection);
00236     if (props == null) {
00237       return null;
00238     }
00239 
00240     return props.getProperty(aKey);
00241   }
00242 
00243 }
00244