View Javadoc

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