|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.uniba.euromath.tools.xml.TransformerTools
public final class TransformerTools
Provides tools for javax.xml.transform package.
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 |
---|
public static final Document doc
public static final TransformerFactory factory
Transformer
objects.
public static final DocumentBuilder builder
Constructor Detail |
---|
public TransformerTools()
Method Detail |
---|
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.
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
.
null
if source is not convertible.
IOException
- if i/o error occurs during source reading.
TransformerException
- if transformer fails while processing
source.public static SAXSource sourceToSax(Source source, boolean parse) throws TransformerException, IOException
SAXSource
. Only
DOMSource
, StreamSource
and
SAXSource
can be converted.
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
.
XMLReader
firing ContentHandler events only.
IOException
- if i/o error occurs during source reading.
TransformerException
- if transformer fails while processing
source.public static InputSource streamSourceToInputSource(StreamSource ss)
ss
- stream source to convert
public static Reader streamSourceAsReader(StreamSource ss, String encoding)
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.
public static SAXParseException transformerExceptionToSax(TransformerException ex)
ex
- exception to wrap.
public static Locator transformerLocatorToLocator(SourceLocator locator)
locator
- locator to wrap.
null
only if given locator is
null
.public static ErrorListener errorHandlerAsListener(ErrorHandler handler)
handler
- handler to wrap.
null
only if given handler
is null
.public static URIResolver entityResolverAsUriResolver(EntityResolver resolver)
resolver
- the resolver to wrap.
null
only if given resolver
is null
.public static XMLReader readerToSax(Reader reader, int chunkSize)
reader
- string to convert.chunkSize
- split text into chunks with this size.
public static XMLReader stringToSax(String string, int chunkSize)
string
- string to convert.chunkSize
- split text into chunks with this size.
public static SAXSource stringToSaxSource(String string, String systemId, int chunkSize)
SAXSource
.
string
- string to convert.systemId
- identifier set to the SAXSource
being
returned.chunkSize
- split text into chunks with this size.
SAXSource
. The
source has an empty reader as its input source.public static SAXSource readerToSaxSource(Reader reader, String systemId, int chunkSize)
SAXSource
.
reader
- string to convert.systemId
- identifier set to the SAXSource
being
returned.chunkSize
- split text into chunks with this size.
SAXSource
. The
source has an empty reader as its input source.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.
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.
public static Transformer newIdentity()
public static DocumentFragment newFragment()
public static void copyStream(StreamSource source, StreamResult result, String encoding) throws IOException
source
- data sourceresult
- here the data shall be copied.encoding
- encoding to use when needed. null
means
UTF-8 encoding.
IOException
- if i/o error occurs.public static Transformer newTransformer(URL url) throws IOException, TransformerException
Transformer
object, that
transforms XML document. Only XSLT is supported.
url
- location of the transformer.
IOException
- if I/O error occurs.
TransformerException
- if XSLT contains errors.public static void closeSource(Source source) throws IOException
StreamSource
can be closed.
source
- the source to close. Closes reader if not null
,
otherwise closes inputstream - does not close them both.
IOException
- if close fails.public static void closeResult(Result result) throws IOException
StreamResult
can be closed.
result
- the source to close. Closes writer if not null
,
otherwise closes outputstream - does not close them both.
IOException
- if close fails.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |