Back to index

lightning-sunbird  0.9+nobinonly
TestINIParser.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 import java.io.*;
00039 import java.util.*;
00040 
00041 import org.mozilla.xpcom.INIParser;
00042 
00043 
00044 public class TestINIParser {
00045 
00052   public static void main(String[] args) throws FileNotFoundException,
00053           IOException {
00054     if (args.length > 1) {
00055       System.err.println("TestINIParser [file]");
00056       return;
00057     }
00058 
00059     INIParser parser = null;
00060     File iniFile = null;
00061     File tempFile = null;
00062 
00063     try {
00064       if (args.length == 1) {
00065         iniFile = new File(args[0]);
00066         parser = new INIParser(iniFile);
00067       } else {
00068         tempFile = File.createTempFile("testiniparser", null);
00069         createSampleINIFile(tempFile);
00070         parser = new INIParser(tempFile);
00071       }
00072 
00073       printValidSections(parser);
00074     } finally {
00075       if (tempFile != null) {
00076         tempFile.delete();
00077       }
00078     }
00079   }
00080 
00081   private static void createSampleINIFile(File aFile) throws IOException {
00082     BufferedWriter out = new BufferedWriter(new FileWriter(aFile));
00083     out.write("; first comment\n");
00084     out.write("; second comment\n");
00085     out.newLine();
00086     out.write("[good section 1]\n");
00087     out.write("param1=value1\n");
00088     out.write("param2=value2\n");
00089     out.newLine();
00090     out.write("[invalid section] 1\n");
00091     out.write("blah=blippity-blah\n");
00092     out.newLine();
00093     out.write("[good section 2]       \n");
00094     out.write("param3=value3\n");
00095     out.write("param4=value4\n");
00096     out.newLine();
00097     out.write("param5=value5\n");
00098     out.newLine();
00099     out.write("invalid pair\n");
00100     out.write("this shouldn't appear\n");
00101     out.write("   ; another comment\n");
00102 
00103     out.close();
00104   }
00105 
00106   private static void printValidSections(INIParser aParser) {
00107     Iterator sectionsIter = aParser.getSections();
00108     while (sectionsIter.hasNext()) {
00109       String sectionName = (String) sectionsIter.next();
00110       System.out.println("[" + sectionName + "]");
00111 
00112       Iterator keysIter = aParser.getKeys(sectionName);
00113       while (keysIter.hasNext()) {
00114         String key = (String) keysIter.next();
00115         String value = aParser.getString(sectionName, key);
00116         System.out.println(key + " = " + value);
00117       }
00118 
00119       System.out.println();
00120     }
00121   }
00122 
00123 }
00124