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.convertStringUtils;
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.commons.lang.BooleanUtils;
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.commons.lang.WordUtils;
31  
32  /**
33   * Converts the format of a string
34   * <p>
35   *     Converter which modifies a string by changing the input text into upper case, lower case, capitalized. The
36   *     input string can be optionally trimmed and truncated at some max length. Ellipses (...) can be added to a 
37   *     truncated string.
38   * </p>
39   * <p>
40   * Provides runtime modification of a string. Uses Apache Lang StringUtils and WordUtils
41   * to peform operations.
42   * </p>
43   * 
44   * @JSFConverter
45   *   name = "s:convertStringUtils"
46   *   tagClass = "org.apache.myfaces.custom.convertStringUtils.StringUtilsConverterTag" 
47   *   serialuidtag = "-8532160892717473339L"
48   *   
49   * @author Julian Ray
50   */
51  public class StringUtilsConverter implements Converter, StateHolder {
52      
53      public static final String CONVERTER_ID = "org.apache.myfaces.custom.convertStringUtils.StringUtilsConverter";
54  
55      protected boolean _transient;
56      
57      protected String format = null;
58      protected Boolean trim = null;
59      protected Integer maxLength = null;
60      protected Boolean appendEllipsesDuringOutput = null;
61      protected Boolean appendEllipsesDuringInput = null;
62      
63      public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
64          return null == value ? null : format(value.toString(), false);
65      }
66  
67      public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
68          return value == null ? "" : format(value.toString(), true);
69      }
70  
71      private String format(String val, boolean duringOutput) throws ConverterException {
72  
73          String str;
74          if (BooleanUtils.isTrue(trim)) {
75              str = val.trim();
76          } else {
77              str = val;
78          }
79          // Any decorations first
80          if (StringUtils.isNotEmpty(format)) {
81              if ("uppercase".equalsIgnoreCase(format)) {
82                  str = StringUtils.upperCase(str);
83              } else if ("lowercase".equalsIgnoreCase(format)) {
84                  str = StringUtils.lowerCase(str);
85              } else if ("capitalize".equalsIgnoreCase(format)) {
86                  str = WordUtils.capitalizeFully(str);
87              } else {
88                  throw new ConverterException("Invalid format '" + format + "'");
89              }
90          }
91          
92          boolean appendEllipses = 
93              ((duringOutput)
94                  && ((null != appendEllipsesDuringOutput)
95                  && (appendEllipsesDuringOutput.booleanValue())))
96            || ((false == duringOutput)
97                  && ((null != appendEllipsesDuringInput)
98                  && (appendEllipsesDuringInput.booleanValue()))) ;
99          
100         if (appendEllipses)
101         {
102             // See if we need to abbreviate/truncate this string
103             if (null != maxLength && maxLength.intValue() > 4) {
104                 str = StringUtils.abbreviate(str, maxLength.intValue());
105             }
106         }
107         else
108         {
109             // See if we need to truncate this string
110             if (null != maxLength) {
111                 str = str.substring(0, maxLength.intValue());
112             }
113         }
114         return str;
115     }
116     public void restoreState(FacesContext context, Object state) {
117         Object values[] = (Object[]) state;
118         this.format = (String) values[0];
119         this.trim = (Boolean) values[1];
120         this.maxLength = (Integer) values[2];
121         this.appendEllipsesDuringOutput = (Boolean) values[3];
122         this.appendEllipsesDuringInput = (Boolean) values[4];
123     }
124 
125     public Object saveState(FacesContext context) {
126         Object[] values = new Object[5];
127         values[0] = this.format;
128         values[1] = this.trim;
129         values[2] = this.maxLength;
130         values[3] = this.appendEllipsesDuringOutput;
131         values[4] = this.appendEllipsesDuringInput;
132         return values;
133     }
134     
135     public boolean isTransient() {
136         return _transient;
137     }
138 
139     public void setTransient(boolean _transient) {
140         this._transient = _transient;
141     }
142 
143     /**
144      * Specifies the output case of the string. One of uppercase | lowercase | capitalize
145      * 
146      * @JSFProperty
147      */
148     public String getFormat() {
149         return format;
150     }
151 
152     public void setFormat(String format) {
153         this.format = format;
154     }
155 
156     /**
157      * Integer value for the maximum length of the rendered string. 
158      * Strings longer than maxValue will be truncated at (maxValue - 3) 
159      * and an ellipsis '...' will be appended.
160      * 
161      * @JSFProperty
162      */
163     public Integer getMaxLength() {
164         return maxLength;
165     }
166 
167     public void setMaxLength(Integer maxLength) {
168         this.maxLength = maxLength;
169     }
170 
171     /**
172      * Boolean value determining if data should be truncated with ellipses 
173      * during output conversion. Default = false
174      * 
175      * @JSFProperty
176      */
177     public Boolean isAppendEllipsesDuringOutput() {
178         return appendEllipsesDuringOutput;
179     }
180 
181     public void setAppendEllipsesDuringOutput(Boolean appendEllipsesDuringOutput) {
182         this.appendEllipsesDuringOutput = appendEllipsesDuringOutput;
183     }
184 
185     /**
186      * Boolean value determining if data should be truncated with ellipses 
187      * during input conversion. Default = false
188      * 
189      * @JSFProperty
190      */
191     public Boolean isAppendEllipsesDuringInput() {
192         return appendEllipsesDuringInput;
193     }
194 
195     public void setAppendEllipsesDuringInput(Boolean appendEllipsesDuringInput) {
196         this.appendEllipsesDuringInput = appendEllipsesDuringInput;
197     }
198 
199     /**
200      * Boolean value determining is the string should be trimmed before any 
201      * other formatting takes place. Default = false
202      * 
203      * @JSFProperty
204      */
205     public Boolean getTrim() {
206         return trim;
207     }
208 
209     public void setTrim(Boolean trim) {
210         this.trim = trim;
211     }
212 }