KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > xmlmodel > XmlGroup


1 package org.apache.turbine.services.intake.xmlmodel;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.Serializable JavaDoc;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26
27 import org.xml.sax.Attributes JavaDoc;
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 JavaDoc
37 {
38     private List JavaDoc fields;
39     private List JavaDoc mapToObjects;
40     private String JavaDoc defaultMapToObject;
41     private AppData parent;
42     private String JavaDoc groupName;
43     private String JavaDoc key;
44     private String JavaDoc poolCapacity;
45
46     /**
47      * Constructs a input group object
48      */

49     public XmlGroup()
50     {
51         fields = new ArrayList JavaDoc();
52         mapToObjects = new ArrayList JavaDoc(2);
53     }
54
55     /**
56      * Load the input group object from an xml tag.
57      */

58     public void loadFromXML(Attributes JavaDoc attrib)
59     {
60         groupName = attrib.getValue("name");
61         key = attrib.getValue("key");
62         poolCapacity = attrib.getValue("pool-capacity");
63
64         String JavaDoc 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 JavaDoc getName()
75     {
76         return groupName;
77     }
78
79     /**
80      * Set the name that handles this group
81      */

82     public void setName(String JavaDoc newGroupName)
83     {
84         groupName = newGroupName;
85     }
86
87     /**
88      * Get the key used to reference this group in input (form)
89      */

90     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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         // if this field has an object defined for mapping,
141
// add it to the list
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         // if a mapToProperty exists, set the object to this group's default
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 JavaDoc 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 JavaDoc name)
190     {
191         String JavaDoc curName;
192
193         for (Iterator JavaDoc 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 JavaDoc name)
217     {
218         return (getField(name) != null);
219     }
220
221     public List JavaDoc 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 JavaDoc getVariable()
251     {
252         String JavaDoc 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 JavaDoc toString()
261     {
262         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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 JavaDoc 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 }
281
Popular Tags