2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.validator.tag;
19  
20  import javax.faces.context.FacesContext;
21  import javax.faces.el.ValueBinding;
22  import javax.faces.validator.Validator;
23  import javax.faces.webapp.UIComponentTag;
24  import javax.faces.webapp.ValidatorTag;
25  import org.apache.shale.util.ConverterHelper;
26  import org.apache.shale.validator.validator.DoubleValidator;
27  
28  /***
29   * <p>JSP custom action for <code>DoubleValidator</code>.</p>
30   */
31  public final class DoubleValidatorTag extends ValidatorTag {
32      
33  
34      // -------------------------------------------------------- Static Variables
35  
36  
37      /***
38       * Serial version UID.
39       */
40      private static final long serialVersionUID = 1L;
41  
42  
43      /***
44       * <p>Helper object for performing conversions.</p>
45       */
46      private static final ConverterHelper HELPER =
47              new ConverterHelper();
48  
49  
50      // -------------------------------------------------------------- Properties
51  
52  
53      private String client = null;
54      public void setClient(String client) {
55          this.client = client;
56      }
57  
58  
59      private String maximum = null;
60      public void setMaximum(String maximum) {
61          this.maximum = maximum;
62      }
63  
64  
65      private String message = null;
66      public void setMessage(String message) {
67          this.message = message;
68      }
69  
70  
71      private String minimum = null;
72      public void setMinimum(String minimum) {
73          this.minimum = minimum;
74      }
75  
76  
77      // ------------------------------------------------------------- Tag Methods
78  
79  
80      /***
81       * <p>Release resources allocated for this instance.</p>
82       */
83      public void release() {
84  
85          client = null;
86          maximum = null;
87          message = null;
88          minimum = null;
89  
90          super.release();
91  
92      }
93  
94  
95      // ---------------------------------------------------- ValidatorTag Methods
96  
97  
98      /***
99       * <p>Create and return a new <code>Validator</code> instance to be
100      * registered on our corresponding <code>UIComponent</code>.</p>
101      */
102     protected Validator createValidator() {
103 
104         FacesContext context = FacesContext.getCurrentInstance();
105         DoubleValidator validator = new DoubleValidator();
106 
107         if (client != null) {
108             Boolean value = null;
109             if (UIComponentTag.isValueReference(client)) {
110                 ValueBinding vb = context.getApplication().createValueBinding(client);
111                 value = (Boolean) vb.getValue(context);
112             } else {
113                 value = (Boolean) HELPER.asObject(context, Boolean.class, client);
114             }
115             validator.setClient(value.booleanValue());
116         }
117 
118         if (maximum != null) {
119             Double value = null;
120             if (UIComponentTag.isValueReference(maximum)) {
121                 ValueBinding vb = context.getApplication().createValueBinding(maximum);
122                 value = (Double) vb.getValue(context);
123             } else {
124                 value = (Double) HELPER.asObject(context, Double.class, maximum);
125             }
126             validator.setMaximum(value.doubleValue());
127         }
128 
129         if (message != null) {
130             String value = null;
131             if (UIComponentTag.isValueReference(message)) {
132                 ValueBinding vb = context.getApplication().createValueBinding(message);
133                 value = (String) vb.getValue(context);
134             } else {
135                 value = message;
136             }
137             validator.setMessage(value);
138         }
139 
140         if (minimum != null) {
141             Double value = null;
142             if (UIComponentTag.isValueReference(minimum)) {
143                 ValueBinding vb = context.getApplication().createValueBinding(minimum);
144                 value = (Double) vb.getValue(context);
145             } else {
146                 value = (Double) HELPER.asObject(context, Double.class, minimum);
147             }
148             validator.setMinimum(value.doubleValue());
149         }
150 
151         return validator;
152 
153     }
154 
155 
156 }