001 package org.apache.fulcrum.intake.xmlmodel;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.Serializable;
023
024 import java.util.ArrayList;
025 import java.util.Iterator;
026 import java.util.List;
027
028 import org.apache.commons.lang.StringUtils;
029
030 import org.xml.sax.Attributes;
031
032 /**
033 * A Class for holding data about a grouping of inputs used in an Application.
034 *
035 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
036 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
037 * @version $Id: XmlGroup.java 535465 2007-05-05 06:58:06Z tv $
038 */
039 public class XmlGroup
040 implements Serializable
041 {
042 /**
043 * Serial version id
044 */
045 private static final long serialVersionUID = 4771953823149519746L;
046
047 private List fields;
048 private List mapToObjects;
049 private String defaultMapToObject;
050 private AppData parent;
051 private String groupName;
052 private String key;
053 private String poolCapacity;
054
055 /**
056 * Constructs a input group object
057 */
058 public XmlGroup()
059 {
060 fields = new ArrayList();
061 mapToObjects = new ArrayList(2);
062 }
063
064 /**
065 * Load the input group object from an xml tag.
066 */
067 public void loadFromXML(Attributes attrib)
068 {
069 groupName = attrib.getValue("name");
070 key = attrib.getValue("key");
071 poolCapacity = attrib.getValue("pool-capacity");
072
073 String objName = attrib.getValue("mapToObject");
074 if (StringUtils.isNotEmpty(objName))
075 {
076 defaultMapToObject = objName;
077 }
078 }
079
080 /**
081 * Get the name that handles this group
082 */
083 public String getName()
084 {
085 return groupName;
086 }
087
088 /**
089 * Set the name that handles this group
090 */
091 public void setName(String newGroupName)
092 {
093 groupName = newGroupName;
094 }
095
096 /**
097 * Get the key used to reference this group in input (form)
098 */
099 public String getKey()
100 {
101 return key;
102 }
103
104 /**
105 * Set the key used to reference this group in input (form)
106 */
107 public void setKey(String newKey)
108 {
109 key = newKey;
110 }
111
112 /**
113 * The maximum number of classes specific to this group
114 * allowed at one time.
115 *
116 * @return an <code>String</code> value
117 */
118 public String getPoolCapacity()
119 {
120 if (poolCapacity == null)
121 {
122 return "128";
123 }
124
125 return poolCapacity;
126 }
127
128 /**
129 * A utility function to create a new field
130 * from attrib and add it to this input group.
131 */
132 public XmlField addField(Attributes attrib)
133 {
134 XmlField field = new XmlField();
135 field.loadFromXML(attrib);
136 addField(field);
137
138 return field;
139 }
140
141 /**
142 * Adds a new field to the fields list and set the
143 * parent group of the field to the current group
144 */
145 public void addField(XmlField field)
146 {
147 field.setGroup(this);
148
149 // if this field has an object defined for mapping,
150 // add it to the list
151 if (field.getMapToObject() != null)
152 {
153 boolean isNewObject = true;
154 for (int i = 0; i < mapToObjects.size(); i++)
155 {
156 if (mapToObjects.get(i).equals(field.getMapToObject()))
157 {
158 isNewObject = false;
159 break;
160 }
161 }
162 if (isNewObject)
163 {
164 mapToObjects.add(field.getMapToObject());
165 }
166 }
167 // if a mapToProperty exists, set the object to this group's default
168 else if (field.getMapToProperty() != null
169 && !"".equals(field.getMapToProperty())
170 && defaultMapToObject != null)
171 {
172 field.setMapToObject(defaultMapToObject);
173 }
174
175 fields.add(field);
176 }
177
178 /**
179 * Returns a collection of fields in this input group
180 */
181 public List getFields()
182 {
183 return fields;
184 }
185
186 /**
187 * Utility method to get the number of fields in this input group
188 */
189 public int getNumFields()
190 {
191 return fields.size();
192 }
193
194 /**
195 * Returns a Specified field.
196 * @return Return a XmlField object or null if it does not exist.
197 */
198 public XmlField getField(String name)
199 {
200 String curName;
201
202 for (Iterator iter = fields.iterator(); iter.hasNext();)
203 {
204 XmlField field = (XmlField) iter.next();
205 curName = field.getRawName();
206 if (curName.equals(name))
207 {
208 return field;
209 }
210 }
211 return null;
212 }
213
214 /**
215 * Returns true if the input group contains a spesified field
216 */
217 public boolean containsField(XmlField field)
218 {
219 return fields.contains(field);
220 }
221
222 /**
223 * Returns true if the input group contains a specified field
224 */
225 public boolean containsField(String name)
226 {
227 return (getField(name) != null);
228 }
229
230 public List getMapToObjects()
231 {
232 return mapToObjects;
233 }
234
235 /**
236 * Set the parent of the group
237 */
238 public void setAppData(AppData parent)
239 {
240 this.parent = parent;
241 if (defaultMapToObject != null)
242 {
243 defaultMapToObject = parent.getBasePackage() + defaultMapToObject;
244 mapToObjects.add(defaultMapToObject);
245 }
246 }
247
248 /**
249 * Get the parent of the input group
250 */
251 public AppData getAppData()
252 {
253 return parent;
254 }
255
256 /**
257 * A String which might be used as a variable of this class
258 */
259 public String getVariable()
260 {
261 String firstChar = getName().substring(0, 1).toLowerCase();
262 return firstChar + getName().substring(1);
263 }
264
265 /**
266 * Creates a string representation of this input group. This
267 * is an xml representation.
268 */
269 public String toString()
270 {
271 StringBuffer result = new StringBuffer();
272
273 result.append("<group name=\"").append(getName());
274 result.append(" key=\"" + key + "\"");
275 result.append(">\n");
276
277 if (fields != null)
278 {
279 for (Iterator iter = fields.iterator(); iter.hasNext();)
280 {
281 result.append(iter.next());
282 }
283 }
284
285 result.append("</group>\n");
286
287 return result.toString();
288 }
289 }