1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
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
103 if (null != maxLength && maxLength.intValue() > 4) {
104 str = StringUtils.abbreviate(str, maxLength.intValue());
105 }
106 }
107 else
108 {
109
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
145
146
147
148 public String getFormat() {
149 return format;
150 }
151
152 public void setFormat(String format) {
153 this.format = format;
154 }
155
156
157
158
159
160
161
162
163 public Integer getMaxLength() {
164 return maxLength;
165 }
166
167 public void setMaxLength(Integer maxLength) {
168 this.maxLength = maxLength;
169 }
170
171
172
173
174
175
176
177 public Boolean isAppendEllipsesDuringOutput() {
178 return appendEllipsesDuringOutput;
179 }
180
181 public void setAppendEllipsesDuringOutput(Boolean appendEllipsesDuringOutput) {
182 this.appendEllipsesDuringOutput = appendEllipsesDuringOutput;
183 }
184
185
186
187
188
189
190
191 public Boolean isAppendEllipsesDuringInput() {
192 return appendEllipsesDuringInput;
193 }
194
195 public void setAppendEllipsesDuringInput(Boolean appendEllipsesDuringInput) {
196 this.appendEllipsesDuringInput = appendEllipsesDuringInput;
197 }
198
199
200
201
202
203
204
205 public Boolean getTrim() {
206 return trim;
207 }
208
209 public void setTrim(Boolean trim) {
210 this.trim = trim;
211 }
212 }