View Javadoc

1   package org.apache.velocity.tools.config;
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.util.Collection;
23  import java.util.SortedSet;
24  import org.apache.velocity.tools.Scope;
25  import org.apache.velocity.tools.ToolboxFactory;
26  
27  /**
28   * <p>This class handles configuration info for the Toolbox instances
29   * that will eventually be produced by {@link ToolboxFactory}.
30   * It contains {@link ToolConfiguration}s for tools which will be managed
31   * by those toolboxes, as well the toolboxes' scope and
32   * any other {@link Property}s which you intend to be available
33   * to all the tools in the toolbox.</p>
34   * <p>
35   * Most users will not find themselves directly using the API of this class.
36   * </p>
37   * <p>NOTE: Two instances of this class are considered equal() if their scopes
38   * are equal. This is not the intuitive behavior at this level but is done
39   * to facilitate intuitive behavior in the higher APIs, which are much more
40   * likely to be used directly.</p>
41   *
42   * @author Nathan Bubna
43   * @version $Id: ToolboxConfiguration.java 511959 2007-02-26 19:24:39Z nbubna $
44   */
45  public class ToolboxConfiguration
46      extends CompoundConfiguration<ToolConfiguration>
47  {
48      private String scope = ToolboxFactory.DEFAULT_SCOPE;
49  
50      public ToolboxConfiguration()
51      {
52          // ensure that even the default scope is set as a property
53          setProperty("scope", this.scope);
54      }
55  
56      public void setScope(String scope)
57      {
58          if (scope == null)
59          {
60              throw new NullPointerException("Toolbox scope cannot be null");
61          }
62          this.scope = scope;
63  
64          // ensure the scope is also set as a property of the toolbox
65          setProperty("scope", scope);
66      }
67  
68      public String getScope()
69      {
70          return this.scope;
71      }
72  
73      public void addTool(ToolConfiguration tool)
74      {
75          addChild(tool);
76      }
77  
78      public void removeTool(ToolConfiguration tool)
79      {
80          removeChild(tool);
81      }
82  
83      public ToolConfiguration getTool(String key)
84      {
85          for (ToolConfiguration tool : getTools())
86          {
87              if (key.equals(tool.getKey()))
88              {
89                  return tool;
90              }
91          }
92          return null;
93      }
94  
95      public Collection<ToolConfiguration> getTools()
96      {
97          return getChildren();
98      }
99  
100     public void setTools(Collection<ToolConfiguration> tools)
101     {
102         setChildren(tools);
103     }
104 
105     @Override
106     public void validate()
107     {
108         super.validate();
109 
110         if (getScope() == null)
111         {
112             throw new ConfigurationException(this, "Toolbox scope cannot be null");
113         }
114         if (!Scope.exists(getScope()))
115         {
116             throw new ConfigurationException(this, "Scope '"+getScope()+"' is not recognized. Please correct or add your new custom scope with "+Scope.class.getName()+".add(\""+getScope()+"\").");
117         }
118 
119         // validate that all tools are allowed in this scope
120         for (ToolConfiguration tool : getTools())
121         {
122             // check if this toolbox's scope has been declared invalid
123             for (String invalidScope : tool.getInvalidScopes())
124             {
125                 if (getScope().equals(invalidScope))
126                 {
127                     throw new InvalidScopeException(this, tool);
128                 }
129             }
130  
131             // if the set of valid scopes has been limited, check to be
132             // sure that this toolbox's scope is in the set
133             String[] validScopes = tool.getValidScopes();
134             if (validScopes != null && validScopes.length > 0)
135             {
136                 boolean found = false;
137                 for (String validScope : validScopes)
138                 {
139                     if (getScope().equals(validScope))
140                     {
141                         found = true;
142                         break;
143                     }
144                 }
145                 if (!found)
146                 {
147                     throw new InvalidScopeException(this, tool);
148                 }
149             }
150         }
151     }
152 
153     @Override
154     public int compareTo(Configuration conf)
155     {
156         if (!(conf instanceof ToolboxConfiguration))
157         {
158             throw new UnsupportedOperationException("ToolboxConfigurations can only be compared to other ToolboxConfigurations");
159         }
160 
161         ToolboxConfiguration toolbox = (ToolboxConfiguration)conf;
162         return getScope().compareTo(toolbox.getScope());
163     }
164 
165     @Override
166     public int hashCode()
167     {
168         return scope.hashCode();
169     }
170 
171     @Override
172     public boolean equals(Object obj)
173     {
174         if (obj instanceof ToolboxConfiguration)
175         {
176             return scope.equals(((ToolboxConfiguration)obj).scope);
177         }
178         return false;
179     }
180 
181     public String toString()
182     {
183         StringBuilder out = new StringBuilder();
184         out.append("Toolbox '");
185         out.append(this.scope);
186         out.append("' ");
187         appendProperties(out);
188         appendChildren(out, "tools: \n  ", "\n  ");
189         return out.toString();
190     }
191 
192 }