Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.opencms.util
Class Encoder  view Encoder download Encoder.java

java.lang.Object
  extended bycom.opencms.util.Encoder

public class Encoder
extends java.lang.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.


Field Summary
private static boolean C_NEW_DECODING_SUPPORTED
          Flag to indicate if the Java 1.4 decoding method (with encoding parameter) is supported by the JVM
private static boolean C_NEW_ENCODING_SUPPORTED
          Flag to indicate if the Java 1.4 encoding method (with encoding parameter) is supported by the JVM
static java.lang.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, java.lang.String oldEncoding, java.lang.String newEncoding)
          Changes the encoding of a byte array that represents a String.
static java.lang.String decode(java.lang.String source)
          Decodes a String using the default encoding.
static java.lang.String decode(java.lang.String source, java.lang.String encoding, boolean fallbackToDefaultDecoding)
          This method is a substitute for URLDecoder.decode().
static java.lang.String encode(java.lang.String source)
          Encodes a String using the default encoding.
static java.lang.String encode(java.lang.String source, java.lang.String encoding, boolean fallbackToDefaultEncoding)
          This method is a substitute for URLEncoder.encode().
static java.lang.String escape(java.lang.String source, java.lang.String encoding)
          Encodes a String in a way that is compatible with the JavaScript escape function.
static java.lang.String escapeHtml(java.lang.String source)
          Escapes special characters in a HTML-String with their number-based entity representation, for example & becomes &.
static java.lang.String escapeNonAscii(java.lang.String source)
          Escapes non ASCII characters in a HTML-String with their number-based entity representation, for example & becomes &.
static java.lang.String escapeWBlanks(java.lang.String source, java.lang.String encoding)
          Encodes a String in a way that is compatible with the JavaScript escape function.
static java.lang.String escapeXml(java.lang.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 java.lang.String redecodeUriComponent(java.lang.String input)
          Re-decodes a String that has not been correctly decoded and thus has scrambled character bytes.
static java.lang.String unescape(java.lang.String source, java.lang.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_NEW_ENCODING_SUPPORTED

private static boolean C_NEW_ENCODING_SUPPORTED
Flag to indicate if the Java 1.4 encoding method (with encoding parameter) is supported by the JVM


C_NEW_DECODING_SUPPORTED

private static boolean C_NEW_DECODING_SUPPORTED
Flag to indicate if the Java 1.4 decoding method (with encoding parameter) is supported by the JVM


C_URI_ENCODING

public static final java.lang.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 java.lang.String encode(java.lang.String source,
                                      java.lang.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.


encode

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


decode

public static java.lang.String decode(java.lang.String source,
                                      java.lang.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.


decode

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


escape

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


escapeWBlanks

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


escapeXml

public static java.lang.String escapeXml(java.lang.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:

  • < with &lt;
  • > with &gt;
  • & with &amp;
  • " with &quot;


escapeHtml

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

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


escapeNonAscii

public static java.lang.String escapeNonAscii(java.lang.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)


unescape

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


changeEncoding

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


redecodeUriComponent

public static java.lang.String redecodeUriComponent(java.lang.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.