KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > pageload > EstablishDefaultParameterValuesVisitor


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 JavaDoc;
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     public void visitComponent(IComponent component)
43     {
44         IComponentSpecification spec = component.getSpecification();
45
46         Iterator JavaDoc i = spec.getParameterNames().iterator();
47
48         while (i.hasNext())
49         {
50             String JavaDoc name = (String JavaDoc) i.next();
51             IParameterSpecification parameterSpec = spec.getParameter(name);
52
53             // Skip aliases
54

55             if (! name.equals(parameterSpec.getParameterName()))
56                 continue;
57             
58             String JavaDoc defaultValue = parameterSpec.getDefaultValue();
59             if (defaultValue == null)
60                 continue;
61
62             // the parameter has a default value, so it must not be required
63
if (parameterSpec.isRequired())
64                 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             if (component.getBinding(name) == null)
72             {
73                 String JavaDoc description = PageloadMessages.defaultParameterName(name);
74
75                 String JavaDoc defaultBindingType = parameterSpec.getDefaultBindingType();
76                 if (defaultBindingType == null)
77                     defaultBindingType = BindingConstants.OGNL_PREFIX;
78
79                 IBinding binding = _bindingSource.createBinding(
80                         component,
81                         description,
82                         defaultValue,
83                         defaultBindingType,
84                         parameterSpec.getLocation());
85
86                 component.setBinding(name, binding);
87             }
88         }
89     }
90
91     /** @since 4.0 */
92
93     public void setBindingSource(BindingSource bindingSource)
94     {
95         _bindingSource = bindingSource;
96     }
97 }
Popular Tags