Debian Codegraph @

src/main/java/net/sf/antcontrib/inifile/IniSection.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.IOException;
19 import java.io.Writer;
20 import java.util.*;
21
22
23 /****************************************************************************
24 * A section within an IniFile.
25 *
26 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
27 *
28 ****************************************************************************/
29
30
31 public class IniSection
32 implements IniPart
33 {
34 private String name;
35 private List properties;
36 private Map propertyMap;
37
38 /***
39 * Default contructor, constructs an IniSectino with no name
40 */
41 public IniSection()
42 {
43 super();
44 this.propertyMap = new HashMap();
45 this.properties = new ArrayList();
46 }
47
48
49 /***
50 * Constructs an IniSection with the given name
51 * @param name The name of the section
52 */
53 public IniSection(String name)
54 {
55 this();
56 this.name = name;
57 }
58
59
60 /***
61 * Gets a list of all properties in this section
62 * @return A List of IniProperty objects
63 */
64 public List getProperties()
65 {
66 return properties;
67 }
68
69
70 /***
71 * Gets the name of the section
72 */
73 public String getName()
74 {
75 return name;
76 }
77
78
79 /***
80 * Sets the name of the section
81 * @param name The name of the section
82 */
83 public void setName(String name)
84 {
85 this.name = name;
86 }
87
88 /***
89 * Gets the property with the given name
90 * @param name The name of the property
91 */
92 public IniProperty getProperty(String name)
93 {
94 return (IniProperty)propertyMap.get(name);
95 }
96
97 /***
98 * Sets a property, replacing the old value, if necessary.
99 * @param property The property to set
100 */
101 public void setProperty(IniProperty property)
102 {
103 IniProperty prop = (IniProperty)propertyMap.get(property.getName());
104 if (prop != null)
105 {
106 int idx = properties.indexOf(prop);
107 properties.set(idx, property);
108 }
109 else
110 {
111 properties.add(property);
112 }
113
114 propertyMap.put(property.getName(), property);
115 }
116
117 /***
118 * Removes a property from this ection
119 * @param name The name of the property to remove
120 */
121 public void removeProperty(String name)
122 {
123 IniProperty prop = (IniProperty)propertyMap.get(name);
124 if (prop != null)
125 {
126 int idx = properties.indexOf(prop);
127 properties.remove(idx);
128 propertyMap.remove(name);
129 }
130 }
131
132
133 public void write(Writer writer)
134 throws IOException
135 {
136 writer.write("[" + name + "]");
137 writer.write(System.getProperty("line.separator"));
138 Iterator it = properties.iterator();
139 IniProperty prop = null;
140 while (it.hasNext())
141 {
142 prop = (IniProperty)it.next();
143 prop.write(writer);
144 writer.write(System.getProperty("line.separator"));
145 }
146 }
147 }