001 // Copyright 2006-2007 Regents of the University of California. May be used
002 // under the terms of the revised BSD license. See LICENSING for details.
003 package org.joe_e.charset;
004
005 import java.net.URLEncoder;
006 import java.net.URLDecoder;
007 import java.io.UnsupportedEncodingException;
008
009 /**
010 * Class for converting strings to and from the
011 * <code>application/x-www-form-urlencoded</code> MIME format used for HTML
012 * forms. Uses the UTF-8 character encoding, as specified by W3C. This class
013 * contains static methods for converting strings between human-readable text
014 * form and its corresponding encoding.
015 */
016 public class URLEncoding {
017 private URLEncoding() {}
018
019 /**
020 * URL encode a value.
021 * @param value The value to encode.
022 * @return The encoded URL segment.
023 */
024 static public String encode(final String value) {
025 try {
026 return URLEncoder.encode(value, "UTF-8");
027 } catch (UnsupportedEncodingException e) {
028 throw new AssertionError("UTF-8 encoding not supported"); // Should never happen
029 }
030 }
031
032 /**
033 * URL decode a segment.
034 * @param segment The segment to decode.
035 * @return The decoded value.
036 */
037 static public String decode(final String segment) {
038 try {
039 return URLDecoder.decode(segment, "UTF-8");
040 } catch (final UnsupportedEncodingException e) {
041 throw new AssertionError("UTF-8 encoding not supported"); // Should never happen
042 }
043 }
044 }