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  package org.apache.myfaces.convert;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  import javax.faces.convert.Converter;
24  import javax.faces.convert.ConverterException;
25  import java.io.UnsupportedEncodingException;
26  import java.net.URLDecoder;
27  import java.net.URLEncoder;
28  import java.util.StringTokenizer;
29  
30  /**
31   * @author Thomas Spiegl (latest modification by $Author: grantsmith $)
32   * @version $Revision: 472630 $ $Date: 2006-11-08 15:40:03 -0500 (Wed, 08 Nov 2006) $
33   */
34  public class StringArrayConverter
35      implements Converter
36  {
37      public Object getAsObject(FacesContext context, UIComponent component, String value)
38          throws ConverterException
39      {
40          try
41          {
42              StringTokenizer tokenizer = new StringTokenizer(value, ",");
43              String[] newValue = new String[tokenizer.countTokens()];
44              for (int i = 0; tokenizer.hasMoreTokens(); i++)
45              {
46                  newValue[i] = URLDecoder.decode(tokenizer.nextToken(), "UTF-8");
47              }
48              return newValue;
49          }
50          catch (UnsupportedEncodingException e)
51          {
52              throw new RuntimeException(e);
53          }
54      }
55  
56      public String getAsString(FacesContext context, UIComponent component, Object value)
57              throws ConverterException
58      {
59          return getAsString((String[])value,
60                             true);   //escapeCommas
61      }
62  
63  
64      public static String getAsString(String[] strings,
65                                       boolean escapeCommas)
66      {
67          try
68          {
69              if (strings == null || strings.length == 0)
70              {
71                  return null;
72              }
73              else if (strings.length == 1)
74              {
75  
76                  return escapeCommas
77                          ? URLEncoder.encode(strings[0], "UTF-8") //Encode, so that commas within Strings are escaped
78                          : strings[0];
79              }
80              else
81              {
82                  StringBuffer buf = new StringBuffer();
83                  for (int i = 0; i < strings.length; i++)
84                  {
85                      if (i > 0)
86                      {
87                          buf.append(',');
88                      }
89  
90                      String s = strings[i];
91  
92                      if(s!=null)
93                      {
94                          if (escapeCommas)
95                          {
96                              //Encode, so that commas within Strings are escaped
97                              s = URLEncoder.encode(s, "UTF-8");
98                          }
99                          buf.append(s);
100                     }
101                 }
102                 return buf.toString();
103             }
104         }
105         catch (UnsupportedEncodingException e)
106         {
107             throw new RuntimeException(e);
108         }
109     }
110 
111 }