KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > css > IniFile


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.css;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * Reads config files with multiple sections. Example:
35  * <pre>
36  * var1 = foo
37  *
38  * [section1]
39  * ; some comment
40  * var1 = 123
41  * var2 = false
42  *
43  * [section2]
44  * var1 = something ; another comment
45  * var2 = true
46  * </pre>
47  * <p>
48  * Values that are defined before the first section is declared, will
49  * automatically be placed in a section called 'global'.
50  * </p>
51  * <p>
52  * The strings 'true' and 'false' are converted to java.lang.Boolean, whereas
53  * strings that can be parsed as integer value are converted to java.lang.Integer.
54  * All other values remain unmodified.
55  * </p>
56  */

57 public class IniFile {
58
59     public static final String JavaDoc GLOBAL_SECTION = "global";
60     
61     private File JavaDoc file;
62     
63     HashMap JavaDoc sections = new HashMap JavaDoc();
64     
65     Map JavaDoc section;
66     
67     private long lastModified;
68     
69     public IniFile(File JavaDoc file) throws IOException JavaDoc {
70         this.file = file;
71         load();
72     }
73
74     public long lastModified() {
75         return file.lastModified();
76     }
77     
78     public synchronized Map JavaDoc getSections() {
79         if (file.lastModified() > lastModified) {
80             try {
81                 load();
82             }
83             catch (IOException JavaDoc e) {
84                 throw new RuntimeException JavaDoc(e);
85             }
86         }
87         return sections;
88     }
89     
90     private synchronized void load() throws IOException JavaDoc {
91         lastModified = file.lastModified();
92         sections.clear();
93         setSection(GLOBAL_SECTION);
94         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
95         for (String JavaDoc ln = br.readLine(); ln != null; ln = br.readLine()) {
96             ln = ln.trim();
97             if (ln.length() > 0 && ln.charAt(0) != ';') {
98                 if (ln.charAt(0) != '[' || ln.indexOf(']') == -1) {
99                     int index = ln.indexOf(';');
100                     if (index != -1) {
101                         ln = ln.substring(0, index).trim();
102                     }
103                     index = ln.indexOf('=');
104                     if (index != -1) {
105                         String JavaDoc key = ln.substring(0, index).trim();
106                         String JavaDoc value = ln.substring(index + 1).trim();
107                         section.put(key, convertString(value));
108                     }
109                 }
110                 else {
111                     setSection(ln.substring(1, ln.indexOf(']')).trim());
112                 }
113             }
114         }
115     }
116     
117     private void setSection(String JavaDoc name) {
118         section = (Map JavaDoc) sections.get(name);
119         if (section == null) {
120             section = new HashMap JavaDoc();
121             sections.put(name, section);
122         }
123     }
124     
125     private Object JavaDoc convertString(String JavaDoc s) {
126         if (s.equals("true")) {
127             return Boolean.TRUE;
128         }
129         if (s.equals("false")) {
130             return Boolean.FALSE;
131         }
132         try {
133             return Integer.valueOf(s);
134         }
135         catch (NumberFormatException JavaDoc e) {
136         }
137         return s;
138     }
139
140 }
141
Popular Tags