View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.commons.converter;
21  
22  import javax.faces.component.StateHolder;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.convert.Converter;
26  import javax.faces.convert.ConverterException;
27  
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter;
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
30  
31  /**
32   * Converter that translates between boolean values (true/false)
33   * and alternate versions of those boolean values like
34   * (yes/no), (1/0), and (way/no way).
35   * <p/>
36   * To customize the representation of a boolean true and false,
37   * use {@link #setTrueValue(String)}
38   * and {@link #setFalseValue(String)}
39   * respectively.  If  not configured with these setter methods,
40   * it defaults to <code>true</code> and <code>false</code>.
41   * <p/>
42   * The values are case sensitive.
43   * <p/>
44   *
45   * @author Ken Weiner
46   */
47  @JSFConverter(
48     name = "mcc:convertBoolean",
49     tagClass = "org.apache.myfaces.commons.converter.ConvertBooleanTag",
50     serialuidtag = "-6004262065580818687L")
51  public class BooleanConverter implements Converter, StateHolder
52  {
53  
54      private String trueValue = "true";
55      private String falseValue = "false";
56  
57      private boolean isTransient;
58  
59      public static final String CONVERTER_ID = "org.apache.myfaces.custom.convertboolean.BooleanConverter";
60  
61      public BooleanConverter()
62      {
63          // Default constructor
64      }
65  
66      /**
67       * Value representing a boolean false, e.g. FALSE, no, 0, etc.
68       * 
69       */
70      @JSFProperty
71      public String getFalseValue()
72      {
73          return falseValue;
74      }
75  
76      public void setFalseValue(String falseValue)
77      {
78          this.falseValue = falseValue;
79      }
80  
81      /**
82       * Value representing a boolean true, e.g. TRUE, yes, 1, etc.
83       * 
84       */
85      @JSFProperty
86      public String getTrueValue()
87      {
88          return trueValue;
89      }
90  
91      public void setTrueValue(String trueValue)
92      {
93          this.trueValue = trueValue;
94      }
95  
96      public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
97              throws ConverterException
98      {
99          if (facesContext == null)
100         {
101             throw new NullPointerException("facesContext");
102         }
103         if (uiComponent == null)
104         {
105             throw new NullPointerException("uiComponent");
106         }
107 
108         if (value != null)
109         {
110             value = value.trim();
111             if (value.length() > 0)
112             {
113                 try
114                 {
115                     return Boolean.valueOf(value.equals(trueValue));
116                 }
117                 catch (Exception e)
118                 {
119                     throw new ConverterException(e);
120                 }
121             }
122         }
123         return null;
124     }
125 
126     public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
127             throws ConverterException
128     {
129         if (facesContext == null)
130         {
131             throw new NullPointerException("facesContext");
132         }
133         if (uiComponent == null)
134         {
135             throw new NullPointerException("uiComponent");
136         }
137 
138         if (value == null)
139         {
140             return "";
141         }
142         if (value instanceof String)
143         {
144             return (String) value;
145         }
146         try
147         {
148             return ((Boolean) value).booleanValue() ? trueValue : falseValue;
149         }
150         catch (Exception e)
151         {
152             throw new ConverterException(e);
153         }
154     }
155 
156     // StateHolder methods ////////////////////////////////////////////////////
157 
158     public boolean isTransient()
159     {
160         return this.isTransient;
161     }
162 
163     public void setTransient(boolean newTransientValue)
164     {
165         this.isTransient = newTransientValue;
166     }
167 
168     public void restoreState(FacesContext context, Object state)
169     {
170         Object values[] = (Object[]) state;
171         this.trueValue = (String) values[0];
172         this.falseValue = (String) values[1];
173     }
174 
175     public Object saveState(FacesContext context)
176     {
177         Object[] values = new Object[2];
178         values[0] = this.trueValue;
179         values[1] = this.falseValue;
180         return values;
181     }
182 
183 
184 }