Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
org.mozilla.xpcom.INIParser Class Reference

A simple parser for INI files. More...

Collaboration diagram for org.mozilla.xpcom.INIParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 INIParser (String aFilename, Charset aCharset) throws FileNotFoundException, IOException
 Creates a new INIParser instance from the INI file at the given path.
 INIParser (String aFilename) throws FileNotFoundException, IOException
 Creates a new INIParser instance from the INI file at the given path, which is assumed to be in the UTF-8 charset.
 INIParser (File aFile, Charset aCharset) throws FileNotFoundException, IOException
 Creates a new INIParser instance from the given file.
 INIParser (File aFile) throws FileNotFoundException, IOException
 Creates a new INIParser instance from the given file, which is assumed to be in the UTF-8 charset.
Iterator getSections ()
 Returns an iterator over the section names available in the INI file.
Iterator getKeys (String aSection)
 Returns an iterator over the keys available within a section.
String getString (String aSection, String aKey)
 Gets the string value for a particular section and key.

Private Member Functions

void initFromFile (File aFile, Charset aCharset) throws FileNotFoundException, IOException
 Parses given INI file.

Private Attributes

HashMap mSections

Detailed Description

A simple parser for INI files.

Definition at line 57 of file INIParser.java.


Constructor & Destructor Documentation

org.mozilla.xpcom.INIParser.INIParser ( String  aFilename,
Charset  aCharset 
) throws FileNotFoundException, IOException [inline]

Creates a new INIParser instance from the INI file at the given path.

aCharset specifies the character encoding of the file.

Parameters:
aFilenamepath of INI file to parse
aCharsetcharacter encoding of file
Exceptions:
FileNotFoundExceptionif aFilename does not exist.
IOExceptionif there is a problem reading the given file.

Definition at line 71 of file INIParser.java.

                                                    {
    initFromFile(new File(aFilename), aCharset);
  }

Here is the call graph for this function:

org.mozilla.xpcom.INIParser.INIParser ( String  aFilename) throws FileNotFoundException, IOException [inline]

Creates a new INIParser instance from the INI file at the given path, which is assumed to be in the UTF-8 charset.

Parameters:
aFilenamepath of INI file to parse
Exceptions:
FileNotFoundExceptionif aFilename does not exist.
IOExceptionif there is a problem reading the given file.

Definition at line 84 of file INIParser.java.

                                                                               {
    initFromFile(new File(aFilename), Charset.forName("UTF-8"));
  }

Here is the call graph for this function:

org.mozilla.xpcom.INIParser.INIParser ( File  aFile,
Charset  aCharset 
) throws FileNotFoundException, IOException [inline]

Creates a new INIParser instance from the given file.

aCharset specifies the character encoding of the file.

Parameters:
aFileINI file to parse
aCharsetcharacter encoding of file
Exceptions:
FileNotFoundExceptionif aFile does not exist.
IOExceptionif there is a problem reading the given file.

Definition at line 97 of file INIParser.java.

                                                    {
    initFromFile(aFile, aCharset);
  }

Here is the call graph for this function:

org.mozilla.xpcom.INIParser.INIParser ( File  aFile) throws FileNotFoundException, IOException [inline]

Creates a new INIParser instance from the given file, which is assumed to be in the UTF-8 charset.

Parameters:
aFileINI file to parse
Exceptions:
FileNotFoundExceptionif aFile does not exist.
IOExceptionif there is a problem reading the given file.

Definition at line 110 of file INIParser.java.

                                                                         {
    initFromFile(aFile, Charset.forName("UTF-8"));
  }

Here is the call graph for this function:


Member Function Documentation

Returns an iterator over the keys available within a section.

Parameters:
aSectionsection name whose keys are to be returned
Returns:
an iterator over section keys, or null if no such section exists

Definition at line 195 of file INIParser.java.

                                           {
    /*
     * Simple wrapper class to convert Enumeration to Iterator
     */
    class PropertiesIterator implements Iterator {
      private Enumeration e;

      public PropertiesIterator(Enumeration aEnum) {
        e = aEnum;
      }

      public boolean hasNext() {
        return e.hasMoreElements();
      }

      public Object next() {
        return e.nextElement();
      }

      public void remove() {
        return;
      }
    }

    Properties props = (Properties) mSections.get(aSection);
    if (props == null) {
      return null;
    }

    return new PropertiesIterator(props.propertyNames());
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Returns an iterator over the section names available in the INI file.

Returns:
an iterator over the section names

Definition at line 184 of file INIParser.java.

                                {
    return mSections.keySet().iterator();
  }

Here is the caller graph for this function:

String org.mozilla.xpcom.INIParser.getString ( String  aSection,
String  aKey 
) [inline]

Gets the string value for a particular section and key.

Parameters:
aSectiona section name
aKeythe key whose value is to be returned.
Returns:
string value of particular section and key

Definition at line 234 of file INIParser.java.

                                                        {
    Properties props = (Properties) mSections.get(aSection);
    if (props == null) {
      return null;
    }

    return props.getProperty(aKey);
  }

Here is the caller graph for this function:

void org.mozilla.xpcom.INIParser.initFromFile ( File  aFile,
Charset  aCharset 
) throws FileNotFoundException, IOException [inline, private]

Parses given INI file.

Parameters:
aFileINI file to parse
aCharsetcharacter encoding of file
Exceptions:
FileNotFoundExceptionif aFile does not exist.
IOExceptionif there is a problem reading the given file.

Definition at line 122 of file INIParser.java.

                                                    {
    FileInputStream fileStream = new FileInputStream(aFile);
    InputStreamReader inStream = new InputStreamReader(fileStream, aCharset);
    BufferedReader reader = new BufferedReader(inStream);

    mSections = new HashMap();
    String currSection = null;

    String line;
    while ((line = reader.readLine()) != null) {
      // skip empty lines and comment lines
      String trimmedLine = line.trim();
      if (trimmedLine.length() == 0 || trimmedLine.startsWith("#")
              || trimmedLine.startsWith(";")) {
        continue;
      }

      // Look for section headers (i.e. "[Section]").
      if (line.startsWith("[")) {
        /*
         * We are looking for a well-formed "[Section]".  If this header is
         * malformed (i.e. "[Section" or "[Section]Moretext"), just skip it
         * and go on to next well-formed section header.
         */
        if (!trimmedLine.endsWith("]") ||
            trimmedLine.indexOf("]") != (trimmedLine.length() - 1)) {
          currSection = null;
          continue;
        }

        // remove enclosing brackets
        currSection = trimmedLine.substring(1, trimmedLine.length() - 1);
        continue;
      }

      // If we haven't found a valid section header, continue to next line
      if (currSection == null) {
        continue;
      }

      StringTokenizer tok = new StringTokenizer(line, "=");
      if (tok.countTokens() != 2) { // looking for value pairs
        continue;
      }

      Properties props = (Properties) mSections.get(currSection);
      if (props == null) {
        props = new Properties();
        mSections.put(currSection, props);
      }
      props.setProperty(tok.nextToken(), tok.nextToken());
    }

    reader.close();
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 59 of file INIParser.java.


The documentation for this class was generated from the following file: