Package io.jenetics.xml.stream
Class Reader<T>
- java.lang.Object
-
- io.jenetics.xml.stream.Reader<T>
-
public abstract class Reader<T> extends Object
XML reader class, used for reading objects in XML format. XML<int-chromosome length="3"> <min>-2147483648</min> <max>2147483647</max> <alleles> <allele>-1878762439</allele> <allele>-957346595</allele> <allele>-88668137</allele> </alleles> </int-chromosome>
final Reader<IntegerChromosome> reader = elem( (Object[] v) -> { final int length = (int)v[0]; final int min = (int)v[1]; final int max = (int)v[2]; final List<Integer> alleles = (List<Integer>)v[3]; assert alleles.size() == length; return IntegerChromosome.of( alleles.stream() .map(value -> IntegerGene.of(value, min, max) .toArray(IntegerGene[]::new) ); }, "int-chromosome", attr("length").map(Integer::parseInt), elem("min", text().map(Integer::parseInt)), elem("max", text().map(Integer::parseInt)), elem("alleles", elems(elem("allele", text().map(Integer::parseInt))) ) );
- Since:
- 3.9
- Version:
- 3.9
- Author:
- Franz Wilhelmstötter
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Reader<String>
attr(String name)
Return aReader
for reading an attribute of an element.static <T> Reader<T>
elem(String name, Reader<? extends T> reader)
Return aReader
which reads the value from the child elements of the given parent elementname
.static <T> Reader<T>
elem(Function<Object[],T> generator, String name, Reader<?>... children)
Return aReader
for reading an object of typeT
from the XML element with the givenname
.static <T> Reader<List<T>>
elems(Reader<? extends T> reader)
Return aReader
which collects the elements, read by the given childreader
, and returns it as list of these elements.<B> Reader<B>
map(Function<? super T,? extends B> mapper)
Create a new reader for the new mapped typeB
.abstract T
read(XMLStreamReader xml)
Read the given type from the underlying XML streamreader
.static Reader<String>
text()
Return aReader
for reading the text of an element.String
toString()
-
-
-
Method Detail
-
read
public abstract T read(XMLStreamReader xml) throws XMLStreamException
Read the given type from the underlying XML streamreader
.try (AutoCloseableXMLStreamReader xml = XML.reader(in)) { // Move XML stream to first element. xml.next(); return reader.read(xml); }
- Parameters:
xml
- the underlying XML streamreader
- Returns:
- the data read from the XML stream, maybe
null
- Throws:
XMLStreamException
- if an error occurs while reading the valueNullPointerException
- if the givenxml
stream reader isnull
-
map
public <B> Reader<B> map(Function<? super T,? extends B> mapper)
Create a new reader for the new mapped typeB
.- Type Parameters:
B
- the target type of the new reader- Parameters:
mapper
- the mapper function- Returns:
- a new reader
- Throws:
NullPointerException
- if the givenmapper
function isnull
-
attr
public static Reader<String> attr(String name)
Return aReader
for reading an attribute of an element.XML
<element length="3"/>
final Reader<Integer> reader = elem( v -> (Integer)v[0], "element", attr("length").map(Integer::parseInt) );
- Parameters:
name
- the attribute name- Returns:
- an attribute reader
- Throws:
NullPointerException
- if the givenname
isnull
-
text
public static Reader<String> text()
Return aReader
for reading the text of an element.XML
<element>1234<element>
final Reader<Integer> reader = elem( v -> (Integer)v[0], "element", text().map(Integer::parseInt) );
- Returns:
- an element text reader
-
elem
public static <T> Reader<T> elem(Function<Object[],T> generator, String name, Reader<?>... children)
Return aReader
for reading an object of typeT
from the XML element with the givenname
.XML
<property name="size">1234<property>
final Reader<Property> reader = elem( v -> { final String name = (String)v[0]; final Integer value = (Integer)v[1]; return Property.of(name, value); }, "property", attr("name"), text().map(Integer::parseInt) );
- Type Parameters:
T
- the reader result type- Parameters:
generator
- the generator function, which build the result object from the given parameter arrayname
- the name of the root (sub-tree) elementchildren
- the child element reader, which creates the values forwarded to thegenerator
function- Returns:
- a node reader
- Throws:
NullPointerException
- if one of the given arguments isnull
IllegalArgumentException
- if the given child readers contains more than one text reader
-
elem
public static <T> Reader<T> elem(String name, Reader<? extends T> reader)
Return aReader
which reads the value from the child elements of the given parent elementname
.XML
<min><property name="size">1234<property></min>
final Reader<Property> reader = elem("min", elem( v -> { final String name = (String)v[0]; final Integer value = (Integer)v[1]; return Property.of(name, value); }, "property", attr("name"), text().map(Integer::parseInt) ) );
- Type Parameters:
T
- the result type- Parameters:
name
- the parent element namereader
- the child elements reader- Returns:
- a node reader
- Throws:
NullPointerException
- if one of the given arguments isnull
-
elems
public static <T> Reader<List<T>> elems(Reader<? extends T> reader)
Return aReader
which collects the elements, read by the given childreader
, and returns it as list of these elements.XML
<properties length="3"> <property>-1878762439</property> <property>-957346595</property> <property>-88668137</property> </properties>
Reader<List<Integer>> reader = elem( v -> (List<Integer>)v[0], "properties", elems(elem("property", text().map(Integer::parseInt))) );
- Type Parameters:
T
- the element type- Parameters:
reader
- the child element reader- Returns:
- a list reader
-
-