com.opencms.util
Class Encoder

java.lang.Object
  |
  +--com.opencms.util.Encoder

public class Encoder
extends Object

The OpenCms Encoder class provides static methods to decode and encode data.

The methods in this class are substitutes for java.net.URLEncoder.encode() and java.net.URLDecoder.decode(). Use the methods from this class in all OpenCms core classes to ensure the encoding is always handled the same way.

The de- and encoding uses the same coding mechanism as JavaScript, special characters are replaxed with %hex where hex is a two digit hex number.

Note: On the client side (browser) instead of using corresponding escape and unescape JavaScript functions, better use encodeURIComponent and decodeURIComponent functions wich are work properly with unicode characters. These functions are supported in IE 5.5+ and NS 6+ only.

Author:
Michael Emmerich, Alexander Kandzior (a.kandzior@alkacon.com)

Field Summary
static String C_URI_ENCODING
          Default encoding for JavaScript decodeUriComponent methods is UTF-8 by w3c standard
 
Constructor Summary
Encoder()
          Constructor
 
Method Summary
static byte[] changeEncoding(byte[] input, String oldEncoding, String newEncoding)
          Changes the encoding of a byte array that represents a String.
static String decode(String source)
          Decodes a String using the default encoding.
static String decode(String source, String encoding, boolean fallbackToDefaultDecoding)
          This method is a substitute for URLDecoder.decode().
static String encode(String source)
          Encodes a String using the default encoding.
static String encode(String source, String encoding, boolean fallbackToDefaultEncoding)
          This method is a substitute for URLEncoder.encode().
static String escape(String source, String encoding)
          Encodes a String in a way that is compatible with the JavaScript escape function.
static String escapeHtml(String source)
          Escapes special characters in a HTML-String with their number-based entity representation, for example & becomes &.
static String escapeNonAscii(String source)
          Escapes non ASCII characters in a HTML-String with their number-based entity representation, for example & becomes &.
static String escapeWBlanks(String source, String encoding)
          Encodes a String in a way that is compatible with the JavaScript escape function.
static String escapeXml(String source)
          Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.
static String redecodeUriComponent(String input)
          Re-decodes a String that has not been correctly decoded and thus has scrambled character bytes.
static String unescape(String source, String encoding)
          Decodes a String in a way that is compatible with the JavaScript unescape function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

C_URI_ENCODING

public static final String C_URI_ENCODING
Default encoding for JavaScript decodeUriComponent methods is UTF-8 by w3c standard

See Also:
Constant Field Values
Constructor Detail

Encoder

public Encoder()
Constructor

Method Detail

encode

public static String encode(String source,
                            String encoding,
                            boolean fallbackToDefaultEncoding)
This method is a substitute for URLEncoder.encode(). Use this in all OpenCms core classes to ensure the encoding is always handled the same way.

In case you don't know what encoding to use, set the value of the encoding parameter to null. This will use the default encoding, which is propably the right one.

It also solves a backward compatiblity issue between Java 1.3 and 1.4, since 1.3 does not support an explicit encoding parameter and always uses the default system encoding.

Parameters:
source - the String to encode
encoding - the encoding to use (if null, the system default is used)
Returns:
the encoded source String

encode

public static String encode(String source)
Encodes a String using the default encoding.

Parameters:
source - the String to encode
Returns:
String the encoded source String

decode

public static String decode(String source,
                            String encoding,
                            boolean fallbackToDefaultDecoding)
This method is a substitute for URLDecoder.decode(). Use this in all OpenCms core classes to ensure the encoding is always handled the same way.

In case you don't know what encoding to use, set the value of the encoding parameter to null. This will use the default encoding, which is propably the right one.

It also solves a backward compatiblity issue between Java 1.3 and 1.4, since 1.3 does not support an explicit encoding parameter and always uses the default system encoding.

Parameters:
source - The string to decode
encoding - The encoding to use (if null, the system default is used)
fallbackToDefaultDecoding - If true, the method will fallback to the default encoding (Java 1.3 style), if false, the source String will be returned undecoded
Returns:
The decoded source String

decode

public static String decode(String source)
Decodes a String using the default encoding.

Parameters:
source - the String to decode
Returns:
String the decoded source String

escape

public static String escape(String source,
                            String encoding)
Encodes a String in a way that is compatible with the JavaScript escape function.

Returns:
The JavaScript escaped string.

escapeWBlanks

public static String escapeWBlanks(String source,
                                   String encoding)
Encodes a String in a way that is compatible with the JavaScript escape function. Muliple blanks are encoded _multiply _with %20.

Returns:
The JavaScript escaped string.

escapeXml

public static String escapeXml(String source)
Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.

This method replaces the following characters in a String:

Parameters:
source - the string to escape
Returns:
the escaped string
See Also:
escapeHtml(String)

escapeHtml

public static String escapeHtml(String source)
Escapes special characters in a HTML-String with their number-based entity representation, for example & becomes &.

A character num is replaced if
((ch != 32) && ((ch > 122) || (ch < 48) || (ch == 60) || (ch == 62)))

Parameters:
source - the String to escape
Returns:
String the escaped String
See Also:
escapeXml(String)

escapeNonAscii

public static String escapeNonAscii(String source)
Escapes non ASCII characters in a HTML-String with their number-based entity representation, for example & becomes &#38;.

A character num is replaced if
(ch > 255)

Parameters:
source - the String to escape
Returns:
String the escaped String
See Also:
escapeXml(String)

unescape

public static String unescape(String source,
                              String encoding)
Decodes a String in a way that is compatible with the JavaScript unescape function.

Returns:
The JavaScript unescaped String.

changeEncoding

public static byte[] changeEncoding(byte[] input,
                                    String oldEncoding,
                                    String newEncoding)
Changes the encoding of a byte array that represents a String.

Parameters:
input - the byte array to convert
oldEncoding - the current encoding of the byte array
newEncoding - the new encoding of the byte array
Returns:
byte[] the byte array encoded in the new encoding

redecodeUriComponent

public static String redecodeUriComponent(String input)
Re-decodes a String that has not been correctly decoded and thus has scrambled character bytes.

This is an equivalent to the JavaScript "decodeURIComponent" function. It converts from the default "UTF-8" to the currently selected system encoding.

Parameters:
input - the String to convert
Returns:
String the converted String