View Javadoc
1   package org.apache.fulcrum.intake.model;
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  import java.util.List;
24  
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlAttribute;
28  import javax.xml.bind.annotation.XmlElement;
29  import javax.xml.bind.annotation.XmlRootElement;
30  
31  import org.apache.fulcrum.intake.IntakeException;
32  
33  /**
34   * A class for holding application data structures.
35   *
36   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
39   * @version $Id$
40   */
41  @XmlRootElement(name="input-data")
42  @XmlAccessorType(XmlAccessType.NONE)
43  public class AppData implements Serializable
44  {
45      /**
46       * Serial version id
47       */
48      private static final long serialVersionUID = -3953843038383617960L;
49  
50      /** List of groups */
51      private List<Group> groups;
52  
53      /** Package that will be used for all mapTo objects */
54      private String basePackage = "";
55  
56      /** Prefix string that will be used to qualify &lt;prefix&gt;:&lt;intakegroup&gt; names */
57      private String groupPrefix;
58  
59      /**
60       * Return a collection of input sections (&lt;group&gt;).
61       * The names of the groups returned here are only unique
62       * to this AppData object and not qualified with the groupPrefix.
63       * This method is used in the IntakeService to register all the
64       * groups with and without prefix in the service.
65       *
66       * @return the list of groups
67       */
68      public List<Group> getGroups()
69      {
70          return groups;
71      }
72  
73      /**
74       * Set the collection of groups
75       *
76       * @param groups the groups to set
77       */
78      @XmlElement(name="group")
79      public void setGroups(List<Group> groups)
80      {
81          this.groups = groups;
82      }
83  
84      /**
85       * Get a XmlGroup with the given name. It finds both
86       * qualified and unqualified names in this package.
87       *
88       * @param groupName a <code>String</code> value
89       * @return a <code>Group</code> value
90       * @throws IntakeException indicates that the groupName was null
91       */
92      public Group getGroup(String groupName)
93              throws IntakeException
94      {
95          if (groupName == null)
96          {
97              throw new IntakeException(
98                      "Intake AppData.getGroup(groupName) is null");
99          }
100 
101         String groupPrefix = getGroupPrefix();
102 
103         for (Group group : groups)
104         {
105             if (group.getIntakeGroupName().equals(groupName))
106             {
107                 return group;
108             }
109             if (groupPrefix != null)
110             {
111                 StringBuilder qualifiedGroupName = new StringBuilder();
112 
113                 qualifiedGroupName.append(groupPrefix)
114                         .append(':')
115                         .append(group.getIntakeGroupName());
116 
117                 if (qualifiedGroupName.toString().equals(groupName))
118                 {
119                     return group;
120                 }
121             }
122         }
123         return null;
124     }
125 
126     /**
127      * Get the base package String that will be appended to
128      * any mapToObjects
129      *
130      * @return value of basePackage.
131      */
132     public String getBasePackage()
133     {
134         return basePackage;
135     }
136 
137     /**
138      * Set the base package String that will be appended to
139      * any mapToObjects
140      *
141      * @param v  Value to assign to basePackage.
142      */
143     @XmlAttribute
144     public void setBasePackage(String v)
145     {
146         if (v == null)
147         {
148             this.basePackage = "";
149         }
150         else
151         {
152             if (v.endsWith("."))
153             {
154                 this.basePackage = v;
155             }
156             else
157             {
158                 this.basePackage = v + ".";
159             }
160         }
161 
162     }
163 
164     /**
165      * Get the prefix String that will be used to qualify
166      * intake groups when using multiple XML files
167      *
168      * @return value of groupPrefix
169      */
170     public String getGroupPrefix()
171     {
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     @XmlAttribute
182     public void setGroupPrefix(String groupPrefix)
183     {
184         this.groupPrefix = groupPrefix;
185     }
186 
187     /**
188      * Creates a string representation of this AppData.
189      * The representation is given in xml format.
190      */
191     @Override
192     public String toString()
193     {
194         StringBuilder result = new StringBuilder();
195 
196         result.append("<input-data>\n");
197         for (Group group : groups)
198         {
199             result.append(group);
200         }
201         result.append("</input-data>");
202         return result.toString();
203     }
204 }