KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > convert > StringArrayConverter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.convert;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import javax.faces.convert.Converter;
21 import javax.faces.convert.ConverterException;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.URLDecoder JavaDoc;
24 import java.net.URLEncoder JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  * @author Thomas Spiegl (latest modification by $Author: matze $)
29  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:57 $
30  */

31 public class StringArrayConverter
32     implements Converter
33 {
34     public Object JavaDoc getAsObject(FacesContext context, UIComponent component, String JavaDoc value)
35         throws ConverterException
36     {
37         try
38         {
39             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ",");
40             String JavaDoc[] newValue = new String JavaDoc[tokenizer.countTokens()];
41             for (int i = 0; tokenizer.hasMoreTokens(); i++)
42             {
43                 newValue[i] = URLDecoder.decode(tokenizer.nextToken(), "UTF-8");
44             }
45             return newValue;
46         }
47         catch (UnsupportedEncodingException JavaDoc e)
48         {
49             throw new RuntimeException JavaDoc(e);
50         }
51     }
52
53     public String JavaDoc getAsString(FacesContext context, UIComponent component, Object JavaDoc value)
54             throws ConverterException
55     {
56         return getAsString((String JavaDoc[])value,
57                            true); //escapeCommas
58
}
59
60
61     public static String JavaDoc getAsString(String JavaDoc[] strings,
62                                      boolean escapeCommas)
63     {
64         try
65         {
66             if (strings == null || strings.length == 0)
67             {
68                 return null;
69             }
70             else if (strings.length == 1)
71             {
72
73                 return escapeCommas
74                         ? URLEncoder.encode(strings[0], "UTF-8") //Encode, so that commas within Strings are escaped
75
: strings[0];
76             }
77             else
78             {
79                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
80                 for (int i = 0; i < strings.length; i++)
81                 {
82                     if (i > 0)
83                     {
84                         buf.append(',');
85                     }
86                     String JavaDoc s = strings[i];
87                     if (escapeCommas)
88                     {
89                         //Encode, so that commas within Strings are escaped
90
s = URLEncoder.encode(s, "UTF-8");
91                     }
92                     buf.append(s);
93                 }
94                 return buf.toString();
95             }
96         }
97         catch (UnsupportedEncodingException JavaDoc e)
98         {
99             throw new RuntimeException JavaDoc(e);
100         }
101     }
102
103 }
104
Popular Tags