001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: URLFormat.java,v $
031: * Revision 1.4 2005/10/02 14:06:33 colinmacleod
032: * Added/improved log4j logging.
033: *
034: * Revision 1.3 2005/04/11 14:45:40 colinmacleod
035: * Changed HTMLFormat from an abstract class
036: * into an interface.
037: *
038: * Revision 1.2 2005/04/09 18:04:18 colinmacleod
039: * Changed copyright text to GPL v2 explicitly.
040: *
041: * Revision 1.1 2005/01/06 22:41:01 colinmacleod
042: * Moved up a version number.
043: * Changed copyright notices to 2005.
044: * Updated the documentation:
045: * - started working on multiproject:site docu.
046: * - changed the logo.
047: * Added checkstyle and fixed LOADS of style issues.
048: * Added separate thirdparty subproject.
049: * Added struts (in web), util and webgui (in webtheme) from ivata op.
050: *
051: * Revision 1.3 2004/03/21 21:16:37 colinmacleod
052: * Shortened name to ivata op.
053: *
054: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
055: * Added full names to author tags
056: *
057: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
058: * Moved ivata op to SourceForge.
059: *
060: * Revision 1.2 2003/10/15 14:13:39 colin
061: * Fixes for XDoclet.
062: *
063: * Revision 1.1 2003/02/24 19:33:33 colin
064: * Moved to new subproject.
065: *
066: * Revision 1.2 2003/02/04 17:43:46 colin
067: * copyright notice
068: *
069: * Revision 1.1 2002/09/25 12:29:28 colin
070: * first version - used for forming URLs to pass to the compose page
071: * -----------------------------------------------------------------------------
072: */
073: package com.ivata.mask.web.format;
074:
075: import org.apache.log4j.Logger;
076:
077: import java.io.UnsupportedEncodingException;
078: import java.net.URLEncoder;
079:
080: /**
081: * <p>
082: * Format a URL by encoding the characters.
083: * </p>
084: *
085: * <p>
086: * <i>ASCII </i> characters 'a' through 'z', 'A' through 'Z', and '0' through
087: * '9' remain the same. So do the unreserved characters - _ . ! ~ * ' ().
088: * </p>
089: *
090: * <p>
091: * All other ASCII characters are converted into the form <code>"%ab"</code>
092: * where <code>ab</code> is the hex value of the character code.
093: * </p>
094: *
095: * <p>
096: * <b>Note: </b> we used to have our own implementation, but this is now just a
097: * wrapper for {@link java.net.URLEncoder}.
098: * </p>
099: *
100: * @since ivata masks 0.4 (2002-09-24)
101: * @author Colin MacLeod
102: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
103: * @version $Revision: 1.4 $
104: */
105: public class URLFormat implements HTMLFormat {
106: /**
107: * Logger for this class.
108: */
109: private static final Logger logger = Logger
110: .getLogger(URLFormat.class);
111:
112: /**
113: * <p>
114: * Convert the given URL by converting the disallowed characters into
115: * two-byte hex representations, preceded by '%'.
116: * </p>
117: *
118: * @param uRLText
119: * URL to be converted.
120: * @return a string representing the URL, containing only allowed
121: * characters.
122: */
123: public final String format(final String uRLText) {
124: if (logger.isDebugEnabled()) {
125: logger.debug("format(String uRLText = " + uRLText
126: + ") - start");
127: }
128:
129: try {
130: String returnString = URLEncoder.encode(uRLText, "UTF-8");
131: if (logger.isDebugEnabled()) {
132: logger.debug("format(String) - end - return value = "
133: + returnString);
134: }
135: return returnString;
136: } catch (UnsupportedEncodingException e) {
137: logger.error("format(String)", e);
138:
139: e.printStackTrace();
140: throw new RuntimeException(e);
141: }
142: }
143: }
|