org.ssonet.io
Class XMLTools

java.lang.Object
  |
  +--org.ssonet.io.XMLTools

public class XMLTools
extends java.lang.Object

This class encloses some methods to handle common XML issues.

Version:
$Id: XMLTools.java,v 1.1 2003/10/20 13:33:30 tk4 Exp $
Author:
SSONET

Field Summary
static boolean debug
           
 
Constructor Summary
XMLTools()
           
 
Method Summary
static int decodeLength(byte[] array)
          Decodes the length field of a byte array.
static byte[] encodeInteger(int intValue)
          Encodes an Integer into an array of 4 bytes.
static byte[] getLengthEncodingFor(byte[] array)
           
static byte[] joinByteArrays(byte[] array1, byte[] array2)
          joins two ByteArrays
static org.w3c.dom.Element parseDocumentElementFromStream(java.io.InputStream xmlStream, boolean isZipped)
          parse a XML document from a stream.
static org.w3c.dom.Element parseDocumentElementFromStream(org.ssonet.io.IOStream xmlStream, boolean isZipped)
          parse an XML document from an IOStream.
static byte[] readByteArrayWithLengthEncoding(java.io.InputStream is)
          Reads a byte array from an InputStream.
static byte[] readByteArrayWithLengthEncoding(org.ssonet.io.IOStream ioStream)
          Reads a byte array from an IOStream.
static byte[] serializeDocument(org.w3c.dom.Document doc, boolean isZipped)
          Serializes a XML document.
static void writeByteArrayWithLengthEncoding(org.ssonet.io.IOStream ioStream, byte[] array)
          Writes a byte array to an IOStream with a preceeding length encoding.
static void writeByteArrayWithLengthEncoding(java.io.OutputStream os, byte[] array)
          Writes a byte array to a stream with a preceeding length encoding.
static void writeDocumentToStream(org.ssonet.io.IOStream ioStream, org.w3c.dom.Document doc, boolean isZipped)
          Writes a XML Document to an IOStream.
static void writeDocumentToStream(java.io.OutputStream outputStream, org.w3c.dom.Document doc, boolean isZipped)
          Writes a XML Document to an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static boolean debug
Constructor Detail

XMLTools

public XMLTools()
Method Detail

parseDocumentElementFromStream

public static org.w3c.dom.Element parseDocumentElementFromStream(java.io.InputStream xmlStream,
                                                                 boolean isZipped)
                                                          throws java.io.IOException
parse a XML document from a stream. The stream is not closed after the document is read.

Parameters:
xmlStream - the stream, where the zipped document shall be read from
isZipped - true, if the Document to be parsed is zipped, false otherwise
Returns:
the XML element read from the stream
java.io.IOException

parseDocumentElementFromStream

public static org.w3c.dom.Element parseDocumentElementFromStream(org.ssonet.io.IOStream xmlStream,
                                                                 boolean isZipped)
                                                          throws java.io.IOException
parse an XML document from an IOStream. The stream is not closed after the document is read.

Parameters:
xmlStream - the IOStream, where the document shall be read from
isZipped - true, if the document to be read is zipped, false otherwise
Returns:
the XML element read from the stream
java.io.IOException

readByteArrayWithLengthEncoding

public static byte[] readByteArrayWithLengthEncoding(java.io.InputStream is)
                                              throws java.io.IOException
Reads a byte array from an InputStream. The byte array must be preceeded by a length encoding in the following way:

1. and 2. Byte: length of content: (1.Byte)+(2.Byte)^8

3. to length. Byte: Content

Parameters:
is - the InputStream to read from
Throws:
java.io.IOException - if an error occures during reading the byte array from stream

readByteArrayWithLengthEncoding

public static byte[] readByteArrayWithLengthEncoding(org.ssonet.io.IOStream ioStream)
                                              throws java.io.IOException
Reads a byte array from an IOStream. The byte array must be preceeded by a length encoding in the following way:

1. and 2. Byte: length of content: (1.Byte)+(2.Byte)^8

3. to length. Byte: Content

Parameters:
ioStream - the IOStream to read from
Throws:
java.io.IOException - if an error occures during reading the byte array from stream

decodeLength

public static int decodeLength(byte[] array)
                        throws java.lang.IllegalArgumentException
Decodes the length field of a byte array. The length information is located in the first four bytes of the array.

Parameters:
array - the array holding the length information
Returns:
the length, which was encoded in the byte array.
Throws:
java.lang.IllegalArgumentException - if the array is two short (array.length < 4), or (array==null)

encodeInteger

public static byte[] encodeInteger(int intValue)
Encodes an Integer into an array of 4 bytes.

Parameters:
intValue - the value to encode
Returns:
a byte array holding the encoded value

getLengthEncodingFor

public static byte[] getLengthEncodingFor(byte[] array)
Returns:
a byte-array with the encoded length information of the given byte-array

writeDocumentToStream

public static void writeDocumentToStream(java.io.OutputStream outputStream,
                                         org.w3c.dom.Document doc,
                                         boolean isZipped)
                                  throws java.io.IOException
Writes a XML Document to an OutputStream.

Parameters:
outputStream - the stream, where the XML Document shall be written to,
doc - the XML Document
isZipped - true, if the document to write shall be zipped, false otherwise
Throws:
java.io.IOException - if an error occures during encoding to XML and writing to the stream.

writeDocumentToStream

public static void writeDocumentToStream(org.ssonet.io.IOStream ioStream,
                                         org.w3c.dom.Document doc,
                                         boolean isZipped)
                                  throws java.io.IOException
Writes a XML Document to an IOStream.

Parameters:
ioStream - the IOStream, where the XML Document shall be written to,
doc - the XML Document
isZipped - true, if the document to write shall be zipped, false otherwise
Throws:
java.io.IOException - if an error occures during encoding to XML and writing to the stream.

serializeDocument

public static byte[] serializeDocument(org.w3c.dom.Document doc,
                                       boolean isZipped)
Serializes a XML document.

Parameters:
doc - the XML document
isZipped - true, if the document to serialize shall be zipped, false otherwise
Returns:
a byte array contaning the serialized document

writeByteArrayWithLengthEncoding

public static void writeByteArrayWithLengthEncoding(java.io.OutputStream os,
                                                    byte[] array)
                                             throws java.io.IOException
Writes a byte array to a stream with a preceeding length encoding.

Parameters:
os - the OutputStream to write to
array - the byte array
Throws:
java.io.IOException - if an error occures during writing the byte array to stream

writeByteArrayWithLengthEncoding

public static void writeByteArrayWithLengthEncoding(org.ssonet.io.IOStream ioStream,
                                                    byte[] array)
                                             throws java.io.IOException
Writes a byte array to an IOStream with a preceeding length encoding.

Parameters:
ioStream - the IOStream to write to
array - the byte array
Throws:
java.io.IOException - if an error occures during writing the byte array to stream

joinByteArrays

public static byte[] joinByteArrays(byte[] array1,
                                    byte[] array2)
joins two ByteArrays

Parameters:
array1 - first ByteArray
array2 - second ByteArray
Returns:
byte[] Concatenation of both ByteArrays


Copyright © 2003 DRIM Team. All Rights Reserved.