1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class ToolboxConfiguration
46 extends CompoundConfiguration<ToolConfiguration>
47 {
48 private String scope = ToolboxFactory.DEFAULT_SCOPE;
49
50 public ToolboxConfiguration()
51 {
52
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
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
120 for (ToolConfiguration tool : getTools())
121 {
122
123 for (String invalidScope : tool.getInvalidScopes())
124 {
125 if (getScope().equals(invalidScope))
126 {
127 throw new InvalidScopeException(this, tool);
128 }
129 }
130
131
132
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 }