KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > xmlmodel > XmlGroup


1 package org.apache.fulcrum.intake.xmlmodel;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.Serializable JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import org.xml.sax.Attributes JavaDoc;
62
63 /**
64  * A Class for holding data about a grouping of inputs used in an Application.
65  *
66  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
67  * @version $Id: XmlGroup.java,v 1.1 2004/11/12 10:25:47 epugh Exp $
68  */

69 public class XmlGroup
70     implements Serializable JavaDoc
71 {
72     private List JavaDoc fields;
73     private List JavaDoc mapToObjects;
74     private String JavaDoc defaultMapToObject;
75     private AppData parent;
76     private String JavaDoc groupName;
77     private String JavaDoc key;
78     private String JavaDoc poolCapacity;
79
80     /**
81      * Constructs a input group object
82      */

83     public XmlGroup()
84     {
85         fields = new ArrayList JavaDoc();
86         mapToObjects = new ArrayList JavaDoc(2);
87     }
88
89     /**
90      * Load the input group object from an xml tag.
91      */

92     public void loadFromXML (Attributes JavaDoc attrib)
93     {
94         groupName = attrib.getValue("name");
95         key = attrib.getValue("key");
96         poolCapacity = attrib.getValue("pool-capacity");
97
98         String JavaDoc objName = attrib.getValue("mapToObject");
99         if ( objName != null && objName.length() != 0 )
100         {
101             defaultMapToObject = objName;
102         }
103     }
104
105     /**
106      * Get the name that handles this group
107      */

108     public String JavaDoc getName()
109     {
110         return groupName;
111     }
112
113     /**
114      * Set the name that handles this group
115      */

116     public void setName(String JavaDoc newGroupName)
117     {
118         groupName = newGroupName;
119     }
120
121     /**
122      * Get the key used to reference this group in input (form)
123      */

124     public String JavaDoc getKey()
125     {
126         return key;
127     }
128
129     /**
130      * Set the key used to reference this group in input (form)
131      */

132     public void setKey(String JavaDoc newKey)
133     {
134         key = newKey;
135     }
136
137
138     /**
139      * The maximum number of classes specific to this group
140      * allowed at one time.
141      *
142      * @return an <code>String</code> value
143      */

144     public String JavaDoc getPoolCapacity()
145     {
146         if ( poolCapacity == null )
147         {
148             return "128";
149         }
150
151         return poolCapacity;
152     }
153
154     /**
155      * A utility function to create a new field
156      * from attrib and add it to this input group.
157      */

158     public XmlField addField(Attributes JavaDoc attrib)
159     {
160         XmlField field = new XmlField();
161         field.loadFromXML(attrib);
162         addField(field);
163
164         return field;
165     }
166
167     /**
168      * Adds a new field to the fields list and set the
169      * parent group of the field to the current group
170      */

171     public void addField(XmlField field)
172     {
173         field.setGroup(this);
174
175         // if this field has an object defined for mapping,
176
// add it to the list
177
if ( field.getMapToObject() != null )
178         {
179             boolean isNewObject = true;
180             for ( int i=0; i<mapToObjects.size(); i++ )
181             {
182                 if ( mapToObjects.get(i).equals(field.getMapToObject()) )
183                 {
184                     isNewObject = false;
185                     break;
186                 }
187             }
188             if ( isNewObject )
189             {
190                 mapToObjects.add(field.getMapToObject());
191             }
192         }
193         // if a mapToProperty exists, set the object to this group's default
194
else if( field.getMapToProperty() != null
195                  && !"".equals(field.getMapToProperty())
196                  && defaultMapToObject != null )
197         {
198             field.setMapToObject(defaultMapToObject);
199         }
200
201         fields.add(field);
202     }
203
204     /**
205      * Returns a collection of fields in this input group
206      */

207     public List JavaDoc getFields()
208     {
209         return fields;
210     }
211
212     /**
213      * Utility method to get the number of fields in this input group
214      */

215     public int getNumFields()
216     {
217         return fields.size();
218     }
219
220
221     /**
222      * Returns a Specified field.
223      * @return Return a XmlField object or null if it does not exist.
224      */

225     public XmlField getField(String JavaDoc name)
226     {
227         String JavaDoc curName;
228
229         for (Iterator JavaDoc iter = fields.iterator() ; iter.hasNext() ;)
230         {
231             XmlField field = (XmlField) iter.next();
232             curName = field.getRawName();
233             if (curName.equals(name))
234             {
235                 return field;
236             }
237         }
238         return null;
239     }
240
241     /**
242      * Returns true if the input group contains a spesified field
243      */

244     public boolean containsField(XmlField field)
245     {
246         return fields.contains (field);
247     }
248
249     /**
250      * Returns true if the input group contains a specified field
251      */

252     public boolean containsField(String JavaDoc name)
253     {
254         return (getField (name) != null);
255     }
256
257     public List JavaDoc getMapToObjects()
258     {
259         return mapToObjects;
260     }
261
262     /**
263      * Set the parent of the group
264      */

265     public void setAppData(AppData parent)
266     {
267         this.parent = parent;
268         if (defaultMapToObject != null )
269         {
270             defaultMapToObject = parent.getBasePackage() + defaultMapToObject;
271             mapToObjects.add(defaultMapToObject);
272         }
273     }
274
275     /**
276      * Get the parent of the input group
277      */

278     public AppData getAppData()
279     {
280         return parent;
281     }
282
283     /**
284      * A String which might be used as a variable of this class
285      */

286     public String JavaDoc getVariable()
287     {
288         String JavaDoc firstChar = getName().substring(0,1).toLowerCase();
289         return firstChar + getName().substring(1);
290     }
291
292
293     /**
294      * Creates a string representation of this input group. This
295      * is an xml representation.
296      */

297     public String JavaDoc toString()
298     {
299         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
300
301         result.append ("<group name=\"").append(getName());
302         result.append(" key=\""+key+"\"");
303         result.append(">\n");
304
305         if (fields != null)
306         {
307             for (Iterator JavaDoc iter = fields.iterator() ; iter.hasNext() ;)
308             {
309                 result.append(iter.next());
310             }
311         }
312
313         result.append ("</group>\n");
314
315         return result.toString();
316     }
317 }
318
Popular Tags