Class XmlWriter
- java.lang.Object
-
- org.ovirt.api.metamodel.runtime.xml.XmlWriter
-
- All Implemented Interfaces:
AutoCloseable
public class XmlWriter extends Object implements AutoCloseable
This class wraps theXMLStreamWriter
class so that the methods don't send checked exceptions, in order to simplify its usage together with streams and lambdas.
-
-
Constructor Summary
Constructors Constructor Description XmlWriter(File file, boolean indent)
Creates a writer that will write to the given file, using UTF-8 as the encoding.XmlWriter(OutputStream out, boolean indent)
Creates an XML writer that will write to the given stream, using UTF-8 as the encoding.XmlWriter(Writer out, boolean indent)
Creates an XML writer that will write to the given writer.XmlWriter(Result result, boolean indent)
Creates an XML writer that will write to the given result, using UTF-8 as the encoding.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the XML document and the underlying result.void
flush()
Flushes the output.static String
renderBoolean(boolean value)
Render a string representation of boolean valuestatic String
renderDate(Date value)
Render a string representation of Date valuestatic String
renderDecimal(BigDecimal value)
Render a string representation of BigDecimal valuestatic String
renderInteger(BigInteger value)
Render a string representation of BigInteger valuestatic String
renderOther(Object value)
Render a string representation of any value other than the standard: String, Boolean, Integer, Decimal, Date by defaulting to .toString()static String
renderString(String value)
Render a string representation of string valuevoid
setPrefix(String prefix, String uri)
Associates a namespace prefix with a namespace URI.void
writeAttribute(String name, String value)
Writes an XML attribute with the given name and value.void
writeBoolean(String name, boolean value)
Writes a boolean value.void
writeBooleans(String name, List<Boolean> values)
Writes a list of boolean values.void
writeCharacters(String text)
Writes the given characters as text content.void
writeDate(String name, Date value)
Writes a date.void
writeDates(String name, List<Date> values)
Writes a list of date values.void
writeDecimal(String name, BigDecimal value)
Writes a decimal value.void
writeDecimals(String name, List<BigDecimal> values)
Writes a list of decimal values.void
writeElement(String name, String value)
Writes an XML element with the given name and value.void
writeElement(String uri, String name, String value)
Writes an XML element with the given name and value.void
writeElements(String name, List<String> values)
Writes a list of string values.void
writeEndDocument()
Writes the end of the document.void
writeEndElement()
Closes the latest XML element started with thewriteStartElement(String)
.void
writeInteger(String name, BigInteger value)
Writes an integer value.void
writeIntegers(String name, List<BigInteger> values)
Writes a list of integer values.void
writeLine()
Writes a blank line.void
writeStartDocument(String encoding, String version)
Writes the start of the document.void
writeStartElement(String name)
Writes the start of an XML element with the given name.void
writeStartElement(String uri, String name)
Writes the start of an XML element with the given name.void
writeString(String name, String value)
Writes a string.
-
-
-
Constructor Detail
-
XmlWriter
public XmlWriter(Result result, boolean indent)
Creates an XML writer that will write to the given result, using UTF-8 as the encoding.- Parameters:
result
- the result where the document will be writtenindent
- indicates if the output should be indented
-
XmlWriter
public XmlWriter(OutputStream out, boolean indent)
Creates an XML writer that will write to the given stream, using UTF-8 as the encoding.- Parameters:
out
- the stream where the document will be writtenindent
- indicates if the output should be indented
-
XmlWriter
public XmlWriter(Writer out, boolean indent)
Creates an XML writer that will write to the given writer.- Parameters:
out
- the writer where the document will be writtenindent
- indicates if the output should be indented
-
XmlWriter
public XmlWriter(File file, boolean indent)
Creates a writer that will write to the given file, using UTF-8 as the encoding.- Parameters:
file
- the file where the document will be writtenindent
- indicates if the output should be indented
-
-
Method Detail
-
writeStartDocument
public void writeStartDocument(String encoding, String version)
Writes the start of the document.- Parameters:
encoding
- the character encoding used in the documentversion
- the XML version used in the document
-
writeEndDocument
public void writeEndDocument()
Writes the end of the document.
-
setPrefix
public void setPrefix(String prefix, String uri)
Associates a namespace prefix with a namespace URI.- Parameters:
prefix
- the namespace prefixuri
- the namespace URI
-
writeElement
public void writeElement(String uri, String name, String value)
Writes an XML element with the given name and value. For example, if the name issize
and the value is100
it will write<size>100</size>
.- Parameters:
uri
- the namespace URIname
- the name of the XML elementvalue
- the text content of the XML element
-
writeElement
public void writeElement(String name, String value)
Writes an XML element with the given name and value. For example, if the name issize
and the value is100
it will write<size>100</size>
.- Parameters:
name
- the name of the XML elementvalue
- the text content of the XML element
-
writeStartElement
public void writeStartElement(String uri, String name)
Writes the start of an XML element with the given name. For example, if the name issize
it will write<size>
.- Parameters:
uri
- the namespace URIname
- the name of the XML element
-
writeStartElement
public void writeStartElement(String name)
Writes the start of an XML element with the given name. For example, if the name issize
it will write<size>
.- Parameters:
name
- the name of the XML element
-
writeEndElement
public void writeEndElement()
Closes the latest XML element started with thewriteStartElement(String)
.
-
writeAttribute
public void writeAttribute(String name, String value)
Writes an XML attribute with the given name and value. For example, if the name issize
and the value is100
it will writesize="100"
.- Parameters:
name
- the name of the XML attributevalue
- the text content of the XML attribute
-
writeCharacters
public void writeCharacters(String text)
Writes the given characters as text content.- Parameters:
text
- the characters to write
-
writeLine
public void writeLine()
Writes a blank line.
-
writeBoolean
public void writeBoolean(String name, boolean value)
Writes a boolean value.
-
writeInteger
public void writeInteger(String name, BigInteger value)
Writes an integer value.
-
writeDecimal
public void writeDecimal(String name, BigDecimal value)
Writes a decimal value.
-
writeBooleans
public void writeBooleans(String name, List<Boolean> values)
Writes a list of boolean values.
-
writeIntegers
public void writeIntegers(String name, List<BigInteger> values)
Writes a list of integer values.
-
writeDecimals
public void writeDecimals(String name, List<BigDecimal> values)
Writes a list of decimal values.
-
writeElements
public void writeElements(String name, List<String> values)
Writes a list of string values.
-
renderString
public static String renderString(String value)
Render a string representation of string value
-
renderBoolean
public static String renderBoolean(boolean value)
Render a string representation of boolean value
-
renderInteger
public static String renderInteger(BigInteger value)
Render a string representation of BigInteger value
-
renderDecimal
public static String renderDecimal(BigDecimal value)
Render a string representation of BigDecimal value
-
renderDate
public static String renderDate(Date value)
Render a string representation of Date value
-
renderOther
public static String renderOther(Object value)
Render a string representation of any value other than the standard: String, Boolean, Integer, Decimal, Date by defaulting to .toString()
-
flush
public void flush()
Flushes the output.
-
close
public void close()
Closes the XML document and the underlying result.- Specified by:
close
in interfaceAutoCloseable
-
-