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