public class Codec
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
Codec()
default constructor.
|
Codec(ResponseFactory theFactory,
boolean xml)
constructor
|
Modifier and Type | Method and Description |
---|---|
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
|
protected ResponseFactory factory
protected boolean useXml
protected java.lang.ThreadLocal<javax.xml.parsers.DocumentBuilder> builder
private static final java.util.regex.Pattern linePattern
private static final java.util.regex.Pattern kvPattern
private static final java.util.regex.Pattern sectionsPattern
private static final java.util.regex.Pattern sectionPattern
private static final java.nio.charset.Charset utf8Charset
public Codec(ResponseFactory theFactory, boolean xml)
theFactory
- the response factory to usexml
- whether to operate with XML
(true
) or key-value pairs
(false
)public Codec()
public byte[] encodeRequest(Request req) throws EncodeException
req
- the requestEncodeException
- if the encoding process failedpublic Response decodeResponse(byte[] data, Request req) throws ResponseParseException
data
- the datareq
- the request, may be null
ResponseParseException
- if problems occurred during the parsing
of the datapublic boolean xmlUsed()
true
if sopublic KVList requestToList(Request req) throws EncodeException
req
- the requestEncodeException
- if the encoding process failedprotected void addList(KVList list, java.lang.StringBuilder sb)
list
- the list to addsb
- the string builder to usepublic java.lang.String listToString(KVList list)
list
- the listpublic java.lang.String listToString(KVMultiList list)
list
- the listpublic byte[] stringToBytes(java.lang.String text)
text
- the stringpublic Response listToResponse(KVMultiList list, Request request) throws ResponseParseException
list
- the listrequest
- the request, may be null
if not availableResponseParseException
- if the response could not be created
based on the inputpublic KVMultiList stringToList(java.lang.String text) throws ResponseParseException
text
- the text to parseResponseParseException
- if the parsing failedpublic java.lang.String bytesToString(byte[] data) throws java.io.IOException
data
- the datajava.io.IOException
- if the conversion failedprotected javax.xml.parsers.DocumentBuilder getBuilder()
public org.w3c.dom.Document requestToDoc(Request req) throws EncodeException
req
- the requestEncodeException
- if the encoding failedpublic byte[] docToBytes(org.w3c.dom.Document doc) throws EncodeException
doc
- the document to convertEncodeException
- if the serialization failedpublic Response docToResponse(org.w3c.dom.Document doc, Request req) throws ResponseParseException
doc
- the documentreq
- the requestResponseParseException
- if the parsing failedpublic org.w3c.dom.Document bytesToDoc(byte[] data) throws ResponseParseException
data
- the dataResponseParseException
- if the parsing failedCopyright © 2005-2013 Knipp Medien und Kommunikation GmbH