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.custom.convertboolean;
21  
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.StateHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.convert.Converter;
26  import javax.faces.convert.ConverterException;
27  
28  /**
29   * Converter that translates between boolean values (true/false)
30   * and alternate versions of those boolean values like
31   * (yes/no), (1/0), and (way/no way).
32   * <p/>
33   * To customize the representation of a boolean true and false,
34   * use {@link #setTrueValue(String)}
35   * and {@link #setFalseValue(String)}
36   * respectively.  If  not configured with these setter methods,
37   * it defaults to <code>true</code> and <code>false</code>.
38   * <p/>
39   * The values are case sensitive.
40   * <p/>
41   *
42   * @JSFConverter
43   *   name = "s:convertBoolean"
44   *   tagClass = "org.apache.myfaces.custom.convertboolean.ConvertBooleanTag"
45   *   serialuidtag = "-6004262065580818687L" 
46   *   
47   * @author Ken Weiner
48   */
49  public class BooleanConverter implements Converter, StateHolder
50  {
51  
52      private String trueValue = "true";
53      private String falseValue = "false";
54  
55      private boolean isTransient;
56  
57      public static final String CONVERTER_ID = "org.apache.myfaces.custom.convertboolean.BooleanConverter";
58  
59      public BooleanConverter()
60      {
61          // Default constructor
62      }
63  
64      /**
65       * Value representing a boolean false, e.g. FALSE, no, 0, etc.
66       * 
67       * @JSFProperty
68       */
69      public String getFalseValue()
70      {
71          return falseValue;
72      }
73  
74      public void setFalseValue(String falseValue)
75      {
76          this.falseValue = falseValue;
77      }
78  
79      /**
80       * Value representing a boolean true, e.g. TRUE, yes, 1, etc.
81       * 
82       * @JSFProperty
83       */
84      public String getTrueValue()
85      {
86          return trueValue;
87      }
88  
89      public void setTrueValue(String trueValue)
90      {
91          this.trueValue = trueValue;
92      }
93  
94      public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
95              throws ConverterException
96      {
97          if (facesContext == null)
98          {
99              throw new NullPointerException("facesContext");
100         }
101         if (uiComponent == null)
102         {
103             throw new NullPointerException("uiComponent");
104         }
105 
106         if (value != null)
107         {
108             value = value.trim();
109             if (value.length() > 0)
110             {
111                 try
112                 {
113                     return Boolean.valueOf(value.equals(trueValue));
114                 }
115                 catch (Exception e)
116                 {
117                     throw new ConverterException(e);
118                 }
119             }
120         }
121         return null;
122     }
123 
124     public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
125             throws ConverterException
126     {
127         if (facesContext == null)
128         {
129             throw new NullPointerException("facesContext");
130         }
131         if (uiComponent == null)
132         {
133             throw new NullPointerException("uiComponent");
134         }
135 
136         if (value == null)
137         {
138             return "";
139         }
140         if (value instanceof String)
141         {
142             return (String) value;
143         }
144         try
145         {
146             return ((Boolean) value).booleanValue() ? trueValue : falseValue;
147         }
148         catch (Exception e)
149         {
150             throw new ConverterException(e);
151         }
152     }
153 
154     // StateHolder methods ////////////////////////////////////////////////////
155 
156     public boolean isTransient()
157     {
158         return this.isTransient;
159     }
160 
161     public void setTransient(boolean newTransientValue)
162     {
163         this.isTransient = newTransientValue;
164     }
165 
166     public void restoreState(FacesContext context, Object state)
167     {
168         Object values[] = (Object[]) state;
169         this.trueValue = (String) values[0];
170         this.falseValue = (String) values[1];
171     }
172 
173     public Object saveState(FacesContext context)
174     {
175         Object[] values = new Object[2];
176         values[0] = this.trueValue;
177         values[1] = this.falseValue;
178         return values;
179     }
180 
181 
182 }