View Javadoc

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