1 package org.apache.turbine.services.intake.xmlmodel;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.Serializable;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.lang.StringUtils;
26
27 import org.xml.sax.Attributes;
28
29 /***
30 * A Class for holding data about a grouping of inputs used in an Application.
31 *
32 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
33 * @version $Id: XmlGroup.java,v 1.5.2.2 2004/05/20 03:06:49 seade Exp $
34 */
35 public class XmlGroup
36 implements Serializable
37 {
38 private List fields;
39 private List mapToObjects;
40 private String defaultMapToObject;
41 private AppData parent;
42 private String groupName;
43 private String key;
44 private String poolCapacity;
45
46 /***
47 * Constructs a input group object
48 */
49 public XmlGroup()
50 {
51 fields = new ArrayList();
52 mapToObjects = new ArrayList(2);
53 }
54
55 /***
56 * Load the input group object from an xml tag.
57 */
58 public void loadFromXML(Attributes attrib)
59 {
60 groupName = attrib.getValue("name");
61 key = attrib.getValue("key");
62 poolCapacity = attrib.getValue("pool-capacity");
63
64 String objName = attrib.getValue("mapToObject");
65 if (StringUtils.isNotEmpty(objName))
66 {
67 defaultMapToObject = objName;
68 }
69 }
70
71 /***
72 * Get the name that handles this group
73 */
74 public String getName()
75 {
76 return groupName;
77 }
78
79 /***
80 * Set the name that handles this group
81 */
82 public void setName(String newGroupName)
83 {
84 groupName = newGroupName;
85 }
86
87 /***
88 * Get the key used to reference this group in input (form)
89 */
90 public String getKey()
91 {
92 return key;
93 }
94
95 /***
96 * Set the key used to reference this group in input (form)
97 */
98 public void setKey(String newKey)
99 {
100 key = newKey;
101 }
102
103 /***
104 * The maximum number of classes specific to this group
105 * allowed at one time.
106 *
107 * @return an <code>String</code> value
108 */
109 public String getPoolCapacity()
110 {
111 if (poolCapacity == null)
112 {
113 return "128";
114 }
115
116 return poolCapacity;
117 }
118
119 /***
120 * A utility function to create a new field
121 * from attrib and add it to this input group.
122 */
123 public XmlField addField(Attributes attrib)
124 {
125 XmlField field = new XmlField();
126 field.loadFromXML(attrib);
127 addField(field);
128
129 return field;
130 }
131
132 /***
133 * Adds a new field to the fields list and set the
134 * parent group of the field to the current group
135 */
136 public void addField(XmlField field)
137 {
138 field.setGroup(this);
139
140
141
142 if (field.getMapToObject() != null)
143 {
144 boolean isNewObject = true;
145 for (int i = 0; i < mapToObjects.size(); i++)
146 {
147 if (mapToObjects.get(i).equals(field.getMapToObject()))
148 {
149 isNewObject = false;
150 break;
151 }
152 }
153 if (isNewObject)
154 {
155 mapToObjects.add(field.getMapToObject());
156 }
157 }
158
159 else if (field.getMapToProperty() != null
160 && !"".equals(field.getMapToProperty())
161 && defaultMapToObject != null)
162 {
163 field.setMapToObject(defaultMapToObject);
164 }
165
166 fields.add(field);
167 }
168
169 /***
170 * Returns a collection of fields in this input group
171 */
172 public List getFields()
173 {
174 return fields;
175 }
176
177 /***
178 * Utility method to get the number of fields in this input group
179 */
180 public int getNumFields()
181 {
182 return fields.size();
183 }
184
185 /***
186 * Returns a Specified field.
187 * @return Return a XmlField object or null if it does not exist.
188 */
189 public XmlField getField(String name)
190 {
191 String curName;
192
193 for (Iterator iter = fields.iterator(); iter.hasNext();)
194 {
195 XmlField field = (XmlField) iter.next();
196 curName = field.getRawName();
197 if (curName.equals(name))
198 {
199 return field;
200 }
201 }
202 return null;
203 }
204
205 /***
206 * Returns true if the input group contains a spesified field
207 */
208 public boolean containsField(XmlField field)
209 {
210 return fields.contains(field);
211 }
212
213 /***
214 * Returns true if the input group contains a specified field
215 */
216 public boolean containsField(String name)
217 {
218 return (getField(name) != null);
219 }
220
221 public List getMapToObjects()
222 {
223 return mapToObjects;
224 }
225
226 /***
227 * Set the parent of the group
228 */
229 public void setAppData(AppData parent)
230 {
231 this.parent = parent;
232 if (defaultMapToObject != null)
233 {
234 defaultMapToObject = parent.getBasePackage() + defaultMapToObject;
235 mapToObjects.add(defaultMapToObject);
236 }
237 }
238
239 /***
240 * Get the parent of the input group
241 */
242 public AppData getAppData()
243 {
244 return parent;
245 }
246
247 /***
248 * A String which might be used as a variable of this class
249 */
250 public String getVariable()
251 {
252 String firstChar = getName().substring(0, 1).toLowerCase();
253 return firstChar + getName().substring(1);
254 }
255
256 /***
257 * Creates a string representation of this input group. This
258 * is an xml representation.
259 */
260 public String toString()
261 {
262 StringBuffer result = new StringBuffer();
263
264 result.append("<group name=\"").append(getName());
265 result.append(" key=\"" + key + "\"");
266 result.append(">\n");
267
268 if (fields != null)
269 {
270 for (Iterator iter = fields.iterator(); iter.hasNext();)
271 {
272 result.append(iter.next());
273 }
274 }
275
276 result.append("</group>\n");
277
278 return result.toString();
279 }
280 }