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