Package io.jenetics.xml.stream
Interface Writer<T>
-
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Writer<T>
XML writer interface, used for writing objects in XML format. The following XML will show the marshaled representation of anIntegerChromosome
.<int-chromosome length="3"> <min>-2147483648</min> <max>2147483647</max> <alleles> <allele>-1878762439</allele> <allele>-957346595</allele> <allele>-88668137</allele> </alleles> </int-chromosome>
Writer
definition.How to write the XML writing is shown by the next code snippet.final Writer<IntegerChromosome> writer = elem("int-chromosome", attr("length").map(ch -> ch.length()), elem("min", Writer.<Integer>text().map(ch -> ch.getMin())), elem("max", Writer.<Integer>text().map(ch -> ch.getMax())), elem("alleles", elems("allele", Writer.<Integer>text()) .map(ch -> ISeq.of(ch).map(g -> g.getAllele())) ) );
final IntegerChromosome ch = IntegerChromosome.of(MIN_VALUE, MAX_VALUE, 3); try (AutoCloseableXMLStreamWriter xml = XML.writer(out, indent)) { write(ch, xml); }
- Since:
- 3.9
- Version:
- 3.9
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> Writer<T>
attr(String name)
Writes the attribute with the givenname
to the current outer element.static <T> Writer<T>
attr(String name, Object value)
Writes the attribute with the givenname
and a constantvalue
to the current outer element.static <T> Writer<T>
doc(Writer<? super T> writer)
Adds a XML prolog element written by the givenwriter
.static <T> Writer<T>
elem(String name, Writer<? super T>... children)
Create a newWriter
, which writes a XML element with the given name and writes the given children into it.static <T> Writer<Iterable<T>>
elems(Writer<? super T> writer)
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.static <T> Writer<Iterable<T>>
elems(String name, Writer<? super T> writer)
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.default <B> Writer<B>
map(Function<? super B,? extends T> mapper)
Maps this writer to a different base type.static <T> Writer<T>
text()
Create a new textWriter
, which writes the given data as string to the outer element.void
write(XMLStreamWriter xml, T data)
Write the data of typeT
to the given XML stream writer.
-
-
-
Method Detail
-
write
void write(XMLStreamWriter xml, T data) throws XMLStreamException
Write the data of typeT
to the given XML stream writer.- Parameters:
xml
- the underlyingXMLStreamWriter
, where the value is written todata
- the value to write- Throws:
XMLStreamException
- if writing the data failsNullPointerException
- if one of the arguments isnull
-
map
default <B> Writer<B> map(Function<? super B,? extends T> mapper)
Maps this writer to a different base type. Mapping to a different data type is necessary when you are going to write sub-objects of your basic data typeT
. E.g. the chromosome length or themin
andmax
value of anIntegerChromosome
.- Type Parameters:
B
- the new data type of returned writer- Parameters:
mapper
- the mapper function- Returns:
- a writer with changed type
-
attr
static <T> Writer<T> attr(String name)
Writes the attribute with the givenname
to the current outer element.final Writer<String> writer1 = elem("element", attr("attribute"));
- Type Parameters:
T
- the writer base type- Parameters:
name
- the attribute name- Returns:
- a new writer instance
- Throws:
NullPointerException
- if the attributename
isnull
- See Also:
attr(String, Object)
-
attr
static <T> Writer<T> attr(String name, Object value)
Writes the attribute with the givenname
and a constantvalue
to the current outer element.final Writer<MyObject> = elem("element", attr("version", "1.0"));
- Type Parameters:
T
- the writer base type- Parameters:
name
- the attribute namevalue
- the attribute value- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of thename
isnull
-
elem
@SafeVarargs static <T> Writer<T> elem(String name, Writer<? super T>... children)
Create a newWriter
, which writes a XML element with the given name and writes the given children into it.- Type Parameters:
T
- the writer base type- Parameters:
name
- the root element namechildren
- the XML child elements- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
text
static <T> Writer<T> text()
Create a new textWriter
, which writes the given data as string to the outer element.- Type Parameters:
T
- the data type, which is written as string to the outer element- Returns:
- a new text writer
-
elems
static <T> Writer<Iterable<T>> elems(String name, Writer<? super T> writer)
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.- Type Parameters:
T
- the writer base type- Parameters:
name
- the enclosing element name used for each data valuewriter
- the sub-element writer- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
elems
static <T> Writer<Iterable<T>> elems(Writer<? super T> writer)
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.- Type Parameters:
T
- the writer base type- Parameters:
writer
- the sub-element writer- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
doc
static <T> Writer<T> doc(Writer<? super T> writer)
Adds a XML prolog element written by the givenwriter
. The default values for encoding and version is set to "UTF-8" and "1.0", respectively.<?xml version="1.0" encoding="UTF-8"?>
- Type Parameters:
T
- the writer data type- Parameters:
writer
- the root element writer- Returns:
- a new writer instance
-
-