org.norther.tammi.acorn.net
Class URLEncoder

java.lang.Object
  extended by org.norther.tammi.acorn.net.URLEncoder

public abstract class URLEncoder
extends Object

A static URL encoder. It encodes strings as specified in RFC3986 Unified Resource Identifier.

Unreserved characters include 'a'-'z', 'A' - 'Z', '0' - '9', '-', '.', '_', and '~' in RFC3986. In addition, characters '!', '$, '&', ''', '(', ')', '*', '+', ',', ';' and '=' may be applied as sub-delimiters. The HTTP scheme reserves '?', '&', '=', '+' and ';' of these. From the general delimiters ':', '/', '?', '#', '[', ']' and '@', characters ':' and '@' may be applied within a path segment of an absolute URI, ':' is not allowed within relative URIs.

Reserved characters and HTTP scheme specific gen-delimiters and sub-delimiters are decoded as character triplets consisting of the character '%' followed by the two hexadecimal digits, which form the hexadecimal value of the octet.

Encoded characters that are longer than 8-bits can be decoded back to UTF strings reliably only with the UTF-8 character encoding.

The encoder uses the platform's default character encoding if nothing else is specified.

Version:
$Id: URLEncoder.java,v 1.31 2010-05-05 20:15:26 cvsimp Exp $
Author:
Ilkka Priha

Field Summary
static String AMPERSAND
          XML references (old WML browsers required an XML entity references, newer ones don't work with it!?) We apply the one required by the HTML DTD.
static char SLASH_COUNT_DELIMITER
          The slash count delimiter for keys.
 
Constructor Summary
URLEncoder()
           
 
Method Summary
static String encode(Map<String,?> map, int offset, int count, String... encoding)
          Encodes part of a map of URL parameters using the specified encoding.
static String encode(Map<String,?> map, String... encoding)
          Encodes a map of URL parameters using the specified encoding.
protected static String encode(String str, char[] chars, int offset, int count, boolean ispath, StringBuilder buffer, String... encoding)
          Encodes a URL parameter.
static String encode(String param, String... encoding)
          Encodes a URL parameter using the specified encoding.
static String encodePath(String path, String... encoding)
          Encodes a URL path using the specified encoding.
static String encodePathInfo(Map<String,?> map, int offset, int count, String... encoding)
          Encodes part of a map of URL path parameters using the specified encoding.
static String encodePathInfo(Map<String,?> map, String... encoding)
          Encodes a map of URL path parameters using the specified encoding.
static String encodePathInfo(String name, String[] value, String... encoding)
          Encodes a URL path parameters using the specified encoding.
static String encodePathInfo(String name, String value, String... encoding)
          Encodes a URL path parameter using the specified encoding.
static int keySlashCount(String value)
          Returns the slash count of the specified value to be appended to key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AMPERSAND

public static final String AMPERSAND
XML references (old WML browsers required an XML entity references, newer ones don't work with it!?) We apply the one required by the HTML DTD.

See Also:
Constant Field Values

SLASH_COUNT_DELIMITER

public static final char SLASH_COUNT_DELIMITER
The slash count delimiter for keys.

See Also:
Constant Field Values
Constructor Detail

URLEncoder

public URLEncoder()
Method Detail

encode

public static final String encode(String param,
                                  String... encoding)
Encodes a URL parameter using the specified encoding.

Parameters:
param - a URL parameter.
encoding - the encoding.
Returns:
the encoded parameter.

encode

public static final String encode(Map<String,?> map,
                                  String... encoding)
Encodes a map of URL parameters using the specified encoding. The keys in the map must be strings and values either strings or string arrays.

Parameters:
map - a map of URL parameters.
encoding - the encoding.
Returns:
encoded parameters without the leading '?' or null.

encode

public static final String encode(Map<String,?> map,
                                  int offset,
                                  int count,
                                  String... encoding)
Encodes part of a map of URL parameters using the specified encoding. The keys in the map must be strings and values either strings or string arrays.

Parameters:
map - a map of URL parameters.
offset - the initial offset.
count - the number keys to process (-1 = all).
encoding - the encoding and parameter separator.
Returns:
encoded parameters without the leading '?' or null.

encodePath

public static final String encodePath(String path,
                                      String... encoding)
Encodes a URL path using the specified encoding.

Parameters:
path - a URL path.
encoding - the encoding.
Returns:
the encoded path.

encodePathInfo

public static final String encodePathInfo(String name,
                                          String value,
                                          String... encoding)
Encodes a URL path parameter using the specified encoding.

Parameters:
name - the parameter name.
value - the parameter value.
encoding - the encoding.
Returns:
the encoded path parameter.

encodePathInfo

public static final String encodePathInfo(String name,
                                          String[] value,
                                          String... encoding)
Encodes a URL path parameters using the specified encoding.

Parameters:
name - the parameter name.
value - the parameter values.
encoding - the encoding.
Returns:
the encoded path parameter.

encodePathInfo

public static final String encodePathInfo(Map<String,?> map,
                                          String... encoding)
Encodes a map of URL path parameters using the specified encoding. The keys in the map must be strings and values either strings or string arrays.

Parameters:
map - a map of path components.
encoding - the encoding.
Returns:
the encoded path with the leading '/' or null.

encodePathInfo

public static final String encodePathInfo(Map<String,?> map,
                                          int offset,
                                          int count,
                                          String... encoding)
Encodes part of a map of URL path parameters using the specified encoding. The keys in the map must be strings and values either strings or string arrays.

Parameters:
map - a map of path components.
offset - the initial offset.
count - the number keys to process (-1 = all).
encoding - the encoding.
Returns:
the encoded path with the leading '/' or null.

keySlashCount

public static final int keySlashCount(String value)
Returns the slash count of the specified value to be appended to key.

Parameters:
value - the value to count.
Returns:
the key slash count.

encode

protected static final String encode(String str,
                                     char[] chars,
                                     int offset,
                                     int count,
                                     boolean ispath,
                                     StringBuilder buffer,
                                     String... encoding)
Encodes a URL parameter.

Parameters:
str - a URL parameter as a string.
chars - a URL parameter as a char array.
offset - the start offset in the array.
count - the number of characters to encode.
ispath - true for path parameters.
buffer - an optional temporary buffer.
encoding - the encoding.
Returns:
the encoded parameter.


Copyright © 2004 The Norther Organization. All rights reserved.