Debian Codegraph @

src/main/java/net/sf/antcontrib/inifile/IniFile.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.inifile;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.Writer;
22 import java.util.*;
23
24
25 /****************************************************************************
26 * Class representing a windows style .ini file.
27 *
28 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
29 *
30 ****************************************************************************/
31
32 public class IniFile
33 {
34 private List sections;
35 private Map sectionMap;
36
37 /***
38 * Create a new IniFile object
39 */
40 public IniFile()
41 {
42 super();
43 this.sections = new ArrayList();
44 this.sectionMap = new HashMap();
45 }
46
47 /***
48 * Gets the List of IniSection objects contained in this IniFile
49 * @return a List of IniSection objects
50 */
51 public List getSections()
52 {
53 return sections;
54 }
55
56
57 /***
58 * Gets the IniSection with the given name
59 * @param name the name of the section
60 */
61 public IniSection getSection(String name)
62 {
63 return (IniSection)sectionMap.get(name);
64 }
65
66 /***
67 * Sets an IniSection object. If a section with the given
68 * name already exists, it is replaced with the passed in section.
69 * @param section The section to set.
70 */
71 public void setSection(IniSection section)
72 {
73 IniSection sec = (IniSection)sectionMap.get(section.getName());
74 if (sec != null)
75 {
76 int idx = sections.indexOf(sec);
77 sections.set(idx, section);
78 }
79 else
80 {
81 sections.add(section);
82 }
83
84 sectionMap.put(section.getName(), section);
85 }
86
87 /***
88 * Removes an entire section from the IniFile
89 * @param name The name of the section to remove
90 */
91 public void removeSection(String name)
92 {
93 IniSection sec = (IniSection)sectionMap.get(name);
94 if (sec != null)
95 {
96 int idx = sections.indexOf(sec);
97 sections.remove(idx);
98 sectionMap.remove(name);
99 }
100 }
101
102 /***
103 * Gets a named property from a specific section
104 * @param section The name of the section
105 * @param property The name of the property
106 * @return The property value, or null, if either the section or property
107 * does not exist.
108 */
109 public String getProperty(String section, String property)
110 {
111 String value = null;
112 IniSection sec = getSection(section);
113 if (sec != null)
114 {
115 IniProperty prop = sec.getProperty(property);
116 if (prop != null)
117 {
118 value = prop.getValue();
119 }
120 }
121 return value;
122 }
123
124 /***
125 * Sets the value of a property in a given section. If the section does
126 * not exist, it is automatically created.
127 * @param section The name of the section
128 * @param property The name of the property
129 * @param value The value of the property
130 */
131 public void setProperty(String section, String property, String value)
132 {
133 IniSection sec = getSection(section);
134 if (sec == null)
135 {
136 sec = new IniSection(section);
137 setSection(sec);
138 }
139
140 sec.setProperty(new IniProperty(property, value));
141 }
142
143 /***
144 * Removes a property from a section.
145 * @param section The name of the section
146 * @param property The name of the property
147 */
148 public void removeProperty(String section, String property)
149 {
150 IniSection sec = getSection(section);
151 if (sec != null)
152 {
153 sec.removeProperty(property);
154 }
155 }
156
157 /***
158 * Writes the current iniFile instance to a Writer object for
159 * serialization.
160 * @param writer The writer to write to
161 * @throws IOException
162 */
163 public void write(Writer writer)
164 throws IOException
165 {
166 Iterator it = sections.iterator();
167 IniSection section = null;
168 while (it.hasNext())
169 {
170 section = (IniSection)it.next();
171 section.write(writer);
172 writer.write(System.getProperty("line.separator"));
173 }
174 }
175
176 /***
177 * Reads from a Reader into the current IniFile instance. Reading
178 * appends to the current instance, so if the current instance has
179 * properties, those properties will still exist.
180 * @param reader The reader to read from.
181 * @throws IOException
182 */
183 public void read(Reader reader)
184 throws IOException
185 {
186 BufferedReader br = new BufferedReader(reader);
187 String line = null;
188
189 IniSection currentSection = new IniSection("NONE");
190
191 while ((line = br.readLine()) != null)
192 {
193 line = line.trim();
194 if (line.length() > 0 && !line.startsWith("#") && !line.startsWith(";"))
195 {
196 if(line.startsWith("[") && line.endsWith("]"))
197 {
198 String secName = line.substring(1, line.length()-1);
199 currentSection = getSection(secName);
200 if (currentSection == null)
201 {
202 currentSection = new IniSection(secName);
203 setSection(currentSection);
204 }
205 }
206 else
207 {
208 String name = line;
209 String value = "";
210 int pos = line.indexOf("=");
211 if (pos != -1)
212 {
213 name = line.substring(0,pos);
214 value = line.substring(pos+1);
215 }
216
217 currentSection.setProperty(new IniProperty(name,value));
218 }
219 }
220
221
222 }
223 }
224 }