View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.web.tag.support;
22  
23  import java.io.UnsupportedEncodingException;
24  import java.net.URLEncoder;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import javax.servlet.jsp.PageContext;
29  
30  /*** 
31   * If you have any problem with decoding URI, please set in Tomcat connector property 
32   * this param  <b>URIEncoding="UTF-8"</b> as well:
33   * <h4>Example</h4>
34   * <p>
35   * &lt;!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 --&gt; 
36   * <BR>&lt;Connector port="8080" 
37   * <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
38   * <b>URIEncoding="UTF-8"</b>&nbsp; ...... /&gt;
39   * </p> 
40   * @author Matthew L. Wilson, Andrej Zachar
41   * @version $Revision: 1.6 $ $Date: 2005/01/26 17:36:57 $
42   */
43  public class DefaultLinkEncoder implements LinkEncoder
44  {
45      
46      private String encoding = "UTF-8";
47  
48  	/***
49  	 * @see net.mlw.vlh.web.tag.support.LinkEncoder#encode(java.util.Map, java.util.Collection, java.util.Collection)
50  	 */
51  	public String encode(PageContext pageContext, Map parameters)
52  	{
53  		StringBuffer sb = new StringBuffer();
54  
55  		try
56  		{
57  			for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();)
58  			{
59  				String key = (String) iter.next();
60  
61  				Object value = parameters.get(key);
62  				if (value instanceof String[])
63  				{
64  					String[] values = (String[]) value;
65  					for (int i = 0; i < values.length; i++)
66  					{
67  						sb.append(key).append("=").append(URLEncoder.encode(values[i], encoding)).append("&amp;");
68  					}
69  				}
70  				else if (value instanceof String)
71  				{
72  					sb.append(key).append("=").append(URLEncoder.encode((String) value, encoding)).append("&amp;");
73  				}
74  				else if (value != null)
75  				{
76  					sb.append(key).append("=").append(URLEncoder.encode(value.toString(), encoding)).append("&amp;");
77  				}
78  
79  			}
80  		}
81  		catch (UnsupportedEncodingException e)
82  		{
83  			throw new RuntimeException(e);
84  		}
85  		return sb.toString();
86  	}
87  
88  	/***
89  	 * @return Returns the encoding.
90  	 */
91  	public String getEncoding()
92  	{
93  		return encoding;
94  	}
95  
96  	/***
97  	 * @param encoding The encoding to set.
98  	 */
99  	public void setEncoding(String encoding)
100 	{
101 		this.encoding = encoding;
102 	}
103 }