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

https://github.com/srstclair/ant-contrib · Java · 211 lines · 105 code · 18 blank · 88 comment · 26 complexity · ad6dc45204958d8e0acf6d57e3188d62 MD5 · raw file

  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. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.Reader;
  20. import java.io.Writer;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import net.sf.antcontrib.util.StringTools;
  27. /**
  28. * Class representing a windows style .ini file.
  29. *
  30. * @author <a href="mailto:mattinger@yahoo.com">Matthew Inger</a>
  31. */
  32. public class IniFile {
  33. /**
  34. * Field sections.
  35. */
  36. private final List<IniSection> sections;
  37. /**
  38. * Field sectionMap.
  39. */
  40. private final Map<String, IniSection> sectionMap;
  41. /**
  42. * Create a new IniFile object.
  43. */
  44. public IniFile() {
  45. super();
  46. this.sections = new ArrayList<IniSection>();
  47. this.sectionMap = new HashMap<String, IniSection>();
  48. }
  49. /**
  50. * Gets the List of IniSection objects contained in this IniFile.
  51. *
  52. * @return a List of IniSection objects
  53. */
  54. public List<IniSection> getSections() {
  55. return sections;
  56. }
  57. /**
  58. * Gets the IniSection with the given name.
  59. *
  60. * @param name the name of the section
  61. * @return IniSection
  62. */
  63. public IniSection getSection(String name) {
  64. return sectionMap.get(name);
  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. *
  70. * @param section The section to set.
  71. */
  72. public void setSection(IniSection section) {
  73. IniSection sec = sectionMap.get(section.getName());
  74. if (sec != null) {
  75. int idx = sections.indexOf(sec);
  76. sections.set(idx, section);
  77. } else {
  78. sections.add(section);
  79. }
  80. sectionMap.put(section.getName(), section);
  81. }
  82. /**
  83. * Removes an entire section from the IniFile.
  84. *
  85. * @param name The name of the section to remove
  86. */
  87. public void removeSection(String name) {
  88. IniSection sec = sectionMap.get(name);
  89. if (sec != null) {
  90. int idx = sections.indexOf(sec);
  91. sections.remove(idx);
  92. sectionMap.remove(name);
  93. }
  94. }
  95. /**
  96. * Gets a named property from a specific section.
  97. *
  98. * @param section The name of the section
  99. * @param property The name of the property
  100. * @return The property value, or null, if either the section or property
  101. * does not exist.
  102. */
  103. public String getProperty(String section, String property) {
  104. String value = null;
  105. IniSection sec = getSection(section);
  106. if (sec != null) {
  107. IniProperty prop = sec.getProperty(property);
  108. if (prop != null) {
  109. value = prop.getValue();
  110. }
  111. }
  112. return value;
  113. }
  114. /**
  115. * Sets the value of a property in a given section. If the section does
  116. * not exist, it is automatically created.
  117. *
  118. * @param section The name of the section
  119. * @param property The name of the property
  120. * @param value The value of the property
  121. */
  122. public void setProperty(String section, String property, String value) {
  123. IniSection sec = getSection(section);
  124. if (sec == null) {
  125. sec = new IniSection(section);
  126. setSection(sec);
  127. }
  128. sec.setProperty(new IniProperty(property, value));
  129. }
  130. /**
  131. * Removes a property from a section.
  132. *
  133. * @param section The name of the section
  134. * @param property The name of the property
  135. */
  136. public void removeProperty(String section, String property) {
  137. IniSection sec = getSection(section);
  138. if (sec != null) {
  139. sec.removeProperty(property);
  140. }
  141. }
  142. /**
  143. * Writes the current iniFile instance to a Writer object for
  144. * serialization.
  145. *
  146. * @param writer The writer to write to
  147. * @throws IOException on write error
  148. */
  149. public void write(Writer writer) throws IOException {
  150. Iterator<IniSection> it = sections.iterator();
  151. IniSection section = null;
  152. while (it.hasNext()) {
  153. section = it.next();
  154. section.write(writer);
  155. writer.write(System.getProperty("line.separator"));
  156. }
  157. }
  158. /**
  159. * Reads from a Reader into the current IniFile instance. Reading
  160. * appends to the current instance, so if the current instance has
  161. * properties, those properties will still exist.
  162. *
  163. * @param reader The reader to read from.
  164. * @throws IOException on read error
  165. */
  166. public void read(Reader reader) throws IOException {
  167. BufferedReader br = new BufferedReader(reader);
  168. String line = null;
  169. IniSection currentSection = new IniSection("NONE");
  170. while ((line = br.readLine()) != null) {
  171. line = StringTools.trim(line);
  172. if (line.length() > 0 && !line.startsWith("#") && !line.startsWith(";")) {
  173. if (line.startsWith("[") && line.endsWith("]")) {
  174. String secName = line.substring(1, line.length() - 1);
  175. currentSection = getSection(secName);
  176. if (currentSection == null) {
  177. currentSection = new IniSection(secName);
  178. setSection(currentSection);
  179. }
  180. } else {
  181. String name = line;
  182. String value = "";
  183. int pos = line.indexOf("=");
  184. if (pos != -1) {
  185. name = line.substring(0, pos);
  186. value = line.substring(pos + 1);
  187. }
  188. currentSection.setProperty(new IniProperty(name, value));
  189. }
  190. }
  191. }
  192. }
  193. }