View Javadoc

1   package org.apache.turbine.services.intake.xmlmodel;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Serializable;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.turbine.services.intake.IntakeException;
26  
27  import org.xml.sax.Attributes;
28  
29  /***
30   * A class for holding application data structures.
31   *
32   * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
33   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34   * @version $Id: AppData.java,v 1.5.2.2 2004/05/20 03:06:49 seade Exp $
35   */
36  public class AppData
37          implements Serializable
38  {
39      /*** List of groups */
40      private List inputs;
41  
42      /*** Package that will be used for all mapTo objects */
43      private String basePackage;
44  
45      /*** Prefix string that will be used to qualify &lt;prefix&gt;:&lt;intakegroup&gt; names */
46      private String groupPrefix;
47  
48      /***
49       * Default Constructor
50       */
51      public AppData()
52      {
53          inputs = new ArrayList();
54      }
55  
56      /***
57       * Imports the top level element from an XML specification
58       */
59      public void loadFromXML(Attributes attrib)
60      {
61          String basePkg = attrib.getValue("basePackage");
62          if (basePkg == null)
63          {
64              setBasePackage("");
65          }
66          else
67          {
68              if (basePkg.charAt(basePkg.length() - 1) != '.')
69              {
70                  setBasePackage(basePkg + '.');
71              }
72              else
73              {
74                  setBasePackage(basePkg);
75              }
76          }
77  
78          setGroupPrefix(attrib.getValue("groupPrefix"));
79      }
80  
81      /***
82       * Return a collection of input sections (&lt;group&gt;).
83       * The names of the groups returned here are only unique
84       * to this AppData object and not qualified with the groupPrefix.
85       * This method is used in the IntakeService to register all the
86       * groups with and without prefix in the service.
87       *
88       */
89      public List getGroups()
90      {
91          return inputs;
92      }
93  
94      /***
95       * Get a XmlGroup with the given name. It finds both
96       * qualified and unqualified names in this package.
97       *
98       * @param groupName a <code>String</code> value
99       * @return a <code>XmlGroup</code> value
100      * @throws IntakeException indicates that the groupName was null
101      */
102     public XmlGroup getGroup(String groupName)
103             throws IntakeException
104     {
105         if (groupName == null)
106         {
107             throw new IntakeException(
108                     "Intake AppData.getGroup(groupName) is null");
109         }
110 
111         String groupPrefix = getGroupPrefix();
112 
113         for (Iterator it = inputs.iterator(); it.hasNext();)
114         {
115             XmlGroup group = (XmlGroup) it.next();
116 
117             if (group.getName().equals(groupName))
118             {
119                 return group;
120             }
121             if (groupPrefix != null)
122             {
123                 StringBuffer qualifiedGroupName = new StringBuffer();
124 
125                 qualifiedGroupName.append(groupPrefix)
126                         .append(':')
127                         .append(group.getName());
128 
129                 if (qualifiedGroupName.toString().equals(groupName))
130                 {
131                     return group;
132                 }
133             }
134         }
135         return null;
136     }
137 
138     /***
139      * An utility method to add a new input group from
140      * an xml attribute.
141      */
142     public XmlGroup addGroup(Attributes attrib)
143     {
144         XmlGroup input = new XmlGroup();
145         input.loadFromXML(attrib);
146         addGroup(input);
147         return input;
148     }
149 
150     /***
151      * Add an input group to the vector and sets the
152      * AppData property to this AppData
153      */
154     public void addGroup(XmlGroup input)
155     {
156         input.setAppData(this);
157         inputs.add(input);
158     }
159 
160     /***
161      * Get the base package String that will be appended to
162      * any mapToObjects
163      *
164      * @return value of basePackage.
165      */
166     public String getBasePackage()
167     {
168         return basePackage;
169     }
170 
171     /***
172      * Set the base package String that will be appended to
173      * any mapToObjects
174      *
175      * @param v  Value to assign to basePackage.
176      */
177     public void setBasePackage(String v)
178     {
179         this.basePackage = v;
180     }
181 
182     /***
183      * Get the prefix String that will be used to qualify
184      * intake groups when using multiple XML files
185      *
186      * @return value of groupPrefix
187      */
188     public String getGroupPrefix()
189     {
190         return groupPrefix;
191     }
192 
193     /***
194      * Set the prefix String that will be used to qualify
195      * intake groups when using multiple XML files
196      *
197      * @param groupPrefix  Value to assign to basePackage.
198      */
199     public void setGroupPrefix(String groupPrefix)
200     {
201         this.groupPrefix = groupPrefix;
202     }
203 
204     /***
205      * Creats a string representation of this AppData.
206      * The representation is given in xml format.
207      */
208     public String toString()
209     {
210         StringBuffer result = new StringBuffer();
211 
212         result.append("<input-data>\n");
213         for (Iterator iter = inputs.iterator(); iter.hasNext();)
214         {
215             result.append(iter.next());
216         }
217         result.append("</input-data>");
218         return result.toString();
219     }
220 }