org.mmbase.util
Class Encode

java.lang.Object
  extended by org.mmbase.util.Encode

public class Encode
extends Object

Class to convert from/to a string (byte[]) from/to a encoded string (byte[]) Supported encodings are at this moment:

A list of supported encodings can be gotten by java org.mmbase.util.Encode, and you add your own encodings by calling the static function 'register' of this class. Usage:
  Encode encoder = new Encode("ESCAPE_XML");
  System.out.println(  encoder.decode( encoder.encode("& \" < >") )  );
  

Version:
$Id: Encode.java 42593 2010-06-17 12:24:13Z michiel $
Author:
Eduard Witteveen, Michiel Meeuwissen
Rename:
Encoder

Constructor Summary
Encode(String encoding)
          Created a encode instance of a certain type of encoding.
 
Method Summary
 String decode(String toDecode)
          This function will decode a given string to it's decoded variant
static String decode(String encoding, String toDecode)
          This function will decode a given string to it's decoded variant.
 byte[] decodeBytes(String toDecode)
           
static byte[] decodeBytes(String encoding, String toDecode)
           
 String encode(byte[] bytes)
          Encodes a byte array.
 String encode(String toEncode)
          This function will encode a given string to it's encoded variant.
static String encode(String encoding, byte[] bytes)
           
static String encode(String encoding, String toEncode)
          This function will encode a given string to it's encoded variant.
 String getEncoding()
          Returns the encoding
 Transformer getTransformer()
           
 boolean isByteToCharEncoder()
          Checks if the transformations makes from byte[] String.
 boolean isCharEncoder()
          Checks if the transformation is between two Strings.
static boolean isEncoding(String e)
          Checks if a certain string represents a known transformation.
static void main(String[] argv)
          Invocation of the class from the commandline for testing.
static Set<String> possibleEncodings()
          All the currently known encodings.
static void register(String clazz)
          Add new transformation types.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Encode

public Encode(String encoding)
Created a encode instance of a certain type of encoding. It will instantiate the right class, and configure it. This instantion will be used when you call 'encode' or 'decode' members later.

Parameters:
encoding - a string that describes which encoding should be used.
Method Detail

getTransformer

public Transformer getTransformer()
Since:
MMBase-1.8

register

public static void register(String clazz)
Add new transformation types. Feed it with a class name (which must implement Transformer)

Parameters:
clazz - a class name.

encode

public static String encode(String encoding,
                            String toEncode)
This function will encode a given string to it's encoded variant. It is static, it will make a temporary instance of the Transformer class. If you need to encode alot, it is better to use new Encode first.

Parameters:
encoding - a string that describes which encoding should be used.
toEncode - a string which is the value which should be encoded. This can also be a byte[].
Returns:
a string which is the encoded representation of toEncode with the given encoding

encode

public static String encode(String encoding,
                            byte[] bytes)

decode

public static String decode(String encoding,
                            String toDecode)
This function will decode a given string to it's decoded variant.

Parameters:
encoding - a string that describes which decoding should be used.
toDecode - a string which is the value which should be encoded.
Returns:
a string which is the encoded representation of toEncode with the given encoding
See Also:
encode(java.lang.String, java.lang.String)

decodeBytes

public static byte[] decodeBytes(String encoding,
                                 String toDecode)

encode

public String encode(String toEncode)
This function will encode a given string to it's encoded variant.

Parameters:
toEncode - A string which is the value which should be encoded. If the transformer does transform bytes, then first getBytes is done on the String.
Returns:
a string which is the encoded representation of toEncode with the given encoding

encode

public String encode(byte[] bytes)
Encodes a byte array.

Returns:
a string;;

decode

public String decode(String toDecode)
This function will decode a given string to it's decoded variant

Parameters:
toDecode - a string which is the value which should be encoded.
Returns:
a string which is the encoded representation of toEncode with the given encoding

decodeBytes

public byte[] decodeBytes(String toDecode)

possibleEncodings

public static Set<String> possibleEncodings()
All the currently known encodings.

Returns:
Set of Strings containing the names of the registered encodings.

isEncoding

public static boolean isEncoding(String e)
Checks if a certain string represents a known transformation.


isCharEncoder

public boolean isCharEncoder()
Checks if the transformation is between two Strings.


isByteToCharEncoder

public boolean isByteToCharEncoder()
Checks if the transformations makes from byte[] String.


getEncoding

public String getEncoding()
Returns the encoding

Returns:
An String representing the coding that is currently used.

main

public static void main(String[] argv)
Invocation of the class from the commandline for testing.



MMBase 2.0-SNAPSHOT - null