View Javadoc

1   package org.apache.turbine.services.intake.xmlmodel;
2   
3   /*
4    * Copyright 2001-2005 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 278822 2005-09-05 19:53:05Z henning $
35   */
36  public class AppData
37          implements Serializable
38  {
39      /*** Serial Version UID */
40      private static final long serialVersionUID = -7998777726853272835L;
41  
42      /*** List of groups */
43      private List inputs;
44  
45      /*** Package that will be used for all mapTo objects */
46      private String basePackage;
47  
48      /*** Prefix string that will be used to qualify &lt;prefix&gt;:&lt;intakegroup&gt; names */
49      private String groupPrefix;
50  
51      /***
52       * Default Constructor
53       */
54      public AppData()
55      {
56          inputs = new ArrayList();
57      }
58  
59      /***
60       * Imports the top level element from an XML specification
61       */
62      public void loadFromXML(Attributes attrib)
63      {
64          String basePkg = attrib.getValue("basePackage");
65          if (basePkg == null)
66          {
67              setBasePackage("");
68          }
69          else
70          {
71              if (basePkg.charAt(basePkg.length() - 1) != '.')
72              {
73                  setBasePackage(basePkg + '.');
74              }
75              else
76              {
77                  setBasePackage(basePkg);
78              }
79          }
80  
81          setGroupPrefix(attrib.getValue("groupPrefix"));
82      }
83  
84      /***
85       * Return a collection of input sections (&lt;group&gt;).
86       * The names of the groups returned here are only unique
87       * to this AppData object and not qualified with the groupPrefix.
88       * This method is used in the IntakeService to register all the
89       * groups with and without prefix in the service.
90       *
91       */
92      public List getGroups()
93      {
94          return inputs;
95      }
96  
97      /***
98       * Get a XmlGroup with the given name. It finds both
99       * qualified and unqualified names in this package.
100      *
101      * @param groupName a <code>String</code> value
102      * @return a <code>XmlGroup</code> value
103      * @throws IntakeException indicates that the groupName was null
104      */
105     public XmlGroup getGroup(String groupName)
106             throws IntakeException
107     {
108         if (groupName == null)
109         {
110             throw new IntakeException(
111                     "Intake AppData.getGroup(groupName) is null");
112         }
113 
114         String groupPrefix = getGroupPrefix();
115 
116         for (Iterator it = inputs.iterator(); it.hasNext();)
117         {
118             XmlGroup group = (XmlGroup) it.next();
119 
120             if (group.getName().equals(groupName))
121             {
122                 return group;
123             }
124             if (groupPrefix != null)
125             {
126                 StringBuffer qualifiedGroupName = new StringBuffer();
127 
128                 qualifiedGroupName.append(groupPrefix)
129                         .append(':')
130                         .append(group.getName());
131 
132                 if (qualifiedGroupName.toString().equals(groupName))
133                 {
134                     return group;
135                 }
136             }
137         }
138         return null;
139     }
140 
141     /***
142      * An utility method to add a new input group from
143      * an xml attribute.
144      */
145     public XmlGroup addGroup(Attributes attrib)
146     {
147         XmlGroup input = new XmlGroup();
148         input.loadFromXML(attrib);
149         addGroup(input);
150         return input;
151     }
152 
153     /***
154      * Add an input group to the vector and sets the
155      * AppData property to this AppData
156      */
157     public void addGroup(XmlGroup input)
158     {
159         input.setAppData(this);
160         inputs.add(input);
161     }
162 
163     /***
164      * Get the base package String that will be appended to
165      * any mapToObjects
166      *
167      * @return value of basePackage.
168      */
169     public String getBasePackage()
170     {
171         return basePackage;
172     }
173 
174     /***
175      * Set the base package String that will be appended to
176      * any mapToObjects
177      *
178      * @param v  Value to assign to basePackage.
179      */
180     public void setBasePackage(String v)
181     {
182         this.basePackage = v;
183     }
184 
185     /***
186      * Get the prefix String that will be used to qualify
187      * intake groups when using multiple XML files
188      *
189      * @return value of groupPrefix
190      */
191     public String getGroupPrefix()
192     {
193         return groupPrefix;
194     }
195 
196     /***
197      * Set the prefix String that will be used to qualify
198      * intake groups when using multiple XML files
199      *
200      * @param groupPrefix  Value to assign to basePackage.
201      */
202     public void setGroupPrefix(String groupPrefix)
203     {
204         this.groupPrefix = groupPrefix;
205     }
206 
207     /***
208      * Creats a string representation of this AppData.
209      * The representation is given in xml format.
210      */
211     public String toString()
212     {
213         StringBuffer result = new StringBuffer();
214 
215         result.append("<input-data>\n");
216         for (Iterator iter = inputs.iterator(); iter.hasNext();)
217         {
218             result.append(iter.next());
219         }
220         result.append("</input-data>");
221         return result.toString();
222     }
223 }