001: package org.apache.turbine.services.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: * @version $Id: XmlGroup.java 534527 2007-05-02 16:10:59Z tv $
037: */
038: public class XmlGroup implements Serializable {
039: /** Serial Version UID */
040: private static final long serialVersionUID = -2529039710689787681L;
041:
042: private List fields;
043: private List mapToObjects;
044: private String defaultMapToObject;
045: private AppData parent;
046: private String groupName;
047: private String key;
048: private String poolCapacity;
049:
050: /**
051: * Constructs a input group object
052: */
053: public XmlGroup() {
054: fields = new ArrayList();
055: mapToObjects = new ArrayList(2);
056: }
057:
058: /**
059: * Load the input group object from an xml tag.
060: */
061: public void loadFromXML(Attributes attrib) {
062: groupName = attrib.getValue("name");
063: key = attrib.getValue("key");
064: poolCapacity = attrib.getValue("pool-capacity");
065:
066: String objName = attrib.getValue("mapToObject");
067: if (StringUtils.isNotEmpty(objName)) {
068: defaultMapToObject = objName;
069: }
070: }
071:
072: /**
073: * Get the name that handles this group
074: */
075: public String getName() {
076: return groupName;
077: }
078:
079: /**
080: * Set the name that handles this group
081: */
082: public void setName(String newGroupName) {
083: groupName = newGroupName;
084: }
085:
086: /**
087: * Get the key used to reference this group in input (form)
088: */
089: public String getKey() {
090: return key;
091: }
092:
093: /**
094: * Set the key used to reference this group in input (form)
095: */
096: public void setKey(String newKey) {
097: key = newKey;
098: }
099:
100: /**
101: * The maximum number of classes specific to this group
102: * allowed at one time.
103: *
104: * @return an <code>String</code> value
105: */
106: public String getPoolCapacity() {
107: if (poolCapacity == null) {
108: return "128";
109: }
110:
111: return poolCapacity;
112: }
113:
114: /**
115: * A utility function to create a new field
116: * from attrib and add it to this input group.
117: */
118: public XmlField addField(Attributes attrib) {
119: XmlField field = new XmlField();
120: field.loadFromXML(attrib);
121: addField(field);
122:
123: return field;
124: }
125:
126: /**
127: * Adds a new field to the fields list and set the
128: * parent group of the field to the current group
129: */
130: public void addField(XmlField field) {
131: field.setGroup(this );
132:
133: // if this field has an object defined for mapping,
134: // add it to the list
135: if (field.getMapToObject() != null) {
136: boolean isNewObject = true;
137: for (int i = 0; i < mapToObjects.size(); i++) {
138: if (mapToObjects.get(i).equals(field.getMapToObject())) {
139: isNewObject = false;
140: break;
141: }
142: }
143: if (isNewObject) {
144: mapToObjects.add(field.getMapToObject());
145: }
146: }
147: // if a mapToProperty exists, set the object to this group's default
148: else if (field.getMapToProperty() != null
149: && !"".equals(field.getMapToProperty())
150: && defaultMapToObject != null) {
151: field.setMapToObject(defaultMapToObject);
152: }
153:
154: fields.add(field);
155: }
156:
157: /**
158: * Returns a collection of fields in this input group
159: */
160: public List getFields() {
161: return fields;
162: }
163:
164: /**
165: * Utility method to get the number of fields in this input group
166: */
167: public int getNumFields() {
168: return fields.size();
169: }
170:
171: /**
172: * Returns a Specified field.
173: * @return Return a XmlField object or null if it does not exist.
174: */
175: public XmlField getField(String name) {
176: String curName;
177:
178: for (Iterator iter = fields.iterator(); iter.hasNext();) {
179: XmlField field = (XmlField) iter.next();
180: curName = field.getRawName();
181: if (curName.equals(name)) {
182: return field;
183: }
184: }
185: return null;
186: }
187:
188: /**
189: * Returns true if the input group contains a spesified field
190: */
191: public boolean containsField(XmlField field) {
192: return fields.contains(field);
193: }
194:
195: /**
196: * Returns true if the input group contains a specified field
197: */
198: public boolean containsField(String name) {
199: return (getField(name) != null);
200: }
201:
202: public List getMapToObjects() {
203: return mapToObjects;
204: }
205:
206: /**
207: * Set the parent of the group
208: */
209: public void setAppData(AppData parent) {
210: this .parent = parent;
211: if (defaultMapToObject != null) {
212: defaultMapToObject = parent.getBasePackage()
213: + defaultMapToObject;
214: mapToObjects.add(defaultMapToObject);
215: }
216: }
217:
218: /**
219: * Get the parent of the input group
220: */
221: public AppData getAppData() {
222: return parent;
223: }
224:
225: /**
226: * A String which might be used as a variable of this class
227: */
228: public String getVariable() {
229: String firstChar = getName().substring(0, 1).toLowerCase();
230: return firstChar + getName().substring(1);
231: }
232:
233: /**
234: * Creates a string representation of this input group. This
235: * is an xml representation.
236: */
237: public String toString() {
238: StringBuffer result = new StringBuffer();
239:
240: result.append("<group name=\"").append(getName());
241: result.append(" key=\"" + key + "\"");
242: result.append(">\n");
243:
244: if (fields != null) {
245: for (Iterator iter = fields.iterator(); iter.hasNext();) {
246: result.append(iter.next());
247: }
248: }
249:
250: result.append("</group>\n");
251:
252: return result.toString();
253: }
254: }
|