sk.uniba.euromath.tools.xml
Class TransformerTools

java.lang.Object
  extended by sk.uniba.euromath.tools.xml.TransformerTools

public final class TransformerTools
extends Object

Provides tools for javax.xml.transform package.

Author:
Martin Vysny

Field Summary
static DocumentBuilder builder
          Produces document instances.
static Document doc
          The document instance, used as a node factory.
static TransformerFactory factory
          Factory that produces Transformer objects.
 
Constructor Summary
TransformerTools()
           
 
Method Summary
static void closeResult(Result result)
          Closes given result.
static void closeSource(Source source)
          Closes given source.
static void copyStream(StreamSource source, StreamResult result, String encoding)
          Copies characters (or bytes) from source to target.
static URIResolver entityResolverAsUriResolver(EntityResolver resolver)
          Wraps the SAX entity resolver as a transformer URI resolver.
static ErrorListener errorHandlerAsListener(ErrorHandler handler)
          Wraps the SAX error handler as a transformer error listener.
static DocumentFragment newFragment()
          Creates new empty document fragment.
static Transformer newIdentity()
          Creates an identity - a transformer that produces document given as an input.
static Transformer newTransformer(URL url)
          For given URL returns appropriate Transformer object, that transforms XML document.
static XMLReader readerToSax(Reader reader, int chunkSize)
          Returns SAX reader that fires these events, in order: start document, multiple text events, end document.
static SAXSource readerToSaxSource(Reader reader, String systemId, int chunkSize)
          Constructs SAX reader that fires three events: start document, text, end document, and returns this reader enclosed in SAXSource.
static DOMSource sourceToDom(Source source, boolean parse)
           Converts source to the DOMSource.
static SAXSource sourceToSax(Source source, boolean parse)
          Converts source to the SAXSource.
static StreamSource sourceToStream(Source source, ThreadGroup group)
           Converts source to the StreamSource.
static Reader streamSourceAsReader(StreamSource ss, String encoding)
          Converts stream source to a reader that reads the stream.
static InputSource streamSourceToInputSource(StreamSource ss)
          Converts stream source instance to an input source object.
static XMLReader stringToSax(String string, int chunkSize)
          Returns SAX reader that fires three events: start document, text, end document.
static SAXSource stringToSaxSource(String string, String systemId, int chunkSize)
          Constructs SAX reader that fires three events: start document, text, end document, and returns this reader enclosed in SAXSource.
static SAXParseException transformerExceptionToSax(TransformerException ex)
          Wraps transformer exception in SAX exception.
static Locator transformerLocatorToLocator(SourceLocator locator)
          Wraps the SAX locator as a transformer locator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

doc

public static final Document doc
The document instance, used as a node factory.


factory

public static final TransformerFactory factory
Factory that produces Transformer objects.


builder

public static final DocumentBuilder builder
Produces document instances.

Constructor Detail

TransformerTools

public TransformerTools()
Method Detail

sourceToDom

public static DOMSource sourceToDom(Source source,
                                    boolean parse)
                             throws TransformerException,
                                    IOException

Converts source to the DOMSource. Only DOMSource, StreamSource and SAXSource can be converted.

Warning: If source is DOMSource and it holds node different than document fragment, this node is deleted from its original position and inserted into new document fragment. If this node is a document node then all its children content nodes are moved into new document fragment.

Parameters:
source - the source object to convert.
parse - if true and source is instance of StreamSource then try to parse the stream. Otherwise, result is a single Text node containing whole stream. Ignored when source is not StreamSource.
Returns:
Document fragment containing the content of the source, or null if source is not convertible.
Throws:
IOException - if i/o error occurs during source reading.
TransformerException - if transformer fails while processing source.

sourceToSax

public static SAXSource sourceToSax(Source source,
                                    boolean parse)
                             throws TransformerException,
                                    IOException
Converts source to the SAXSource. Only DOMSource, StreamSource and SAXSource can be converted.

Parameters:
source - the source object to convert.
parse - if true and source is instance of StreamSource then try to parse the stream. Otherwise, result is a single Text node containing whole stream. Ignored when source is not StreamSource.
Returns:
XMLReader firing ContentHandler events only.
Throws:
IOException - if i/o error occurs during source reading.
TransformerException - if transformer fails while processing source.

streamSourceToInputSource

public static InputSource streamSourceToInputSource(StreamSource ss)
Converts stream source instance to an input source object.

Parameters:
ss - stream source to convert
Returns:
InputSource instance representing original stream source.

streamSourceAsReader

public static Reader streamSourceAsReader(StreamSource ss,
                                          String encoding)
Converts stream source to a reader that reads the stream.

Parameters:
ss - stream source to convert.
encoding - optional encoding, used if stream source does not have reader defined. If null then UTF-8 encoding is used.
Returns:
reader reading given stream.

transformerExceptionToSax

public static SAXParseException transformerExceptionToSax(TransformerException ex)
Wraps transformer exception in SAX exception.

Parameters:
ex - exception to wrap.
Returns:
SAX exception.

transformerLocatorToLocator

public static Locator transformerLocatorToLocator(SourceLocator locator)
Wraps the SAX locator as a transformer locator.

Parameters:
locator - locator to wrap.
Returns:
SAX locator. Returns null only if given locator is null.

errorHandlerAsListener

public static ErrorListener errorHandlerAsListener(ErrorHandler handler)
Wraps the SAX error handler as a transformer error listener.

Parameters:
handler - handler to wrap.
Returns:
error listener. Returns null only if given handler is null.

entityResolverAsUriResolver

public static URIResolver entityResolverAsUriResolver(EntityResolver resolver)
Wraps the SAX entity resolver as a transformer URI resolver.

Parameters:
resolver - the resolver to wrap.
Returns:
URI resolver. Returns null only if given resolver is null.

readerToSax

public static XMLReader readerToSax(Reader reader,
                                    int chunkSize)
Returns SAX reader that fires these events, in order: start document, multiple text events, end document.

Parameters:
reader - string to convert.
chunkSize - split text into chunks with this size.
Returns:
SAX reader instance.

stringToSax

public static XMLReader stringToSax(String string,
                                    int chunkSize)
Returns SAX reader that fires three events: start document, text, end document.

Parameters:
string - string to convert.
chunkSize - split text into chunks with this size.
Returns:
SAX reader instance.

stringToSaxSource

public static SAXSource stringToSaxSource(String string,
                                          String systemId,
                                          int chunkSize)
Constructs SAX reader that fires three events: start document, text, end document, and returns this reader enclosed in SAXSource.

Parameters:
string - string to convert.
systemId - identifier set to the SAXSource being returned.
chunkSize - split text into chunks with this size.
Returns:
SAX reader instance, enclosed in SAXSource. The source has an empty reader as its input source.

readerToSaxSource

public static SAXSource readerToSaxSource(Reader reader,
                                          String systemId,
                                          int chunkSize)
Constructs SAX reader that fires three events: start document, text, end document, and returns this reader enclosed in SAXSource.

Parameters:
reader - string to convert.
systemId - identifier set to the SAXSource being returned.
chunkSize - split text into chunks with this size.
Returns:
SAX reader instance, enclosed in SAXSource. The source has an empty reader as its input source.

sourceToStream

public static StreamSource sourceToStream(Source source,
                                          ThreadGroup group)

Converts source to the StreamSource. Only DOMSource, StreamSource and SAXSource can be converted.

Warning: if converting from DOMSource and the node is not document fragment, document nor element node then the node is moved to a newly created document fragment.

The source polling is executed in its own daemon thread, attached to given thread group.

Parameters:
source - the source object to convert.
group - created thread is attached to this group. May be null - for details please see Thread.Thread(ThreadGroup, String) constructor.
Returns:
reader

newIdentity

public static Transformer newIdentity()
Creates an identity - a transformer that produces document given as an input.

Returns:
new identity transformer.

newFragment

public static DocumentFragment newFragment()
Creates new empty document fragment. You may use its document to create new nodes and fill the fragment with them.

Returns:
new empty instance of document fragment.

copyStream

public static void copyStream(StreamSource source,
                              StreamResult result,
                              String encoding)
                       throws IOException
Copies characters (or bytes) from source to target.

Parameters:
source - data source
result - here the data shall be copied.
encoding - encoding to use when needed. null means UTF-8 encoding.
Throws:
IOException - if i/o error occurs.

newTransformer

public static Transformer newTransformer(URL url)
                                  throws IOException,
                                         TransformerException
For given URL returns appropriate Transformer object, that transforms XML document. Only XSLT is supported.

Parameters:
url - location of the transformer.
Returns:
Transformer instance
Throws:
IOException - if I/O error occurs.
TransformerException - if XSLT contains errors.

closeSource

public static void closeSource(Source source)
                        throws IOException
Closes given source. Only StreamSource can be closed.

Parameters:
source - the source to close. Closes reader if not null, otherwise closes inputstream - does not close them both.
Throws:
IOException - if close fails.

closeResult

public static void closeResult(Result result)
                        throws IOException
Closes given result. Only StreamResult can be closed.

Parameters:
result - the source to close. Closes writer if not null, otherwise closes outputstream - does not close them both.
Throws:
IOException - if close fails.


Copyright 2003-2003-2006 null. All Rights Reserved.