2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.shale.clay.convert;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.convert.Converter;
25 import javax.faces.convert.ConverterException;
26
27 import org.apache.shale.util.Messages;
28
29 /***
30 * <p>Converts a <code>String</code> to a <code>String[]</code> or
31 * vise versa. This converter is targeted at supporting the
32 * myfaces trinidad partialTriggers component properties.
33 * The default element <code>delimiter</code> is none and the default
34 * element <code>separator</code> is a space.</p>
35 */
36 public class StringArrayConverter implements Converter {
37
38 /***
39 * <p>
40 * Message resources for this class.
41 * </p>
42 */
43 private static Messages messages = new Messages(
44 "org.apache.shale.clay.Bundle", StringArrayConverter.class
45 .getClassLoader());
46
47 /***
48 * <p>Begining and ending element delimiter. The default
49 * is <code>null</code> meaning no delimiter.</p>
50 */
51 private Character delimiter = null;
52
53 /***
54 * <p>The element seperator. The default is a space.</p>
55 */
56 private Character separator = new Character(' ');
57
58 /***
59 * @return the element begining and ending delimiter
60 */
61 public Character getDelimiter() {
62 return delimiter;
63 }
64
65 /***
66 * @param delimiter element begining and ending delimiter char
67 */
68 public void setDelimiter(Character delimiter) {
69 this.delimiter = delimiter;
70 }
71
72 /***
73 * @return the character used to delimit elements
74 */
75 public Character getSeparator() {
76 return separator;
77 }
78
79 /***
80 * @param separator the character used to delimit elements
81 */
82 public void setSeparator(Character separator) {
83 this.separator = separator;
84 }
85
86
87 /***
88 * <p>Converts the <code>value</code> into a string array using
89 * the element <code>delimiter</code> and element <code>separator</code>.</p>
90 *
91 * @param facesContext faces context
92 * @param component value holder component
93 * @param value source value to converter to a String[]
94 * @return the target type is a <code>String[]</code>
95 */
96 public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
97
98 List elements = new ArrayList();
99 StringBuffer buff = new StringBuffer(value != null ? value : "");
100 boolean isBlockOn = false;
101 for (int i = buff.length() - 1; i > -1; i--) {
102 if (delimiter != null
103 && buff.charAt(i) == delimiter.charValue()) {
104
105 if (!isBlockOn) {
106 buff.deleteCharAt(i);
107 } else {
108 elements.add(0, buff.substring(i + 1));
109 buff.delete(i, buff.length());
110 }
111 isBlockOn = !isBlockOn;
112 } else if (separator != null
113 && buff.charAt(i) == separator.charValue()
114 && !isBlockOn) {
115
116 if (i + 1 < buff.length()) {
117 elements.add(0, buff.substring(i + 1));
118 }
119 buff.delete(i, buff.length());
120 }
121 }
122
123 if (buff.length() > 0) {
124 elements.add(0, buff.toString());
125 }
126
127 String[] values = new String[elements.size()];
128 elements.toArray(values);
129
130 return values;
131 }
132
133 /***
134 * <p>Converts a string array into a value delimited string. The <code>delimiter</code>
135 * and <code>separator</code> properties are used to distinguish each element.</p>
136 *
137 * @param facesContext faces context
138 * @param component value owning component
139 * @param value source String array that is converter into a tokenized String
140 * @return delimited string holding all values of the source string array
141 */
142 public String getAsString(FacesContext facesContext, UIComponent component,
143 Object value) {
144
145 if (value == null) {
146 return "";
147 }
148
149 if (value instanceof String) {
150 return (String) value;
151 }
152
153 if (!(value instanceof String[])) {
154 throw new ConverterException(messages
155 .getMessage("string.array.converter.invalid.type"));
156 }
157 StringBuffer buff = new StringBuffer();
158 String[] values = (String[]) value;
159 for (int i = 0; i < values.length; i++) {
160 if (i > 0) {
161 buff.append(separator);
162 }
163
164 buff.append(delimiter != null ? delimiter.toString() : "")
165 .append(values[i])
166 .append(delimiter != null ? delimiter.toString() : "");
167 }
168 return buff.toString();
169
170 }
171
172 }