View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: URLFormat.java,v $
31   * Revision 1.3  2005/04/11 14:45:40  colinmacleod
32   * Changed HTMLFormat from an abstract class
33   * into an interface.
34   *
35   * Revision 1.2  2005/04/09 18:04:18  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.1  2005/01/06 22:41:01  colinmacleod
39   * Moved up a version number.
40   * Changed copyright notices to 2005.
41   * Updated the documentation:
42   *   - started working on multiproject:site docu.
43   *   - changed the logo.
44   * Added checkstyle and fixed LOADS of style issues.
45   * Added separate thirdparty subproject.
46   * Added struts (in web), util and webgui (in webtheme) from ivata op.
47   *
48   * Revision 1.3  2004/03/21 21:16:37  colinmacleod
49   * Shortened name to ivata op.
50   *
51   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
52   * Added full names to author tags
53   *
54   * Revision 1.1.1.1  2004/01/27 20:59:48  colinmacleod
55   * Moved ivata op to SourceForge.
56   *
57   * Revision 1.2  2003/10/15 14:13:39  colin
58   * Fixes for XDoclet.
59   *
60   * Revision 1.1  2003/02/24 19:33:33  colin
61   * Moved to new subproject.
62   *
63   * Revision 1.2  2003/02/04 17:43:46  colin
64   * copyright notice
65   *
66   * Revision 1.1  2002/09/25 12:29:28  colin
67   * first version - used for forming URLs to pass to the compose page
68   * -----------------------------------------------------------------------------
69   */
70  package com.ivata.mask.web.format;
71  import java.io.UnsupportedEncodingException;
72  import java.net.URLEncoder;
73  /***
74   * <p>
75   * Format a URL by encoding the characters.
76   * </p>
77   *
78   * <p>
79   * <i>ASCII </i> characters 'a' through 'z', 'A' through 'Z', and '0' through
80   * '9' remain the same. So do the unreserved characters - _ . ! ~ * ' ().
81   * </p>
82   *
83   * <p>
84   * All other ASCII characters are converted into the form <code>"%ab"</code>
85   * where <code>ab</code> is the hex value of the character code.
86   * </p>
87   *
88   * <p>
89   * <b>Note: </b> we used to have our own implementation, but this is now just a
90   * wrapper for {@link java.net.URLEncoder}.
91   * </p>
92   *
93   * @since ivata masks 0.4 (2002-09-24)
94   * @author Colin MacLeod
95   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
96   * @version $Revision: 1.3 $
97   */
98  public class URLFormat implements HTMLFormat {
99      /***
100      * <p>
101      * Convert the given URL by converting the disallowed characters into
102      * two-byte hex representations, preceded by '%'.
103      * </p>
104      *
105      * @param uRLText
106      *            URL to be converted.
107      * @return a string representing the URL, containing only allowed
108      *         characters.
109      */
110     public final String format(final String uRLText) {
111         try {
112             return URLEncoder.encode(uRLText, "UTF-8");
113         } catch (UnsupportedEncodingException e) {
114             e.printStackTrace();
115             throw new RuntimeException(e);
116         }
117     }
118 }
119