public class SimpleXmlWriter
extends java.lang.Object
implements java.lang.AutoCloseable
This writer makes creation of simple XML documents without advanced features simple. Use the following to create simple XML:
This class was not only moved from package de.caff.util.xml
, it also underwent
important changes: in order to become auto-closeable methods are renamed:
SimpleXmlWriter.open(String, Object...)
is replaced by
begin(String, Object...)
.
SimpleXmlWriter.close()
is replaced by
end()
. Because the new class also provides a close()
with different semantics special care has to be taken when switching!
SimpleXmlWriter.finish()
is replaced by
close()
to make this class auto-closable.
element(String, Object...)
method writes an empty element
which is already closed with the given attributes' name-value pairs.
begin(String, Object...)
method writes an opening element
with the given attributes' name-value pairs.
end()
method closes the element created by the latest
begin(String, Object...)
call.
close()
method closes all open elements. Although
inherited from the AutoCloseable
interface it will not
throw any checked exception.
comment(String)
method inserts a comment with the given text.
text(Object)
method inserts the given text, taking care of
escaping.
textf(String, Object...)
method does the same, but allows
formatting like String.format(String, Object...)
.
cdata(String)
method inserts the given text in raw
form as a CDATA section.
nl()
method can be used to insert a newline,
see also the comments on automatic wrapping below.
expand(byte[])
inserts binary data as 2-digit-per-byte
hexadecimal numbers.
expandBase64(byte[])
inserts binary data in base64 encoding.
expand(byte[], int)
method can be used to format binary
data as 2-digit-per-byte hexadecimal numbers, and can be used with
either the text(Object)
method or as attribute value (in the
latter case best with columns
set to 0
).
Automatic wrapping and indentation of hierarchy levels can be switched on
by using constructor which accept an indentation
parameter with
a non-negative indentation value.
A value of 0
switches auto-wrapping on, starting each opening tag
on a new line. A value greater than zero will also indent each sub level
by the given number of blanks. A negative value will switch both auto-wrapping
and indentation off.
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoWrap
Is automatic wrapping enabled?
|
protected StringCollector |
collector
Collector used for output text collection.
|
static java.lang.String |
DEFAULT_ENCODING
Default encoding in XML header.
|
protected java.lang.String |
indentation
Use as indentation if non-
null . |
protected java.util.Locale |
locale
Locale to be used when formatting.
|
Constructor and Description |
---|
SimpleXmlWriter(java.util.Locale locale,
StringCollector collector)
Constructor.
|
SimpleXmlWriter(java.util.Locale locale,
StringCollector collector,
int indentation)
Constructor.
|
SimpleXmlWriter(java.util.Locale locale,
StringCollector collector,
java.lang.String preamble)
Constructor.
|
SimpleXmlWriter(java.util.Locale locale,
StringCollector collector,
java.lang.String preamble,
int indentation)
Constructor.
|
SimpleXmlWriter(StringCollector collector)
Constructor.
|
SimpleXmlWriter(StringCollector collector,
int indentation)
Constructor.
|
SimpleXmlWriter(StringCollector collector,
java.lang.String preamble)
Constructor.
|
SimpleXmlWriter(StringCollector collector,
java.lang.String preamble,
int indentation)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
SimpleXmlWriter |
begin(java.lang.String tag,
java.lang.Object... attributes)
Write the opening tag of an element.
|
SimpleXmlWriter |
beginI(java.lang.String tag,
Indexable<?> attributes)
Write the opening tag of an element, using indexed attributes.
|
SimpleXmlWriter |
beginP(java.lang.String tag,
Indexable<? extends Pair<?>> attributes)
Write the opening tag of an element, using paired attributes.
|
SimpleXmlWriter |
cdata(java.lang.String text)
Outputs the raw text as a CDATA section.
|
void |
close() |
SimpleXmlWriter |
comment(java.lang.String comment)
Add a comment.
|
SimpleXmlWriter |
element(java.lang.String tag,
java.lang.Object... attributes)
Writes an empty element.
|
SimpleXmlWriter |
elementI(java.lang.String tag,
Indexable<?> attributes)
Writes an empty element with indexed attributes.
|
SimpleXmlWriter |
elementP(java.lang.String tag,
Indexable<? extends Pair<?>> attributes)
Writes an empty element with paired attributes.
|
SimpleXmlWriter |
end()
Close the latest open element.
|
SimpleXmlWriter |
end(java.lang.String tag)
Close the given element.
|
SimpleXmlWriter |
expand(byte[] data)
Convert data into hex code and add it as text.
|
static java.lang.String |
expand(byte[] data,
int columns)
Expand an array of bytes into a hex string.
|
SimpleXmlWriter |
expandBase64(byte[] data)
Convert data into hex code and add it as text.
|
static java.lang.String |
getDefaultPreamble()
Get the default XML preamble with UTF-8 encoding.
|
java.lang.String |
getIndentation()
Get the indentation.
|
java.util.Locale |
getLocale()
Get the locale used for formatting text.
|
static java.lang.String |
getPreamble(java.lang.String encoding)
Get an XML preamble with the given encoding.
|
SimpleXmlWriter |
indentedText(java.lang.Object text)
Output text following the current indentation.
|
SimpleXmlWriter |
indentedText(java.lang.String format,
java.lang.Object... args)
Output formatted text following the current indentation.
|
boolean |
isAutoWrap()
Is automatic wrapping enabled?
|
SimpleXmlWriter |
loop(java.lang.String loopTag,
java.lang.Iterable<? extends XmlStorable> items)
Store a loop with no size.
|
SimpleXmlWriter |
loop(java.lang.String loopTag,
java.lang.String sizeAttr,
java.util.Collection<? extends XmlStorable> items)
Store a sized loop with a size.
|
SimpleXmlWriter |
loop(java.lang.String loopTag,
java.lang.String sizeAttr,
Countable<? extends XmlStorable> items)
Store a sized loop with a size.
|
SimpleXmlWriter |
loop(java.lang.String loopTag,
java.lang.String sizeAttr,
XmlStorable... items)
Store a sized loop with a size.
|
SimpleXmlWriter |
nl()
Add a newline.
|
SimpleXmlWriter |
text(java.lang.Object text)
Add text outside of tags.
|
SimpleXmlWriter |
textf(java.lang.String format,
java.lang.Object... args)
Add formatted text outside of tags.
|
public static final java.lang.String DEFAULT_ENCODING
@NotNull protected final StringCollector collector
protected final boolean autoWrap
@Nullable protected final java.lang.String indentation
null
.@NotNull protected final java.util.Locale locale
public SimpleXmlWriter(@NotNull StringCollector collector)
default preamble
,
default locale, no indentation, and no automatic wrapping,collector
- string collector to which the XML output is writtenOutputStreamStringCollector
,
TextStringCollector
public SimpleXmlWriter(@NotNull java.util.Locale locale, @NotNull StringCollector collector)
default preamble
,
no indentation, and no automatic wrapping,locale
- locale used for text formatting in textf(String, Object...)
collector
- string collector to which the XML output is writtenOutputStreamStringCollector
,
TextStringCollector
public SimpleXmlWriter(@NotNull StringCollector collector, @Nullable java.lang.String preamble)
collector
- string collector to which the XML output is writtenpreamble
- preamble written at the start, if null
no preamble is writtenOutputStreamStringCollector
,
TextStringCollector
public SimpleXmlWriter(@NotNull java.util.Locale locale, @NotNull StringCollector collector, @Nullable java.lang.String preamble)
locale
- locale used for text formatting in textf(String, Object...)
collector
- string collector to which the XML output is writtenpreamble
- preamble written at the start, if null
no preamble is writtenOutputStreamStringCollector
,
TextStringCollector
public SimpleXmlWriter(@NotNull StringCollector collector, int indentation)
default preamble
.collector
- string collector to which the XML output is writtenindentation
- if 0
or more automatic newlines will be inserted and nested levels will be indented
by the given number of spacespublic SimpleXmlWriter(@NotNull java.util.Locale locale, @NotNull StringCollector collector, int indentation)
default preamble
.locale
- locale used for text formatting in textf(String, Object...)
collector
- string collector to which the XML output is writtenindentation
- if 0
or more automatic newlines will be inserted and nested levels will be indented
by the given number of spacespublic SimpleXmlWriter(@NotNull StringCollector collector, @Nullable java.lang.String preamble, int indentation)
collector
- string collector to which the XML output is writtenpreamble
- preamble to be written at the beginning of the output, use null
for no preambleindentation
- if 0
or more automatic newlines will be inserted and nested levels will be indented
by the given number of spacesgetDefaultPreamble()
,
getPreamble(String)
public SimpleXmlWriter(@NotNull java.util.Locale locale, @NotNull StringCollector collector, @Nullable java.lang.String preamble, int indentation)
locale
- locale used for text formatting in textf(String, Object...)
collector
- string collector to which the XML output is writtenpreamble
- preamble to be written at the beginning of the output, use null
for no preambleindentation
- if 0
or more automatic newlines will be inserted and nested levels will be indented
by the given number of spacesgetDefaultPreamble()
,
getPreamble(String)
public boolean isAutoWrap()
true
: automatic wrapping is enabledfalse
: automatic wrapping is disabled@NotNull public java.lang.String getIndentation()
@NotNull public java.util.Locale getLocale()
textf(String, Object...)
@NotNull public static java.lang.String getDefaultPreamble()
@NotNull public static java.lang.String getPreamble(@NotNull java.lang.String encoding)
encoding
- encoding@NotNull public SimpleXmlWriter begin(@NotNull java.lang.String tag, java.lang.Object... attributes)
tag
- element tagattributes
- attributes, always in pairs of attribute name
and attribute value, values will be escaped@NotNull public SimpleXmlWriter beginI(@NotNull java.lang.String tag, @NotNull Indexable<?> attributes)
tag
- element tagattributes
- attributes, always in pairs of attribute name
and attribute value, values will be escaped@NotNull public SimpleXmlWriter beginP(@NotNull java.lang.String tag, @NotNull Indexable<? extends Pair<?>> attributes)
tag
- element tagattributes
- attributes, always in pairs of attribute name
and attribute value, values will be escaped@NotNull public SimpleXmlWriter end()
java.util.EmptyStackException
- if closing although no element is open@NotNull public SimpleXmlWriter end(@Nullable java.lang.String tag)
In difference to end()
this method checks whether the
closing tag matches the latest opening tag when a tag is given.
tag
- tag expected to end, or null
to ignorejava.lang.IllegalStateException
- if tag does not match@NotNull public SimpleXmlWriter comment(@NotNull java.lang.String comment)
comment
- comment to add@NotNull public SimpleXmlWriter expand(@NotNull byte[] data)
data
- data to add@NotNull public static java.lang.String expand(@NotNull byte[] data, int columns)
data
- data to expandcolumns
- columns of hex digits of returned string before a line break is inserted.
If 0
or less no break is inserted.@NotNull public SimpleXmlWriter expandBase64(@NotNull byte[] data)
data
- data to add@NotNull public SimpleXmlWriter element(@NotNull java.lang.String tag, java.lang.Object... attributes)
tag
- element tagattributes
- attributes, always in pairs of attribute name
and attribute value, values will be escaped@NotNull public SimpleXmlWriter elementI(@NotNull java.lang.String tag, @NotNull Indexable<?> attributes)
tag
- element tagattributes
- attributes, always in pairs of attribute name
and attribute value, values will be escaped@NotNull public SimpleXmlWriter elementP(@NotNull java.lang.String tag, @NotNull Indexable<? extends Pair<?>> attributes)
tag
- element tagattributes
- attributes, in pairs where OrderedPair.first
defines the attribute's name,
and OrderedPair.second
defines its value@NotNull public SimpleXmlWriter text(@NotNull java.lang.Object text)
text
- text to add@NotNull public SimpleXmlWriter indentedText(@NotNull java.lang.Object text)
text
- text to be outputted@NotNull public SimpleXmlWriter textf(@NotNull java.lang.String format, @NotNull java.lang.Object... args)
String.format(String, Object...)
using the locale
set via the constructor.format
- format stringargs
- arguments for the format string@NotNull public SimpleXmlWriter indentedText(@NotNull java.lang.String format, java.lang.Object... args)
String.format(String, Object...)
using the locale
set via the constructor.
It will also remove all the whitespace around the text when outputting. If indentation is enabled, all lines will be outputted correctly indented, removing the whitespace around each line.
format
- format stringargs
- arguments for the format string@NotNull public SimpleXmlWriter nl()
@NotNull public SimpleXmlWriter cdata(@NotNull java.lang.String text)
text
- text to be outputted, not escaped in any formpublic void close()
close
in interface java.lang.AutoCloseable
@NotNull public SimpleXmlWriter loop(@NotNull java.lang.String loopTag, @NotNull java.lang.Iterable<? extends XmlStorable> items)
loopTag
- tag for enclosing elementitems
- items to output@NotNull public SimpleXmlWriter loop(@NotNull java.lang.String loopTag, @NotNull java.lang.String sizeAttr, @NotNull Countable<? extends XmlStorable> items)
items
as its value.loopTag
- tag for enclosing elementsizeAttr
- attribute for the sizeitems
- items to output@NotNull public SimpleXmlWriter loop(@NotNull java.lang.String loopTag, @NotNull java.lang.String sizeAttr, @NotNull java.util.Collection<? extends XmlStorable> items)
items
as its value.loopTag
- tag for enclosing elementsizeAttr
- attribute for the sizeitems
- items to output@NotNull public SimpleXmlWriter loop(@NotNull java.lang.String loopTag, @NotNull java.lang.String sizeAttr, @NotNull XmlStorable... items)
items
as its value.loopTag
- tag for enclosing elementsizeAttr
- attribute for the sizeitems
- items to output