de.knipp.rritoolkit
Class Codec

java.lang.Object
  extended by de.knipp.rritoolkit.Codec

public class Codec
extends java.lang.Object

Class that performs the conversion to and from byte data, as required for the exchange with the registry server. The class is thread-safe. Various methods are exposed that represent various steps in the conversion, merely to allow investigation of the intermediate data in case of problems.

Version:
$Revision: 14936 $
Author:
Klaus Malorny

Field Summary
protected  java.lang.ThreadLocal<javax.xml.parsers.DocumentBuilder> builder
          the document builder we are using
protected  ResponseFactory factory
          the response factory we are using
private static java.util.regex.Pattern kvPattern
          pattern to decode a key-value pair
private static java.util.regex.Pattern linePattern
          pattern to split a key-value list into lines
private static java.util.regex.Pattern sectionPattern
          pattern to decode an individual section name
private static java.util.regex.Pattern sectionsPattern
          pattern to decode section names line
protected  boolean useXml
          whether requests/responses shall be encoded/decoded to/from XML
private static java.nio.charset.Charset utf8Charset
          the UTF-8 character set
 
Constructor Summary
Codec()
          default constructor.
Codec(ResponseFactory theFactory, boolean xml)
          constructor
 
Method Summary
protected  void addList(KVList list, java.lang.StringBuilder sb)
          add a key-value list to the given string builder
 org.w3c.dom.Document bytesToDoc(byte[] data)
          convert the byte array to an DOM document
 java.lang.String bytesToString(byte[] data)
          interpret the given byte array as a sequence of UTF-8 encoded characters and return it as a string.
 Response decodeResponse(byte[] data, Request req)
          convert the given byte sequence to a response object.
 byte[] docToBytes(org.w3c.dom.Document doc)
          convert the DOM document to a sequence of bytes
 Response docToResponse(org.w3c.dom.Document doc, Request req)
          convert the document to a response
 byte[] encodeRequest(Request req)
          convert the given request to a byte sequence
protected  javax.xml.parsers.DocumentBuilder getBuilder()
          return the document builder that shall be used for parsing and generating XML
 Response listToResponse(KVMultiList list, Request request)
          convert the list to the response instance
 java.lang.String listToString(KVList list)
          convert a key-value list to a string
 java.lang.String listToString(KVMultiList list)
          convert a key-value multilist to a string.
 org.w3c.dom.Document requestToDoc(Request req)
          convert the request to an XML document
 KVList requestToList(Request req)
          convert the given request to a list
 byte[] stringToBytes(java.lang.String text)
          return the given string in the UTF-8 representation
 KVMultiList stringToList(java.lang.String text)
          parse the given string into a key-value list
 boolean xmlUsed()
          return whether XML is used for encoding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factory

protected ResponseFactory factory
the response factory we are using


useXml

protected boolean useXml
whether requests/responses shall be encoded/decoded to/from XML


builder

protected java.lang.ThreadLocal<javax.xml.parsers.DocumentBuilder> builder
the document builder we are using


linePattern

private static final java.util.regex.Pattern linePattern
pattern to split a key-value list into lines


kvPattern

private static final java.util.regex.Pattern kvPattern
pattern to decode a key-value pair


sectionsPattern

private static final java.util.regex.Pattern sectionsPattern
pattern to decode section names line


sectionPattern

private static final java.util.regex.Pattern sectionPattern
pattern to decode an individual section name


utf8Charset

private static final java.nio.charset.Charset utf8Charset
the UTF-8 character set

Constructor Detail

Codec

public Codec(ResponseFactory theFactory,
             boolean xml)
constructor

Parameters:
theFactory - the response factory to use
xml - whether to operate with XML (true) or key-value pairs (false)

Codec

public Codec()
default constructor. Uses the default factory and XML.

Method Detail

encodeRequest

public byte[] encodeRequest(Request req)
                     throws EncodeException
convert the given request to a byte sequence

Parameters:
req - the request
Returns:
the byte data
Throws:
EncodeException - if the encoding process failed

decodeResponse

public Response decodeResponse(byte[] data,
                               Request req)
                        throws ResponseParseException
convert the given byte sequence to a response object. Please note that it is recommended to specify the request when using the key-value encoding, otherwise it may happen that the class of the returned instance varies depending on the response, since the response gives not enough information of the type of the related request in all cases.

Parameters:
data - the data
req - the request, may be null
Returns:
the response
Throws:
ResponseParseException - if problems occurred during the parsing of the data

xmlUsed

public boolean xmlUsed()
return whether XML is used for encoding

Returns:
true if so

requestToList

public KVList requestToList(Request req)
                     throws EncodeException
convert the given request to a list

Parameters:
req - the request
Returns:
the list
Throws:
EncodeException - if the encoding process failed

addList

protected void addList(KVList list,
                       java.lang.StringBuilder sb)
add a key-value list to the given string builder

Parameters:
list - the list to add
sb - the string builder to use

listToString

public java.lang.String listToString(KVList list)
convert a key-value list to a string

Parameters:
list - the list
Returns:
the list as a string

listToString

public java.lang.String listToString(KVMultiList list)
convert a key-value multilist to a string. Although not required for the operation, this is provided for convenience

Parameters:
list - the list
Returns:
the list as a string

stringToBytes

public byte[] stringToBytes(java.lang.String text)
return the given string in the UTF-8 representation

Parameters:
text - the string
Returns:
the byte array

listToResponse

public Response listToResponse(KVMultiList list,
                               Request request)
                        throws ResponseParseException
convert the list to the response instance

Parameters:
list - the list
request - the request, may be null if not available
Returns:
the response
Throws:
ResponseParseException - if the response could not be created based on the input

stringToList

public KVMultiList stringToList(java.lang.String text)
                         throws ResponseParseException
parse the given string into a key-value list

Parameters:
text - the text to parse
Returns:
the parsed key-value list
Throws:
ResponseParseException - if the parsing failed

bytesToString

public java.lang.String bytesToString(byte[] data)
                               throws java.io.IOException
interpret the given byte array as a sequence of UTF-8 encoded characters and return it as a string. Contrary to the related String constructor, malformed input is detected and reported.

Parameters:
data - the data
Returns:
the converted string
Throws:
java.io.IOException - if the conversion failed

getBuilder

protected javax.xml.parsers.DocumentBuilder getBuilder()
return the document builder that shall be used for parsing and generating XML

Returns:
the document builder

requestToDoc

public org.w3c.dom.Document requestToDoc(Request req)
                                  throws EncodeException
convert the request to an XML document

Parameters:
req - the request
Returns:
the document
Throws:
EncodeException - if the encoding failed

docToBytes

public byte[] docToBytes(org.w3c.dom.Document doc)
                  throws EncodeException
convert the DOM document to a sequence of bytes

Parameters:
doc - the document to convert
Returns:
the data
Throws:
EncodeException - if the serialization failed

docToResponse

public Response docToResponse(org.w3c.dom.Document doc,
                              Request req)
                       throws ResponseParseException
convert the document to a response

Parameters:
doc - the document
req - the request
Returns:
the response
Throws:
ResponseParseException - if the parsing failed

bytesToDoc

public org.w3c.dom.Document bytesToDoc(byte[] data)
                                throws ResponseParseException
convert the byte array to an DOM document

Parameters:
data - the data
Returns:
the document
Throws:
ResponseParseException - if the parsing failed


Copyright © 2005-2011 Knipp Medien und Kommunikation GmbH