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.turbine.services.intake.IntakeException;
029:
030: import org.xml.sax.Attributes;
031:
032: /**
033: * A class for holding application data structures.
034: *
035: * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @version $Id: AppData.java 534527 2007-05-02 16:10:59Z tv $
038: */
039: public class AppData implements Serializable {
040: /** Serial Version UID */
041: private static final long serialVersionUID = -7998777726853272835L;
042:
043: /** List of groups */
044: private List inputs;
045:
046: /** Package that will be used for all mapTo objects */
047: private String basePackage;
048:
049: /** Prefix string that will be used to qualify <prefix>:<intakegroup> names */
050: private String groupPrefix;
051:
052: /**
053: * Default Constructor
054: */
055: public AppData() {
056: inputs = new ArrayList();
057: }
058:
059: /**
060: * Imports the top level element from an XML specification
061: */
062: public void loadFromXML(Attributes attrib) {
063: String basePkg = attrib.getValue("basePackage");
064: if (basePkg == null) {
065: setBasePackage("");
066: } else {
067: if (basePkg.charAt(basePkg.length() - 1) != '.') {
068: setBasePackage(basePkg + '.');
069: } else {
070: setBasePackage(basePkg);
071: }
072: }
073:
074: setGroupPrefix(attrib.getValue("groupPrefix"));
075: }
076:
077: /**
078: * Return a collection of input sections (<group>).
079: * The names of the groups returned here are only unique
080: * to this AppData object and not qualified with the groupPrefix.
081: * This method is used in the IntakeService to register all the
082: * groups with and without prefix in the service.
083: *
084: */
085: public List getGroups() {
086: return inputs;
087: }
088:
089: /**
090: * Get a XmlGroup with the given name. It finds both
091: * qualified and unqualified names in this package.
092: *
093: * @param groupName a <code>String</code> value
094: * @return a <code>XmlGroup</code> value
095: * @throws IntakeException indicates that the groupName was null
096: */
097: public XmlGroup getGroup(String groupName) throws IntakeException {
098: if (groupName == null) {
099: throw new IntakeException(
100: "Intake AppData.getGroup(groupName) is null");
101: }
102:
103: String groupPrefix = getGroupPrefix();
104:
105: for (Iterator it = inputs.iterator(); it.hasNext();) {
106: XmlGroup group = (XmlGroup) it.next();
107:
108: if (group.getName().equals(groupName)) {
109: return group;
110: }
111: if (groupPrefix != null) {
112: StringBuffer qualifiedGroupName = new StringBuffer();
113:
114: qualifiedGroupName.append(groupPrefix).append(':')
115: .append(group.getName());
116:
117: if (qualifiedGroupName.toString().equals(groupName)) {
118: return group;
119: }
120: }
121: }
122: return null;
123: }
124:
125: /**
126: * An utility method to add a new input group from
127: * an xml attribute.
128: */
129: public XmlGroup addGroup(Attributes attrib) {
130: XmlGroup input = new XmlGroup();
131: input.loadFromXML(attrib);
132: addGroup(input);
133: return input;
134: }
135:
136: /**
137: * Add an input group to the vector and sets the
138: * AppData property to this AppData
139: */
140: public void addGroup(XmlGroup input) {
141: input.setAppData(this );
142: inputs.add(input);
143: }
144:
145: /**
146: * Get the base package String that will be appended to
147: * any mapToObjects
148: *
149: * @return value of basePackage.
150: */
151: public String getBasePackage() {
152: return basePackage;
153: }
154:
155: /**
156: * Set the base package String that will be appended to
157: * any mapToObjects
158: *
159: * @param v Value to assign to basePackage.
160: */
161: public void setBasePackage(String v) {
162: this .basePackage = v;
163: }
164:
165: /**
166: * Get the prefix String that will be used to qualify
167: * intake groups when using multiple XML files
168: *
169: * @return value of groupPrefix
170: */
171: public String getGroupPrefix() {
172: return groupPrefix;
173: }
174:
175: /**
176: * Set the prefix String that will be used to qualify
177: * intake groups when using multiple XML files
178: *
179: * @param groupPrefix Value to assign to basePackage.
180: */
181: public void setGroupPrefix(String groupPrefix) {
182: this .groupPrefix = groupPrefix;
183: }
184:
185: /**
186: * Creats a string representation of this AppData.
187: * The representation is given in xml format.
188: */
189: public String toString() {
190: StringBuffer result = new StringBuffer();
191:
192: result.append("<input-data>\n");
193: for (Iterator iter = inputs.iterator(); iter.hasNext();) {
194: result.append(iter.next());
195: }
196: result.append("</input-data>");
197: return result.toString();
198: }
199: }
|