|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
EstablishDefaultParameterValuesVisitor.java | 83.3% | 90% | 100% | 88.2% |
|
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.pageload; | |
16 | ||
17 | import java.util.Iterator; | |
18 | ||
19 | import org.apache.hivemind.ApplicationRuntimeException; | |
20 | import org.apache.tapestry.IBinding; | |
21 | import org.apache.tapestry.IComponent; | |
22 | import org.apache.tapestry.binding.BindingConstants; | |
23 | import org.apache.tapestry.binding.BindingSource; | |
24 | import org.apache.tapestry.spec.IComponentSpecification; | |
25 | import org.apache.tapestry.spec.IParameterSpecification; | |
26 | ||
27 | /** | |
28 | * For all parameters in the examined component that have default values, but are not bound, | |
29 | * automatically add an ExpressionBinding with the default value. | |
30 | * | |
31 | * @author mindbridge | |
32 | * @since 3.0 | |
33 | */ | |
34 | public class EstablishDefaultParameterValuesVisitor implements IComponentVisitor | |
35 | { | |
36 | /** @since 4.0 */ | |
37 | private BindingSource _bindingSource; | |
38 | ||
39 | /** | |
40 | * @see org.apache.tapestry.pageload.IComponentVisitor#visitComponent(org.apache.tapestry.IComponent) | |
41 | */ | |
42 | 1067 | public void visitComponent(IComponent component) |
43 | { | |
44 | 1067 | IComponentSpecification spec = component.getSpecification(); |
45 | ||
46 | 1067 | Iterator i = spec.getParameterNames().iterator(); |
47 | ||
48 | 1067 | while (i.hasNext()) |
49 | { | |
50 | 3362 | String name = (String) i.next(); |
51 | 3362 | IParameterSpecification parameterSpec = spec.getParameter(name); |
52 | ||
53 | // Skip aliases | |
54 | ||
55 | 3362 | if (! name.equals(parameterSpec.getParameterName())) |
56 | 0 | continue; |
57 | ||
58 | 3362 | String defaultValue = parameterSpec.getDefaultValue(); |
59 | 3362 | if (defaultValue == null) |
60 | 2869 | continue; |
61 | ||
62 | // the parameter has a default value, so it must not be required | |
63 | 493 | if (parameterSpec.isRequired()) |
64 | 0 | throw new ApplicationRuntimeException(PageloadMessages |
65 | .parameterMustHaveNoDefaultValue(component, name), component, parameterSpec | |
66 | .getLocation(), null); | |
67 | ||
68 | // if there is no binding for this parameter, bind it to the default value. | |
69 | // In 3.0, default-value was always an OGNL expression, but now its a binding reference. | |
70 | ||
71 | 493 | if (component.getBinding(name) == null) |
72 | { | |
73 | 473 | String description = PageloadMessages.defaultParameterName(name); |
74 | ||
75 | 473 | String defaultBindingType = parameterSpec.getDefaultBindingType(); |
76 | 473 | if (defaultBindingType == null) |
77 | 16 | defaultBindingType = BindingConstants.OGNL_PREFIX; |
78 | ||
79 | 473 | IBinding binding = _bindingSource.createBinding( |
80 | component, | |
81 | description, | |
82 | defaultValue, | |
83 | defaultBindingType, | |
84 | parameterSpec.getLocation()); | |
85 | ||
86 | 473 | component.setBinding(name, binding); |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | /** @since 4.0 */ | |
92 | ||
93 | 41 | public void setBindingSource(BindingSource bindingSource) |
94 | { | |
95 | 41 | _bindingSource = bindingSource; |
96 | } | |
97 | } |
|