Package com.norconex.commons.lang.xml
Class EnhancedXMLStreamWriter
- java.lang.Object
-
- com.norconex.commons.lang.xml.EnhancedXMLStreamWriter
-
- All Implemented Interfaces:
XMLStreamWriter
public class EnhancedXMLStreamWriter extends Object implements XMLStreamWriter
A version of
XMLStreamWriter
that adds convenience methods for adding simple elements and typed attributes, as well as offering pretty-printing. Can be used on its own with a Writer, or as a wrapper to an existingXMLStreamWriter
instance.Since 2.0.0 checked exceptions are wrapped in an
XMLException
.- Since:
- 1.5.0
- Author:
- Pascal Essiembre
-
-
Constructor Summary
Constructors Constructor Description EnhancedXMLStreamWriter(Writer writer)
EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks)
Creates a new xml stream writer.EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks, int indent)
Creates a new xml stream writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
NamespaceContext
getNamespaceContext()
String
getPrefix(String uri)
Object
getProperty(String name)
Writer
getWriter()
Gets the underlying writer, after flushing this XML stream writer.void
setDefaultNamespace(String uri)
void
setNamespaceContext(NamespaceContext context)
void
setPrefix(String prefix, String uri)
void
writeAttribute(String localName, String value)
void
writeAttribute(String namespaceURI, String localName, String value)
void
writeAttribute(String prefix, String namespaceURI, String localName, String value)
void
writeAttributeBoolean(String localName, Boolean value)
void
writeAttributeBoolean(String localName, Boolean value, boolean writeBlanks)
Writes a Boolean attribute.void
writeAttributeClass(Class<?> value)
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName()
.void
writeAttributeClass(Class<?> value, boolean writeBlanks)
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName()
.void
writeAttributeClass(String localName, Class<?> value)
void
writeAttributeClass(String localName, Class<?> value, boolean writeBlanks)
Writes an attribute containing a class name, getting it by invokingClass.getCanonicalName()
.void
writeAttributeDelimited(String localName, List<?> values)
Writes an attribute by converting a list into a comma-separated string.void
writeAttributeDelimited(String localName, List<?> values, String delimiter)
Writes an attribute by converting a list into a comma-separated string.void
writeAttributeDisabled()
Write a "disabled" attribute set totrue
.void
writeAttributeDisabled(boolean value)
Write a "disabled" attribute.void
writeAttributeDisabled(Boolean value, boolean writeBlanks)
Write a "disabled" attribute.void
writeAttributeDouble(String localName, Double value)
void
writeAttributeDouble(String localName, Double value, boolean writeBlanks)
Writes a Double attribute.void
writeAttributeFloat(String localName, Float value)
void
writeAttributeFloat(String localName, Float value, boolean writeBlanks)
Writes a Float attribute.void
writeAttributeInteger(String localName, Integer value)
void
writeAttributeInteger(String localName, Integer value, boolean writeBlanks)
Writes a Integer attribute.void
writeAttributeLong(String localName, Long value)
void
writeAttributeLong(String localName, Long value, boolean writeBlanks)
Writes a Long attribute.void
writeAttributeObject(String localName, Object value)
Writes an attribute object by first converting it to string using its "toString()" method.void
writeAttributeObject(String localName, Object value, boolean writeBlanks)
Writes an attribute object by first converting it to string using its "toString()" method.void
writeAttributeString(String localName, String value)
void
writeAttributeString(String localName, String value, boolean writeBlanks)
Writes a String attribute.void
writeCData(String data)
void
writeCharacters(char[] text, int start, int len)
void
writeCharacters(String text)
void
writeComment(String data)
void
writeDefaultNamespace(String namespaceURI)
void
writeDTD(String dtd)
void
writeElementBoolean(String localName, Boolean value)
void
writeElementBoolean(String localName, Boolean value, boolean writeBlanks)
Writes a simple Boolean element.void
writeElementClass(String localName, Class<?> value)
void
writeElementClass(String localName, Class<?> value, boolean writeBlanks)
Writes a simple element containing a class name, getting it by invokingClass.getCanonicalName()
.void
writeElementDelimited(String localName, List<?> values)
Writes an element by converting a list into a comma-separated string.void
writeElementDelimited(String localName, List<?> values, String delimiter)
Writes an element by converting a list into a comma-separated string.void
writeElementDimension(String localName, Dimension value)
Writes a simple element containing a Dimension.void
writeElementDimension(String localName, Dimension value, boolean writeBlanks)
Writes a simple element containing a Dimension.void
writeElementDisabled(String localName)
Writes an empty element with a "disabled" attribute set totrue
.void
writeElementDisabled(String localName, Class<?> clazz)
Writes an empty element with a "disabled" attribute set totrue
and a "class" attribute matching the class name.void
writeElementDouble(String localName, Double value)
void
writeElementDouble(String localName, Double value, boolean writeBlanks)
Writes a simple Double element.void
writeElementFile(String localName, File value)
Writes aFile
element.void
writeElementFile(String localName, File value, boolean writeBlanks)
Writes aFile
element.void
writeElementFloat(String localName, Float value)
void
writeElementFloat(String localName, Float value, boolean writeBlanks)
Writes a simple Float element.void
writeElementInteger(String localName, Integer value)
void
writeElementInteger(String localName, Integer value, boolean writeBlanks)
Writes a simple Integer element.void
writeElementLong(String localName, Long value)
void
writeElementLong(String localName, Long value, boolean writeBlanks)
Writes a simple Long element.void
writeElementObject(String localName, Object value)
Writes an element object as string.void
writeElementObject(String localName, Object value, boolean writeBlanks)
Writes an element object as string.void
writeElementObjectList(String parentLocalName, String localName, List<?> values)
Writes a list of objects.void
writeElementObjectList(String parentLocalName, String localName, List<?> values, boolean writeBlanks)
Writes a list of objects.void
writeElementString(String localName, String value)
void
writeElementString(String localName, String value, boolean writeBlanks)
Writes a simple string element.void
writeEmptyElement(String localName)
void
writeEmptyElement(String namespaceURI, String localName)
void
writeEmptyElement(String prefix, String localName, String namespaceURI)
void
writeEndDocument()
void
writeEndElement()
void
writeEntityRef(String name)
void
writeNamespace(String prefix, String namespaceURI)
void
writeObject(String localName, Object value)
Writes an object.void
writeObject(String localName, Object value, boolean disabled)
Writes an object.void
writeObjectList(String parentLocalName, String localName, List<? extends Object> values)
Writes a list of objects.void
writeProcessingInstruction(String target)
void
writeProcessingInstruction(String target, String data)
void
writeStartDocument()
void
writeStartDocument(String version)
void
writeStartDocument(String encoding, String version)
void
writeStartElement(String localName)
void
writeStartElement(String localName, Class<?> clazz)
Writes a start element of the supplied name, with a "class" attribute for the given class.void
writeStartElement(String localName, Class<?> clazz, boolean disabled)
Writes a start element of the supplied name, with a "class" attribute for the given class, and a "disabled" attribute iftrue
.void
writeStartElement(String namespaceURI, String localName)
void
writeStartElement(String prefix, String localName, String namespaceURI)
-
-
-
Constructor Detail
-
EnhancedXMLStreamWriter
public EnhancedXMLStreamWriter(Writer writer)
-
EnhancedXMLStreamWriter
public EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks)
Creates a new xml stream writer.- Parameters:
writer
- writer used to write XMLwriteBlanks
-true
to write attributes/elements with no values when invoking methods without the "writeBlanks" argument. This sets the default behavior which can be overwritten using methods with "writeBlanks" argument.
-
EnhancedXMLStreamWriter
public EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks, int indent)
Creates a new xml stream writer.- Parameters:
writer
- writer used to write XMLwriteBlanks
-true
to write attributes/elements with no values when invoking methods without the "writeBlanks" argument. This sets the default behavior which can be overwritten using methods with "writeBlanks" argument.indent
- how many spaces to use for indentation (-1=no indent; 0=newline only; 1+=number of spaces after newline)- Since:
- 1.13.0
-
-
Method Detail
-
getWriter
public Writer getWriter()
Gets the underlying writer, after flushing this XML stream writer.- Returns:
- the writer
- Since:
- 2.0.0
-
writeAttributeInteger
public void writeAttributeInteger(String localName, Integer value, boolean writeBlanks)
Writes a Integer attribute.- Parameters:
localName
- attribute namevalue
- Integer attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeLong
public void writeAttributeLong(String localName, Long value, boolean writeBlanks)
Writes a Long attribute.- Parameters:
localName
- attribute namevalue
- Long attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeFloat
public void writeAttributeFloat(String localName, Float value, boolean writeBlanks)
Writes a Float attribute.- Parameters:
localName
- attribute namevalue
- Float attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeDouble
public void writeAttributeDouble(String localName, Double value, boolean writeBlanks)
Writes a Double attribute.- Parameters:
localName
- attribute namevalue
- Double attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeBoolean
public void writeAttributeBoolean(String localName, Boolean value, boolean writeBlanks)
Writes a Boolean attribute.- Parameters:
localName
- attribute namevalue
- Boolean attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeString
public void writeAttributeString(String localName, String value, boolean writeBlanks)
Writes a String attribute.- Parameters:
localName
- attribute namevalue
- String attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeClass
public void writeAttributeClass(Class<?> value, boolean writeBlanks)
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName()
.- Parameters:
value
- the class to writewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 2.0.0
-
writeAttributeClass
public void writeAttributeClass(Class<?> value)
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName()
.- Parameters:
value
- the class to write- Since:
- 2.0.0
-
writeAttributeClass
public void writeAttributeClass(String localName, Class<?> value, boolean writeBlanks)
Writes an attribute containing a class name, getting it by invokingClass.getCanonicalName()
.- Parameters:
localName
- attribute namevalue
- Class attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeDisabled
public void writeAttributeDisabled(Boolean value, boolean writeBlanks)
Write a "disabled" attribute.- Parameters:
value
-true
orfalse
writeBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 2.0.0
-
writeAttributeDisabled
public void writeAttributeDisabled(boolean value)
Write a "disabled" attribute.- Parameters:
value
-true
orfalse
- Since:
- 2.0.0
-
writeAttributeDisabled
public void writeAttributeDisabled()
Write a "disabled" attribute set totrue
.- Since:
- 2.0.0
-
writeAttributeObject
public void writeAttributeObject(String localName, Object value)
Writes an attribute object by first converting it to string using its "toString()" method.- Parameters:
localName
- attribute namevalue
- attribute value- Since:
- 1.14.0
-
writeAttributeObject
public void writeAttributeObject(String localName, Object value, boolean writeBlanks)
Writes an attribute object by first converting it to string using its "toString()" method.- Parameters:
localName
- attribute namevalue
- attribute valuewriteBlanks
- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeDelimited
public void writeAttributeDelimited(String localName, List<?> values)
Writes an attribute by converting a list into a comma-separated string.- Parameters:
localName
- attribute namevalues
- the values to write- Since:
- 2.0.0
-
writeAttributeDelimited
public void writeAttributeDelimited(String localName, List<?> values, String delimiter)
Writes an attribute by converting a list into a comma-separated string.- Parameters:
localName
- attribute namevalues
- the values to writedelimiter
- the string separating each values- Since:
- 2.0.0
-
writeElementDisabled
public void writeElementDisabled(String localName)
Writes an empty element with a "disabled" attribute set totrue
.- Parameters:
localName
- element (tag) name- Since:
- 2.0.0
-
writeElementDisabled
public void writeElementDisabled(String localName, Class<?> clazz)
Writes an empty element with a "disabled" attribute set totrue
and a "class" attribute matching the class name.- Parameters:
localName
- element (tag) nameclazz
- the class- Since:
- 2.0.0
-
writeElementInteger
public void writeElementInteger(String localName, Integer value, boolean writeBlanks)
Writes a simple Integer element.- Parameters:
localName
- element (tag) namevalue
- the Integer valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementLong
public void writeElementLong(String localName, Long value, boolean writeBlanks)
Writes a simple Long element.- Parameters:
localName
- element (tag) namevalue
- the Long valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementFloat
public void writeElementFloat(String localName, Float value, boolean writeBlanks)
Writes a simple Float element.- Parameters:
localName
- element (tag) namevalue
- the Float valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementDouble
public void writeElementDouble(String localName, Double value, boolean writeBlanks)
Writes a simple Double element.- Parameters:
localName
- element (tag) namevalue
- the Double valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementBoolean
public void writeElementBoolean(String localName, Boolean value, boolean writeBlanks)
Writes a simple Boolean element.- Parameters:
localName
- element (tag) namevalue
- the Boolean valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementString
public void writeElementString(String localName, String value, boolean writeBlanks)
Writes a simple string element.- Parameters:
localName
- element (tag) namevalue
- the string valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementClass
public void writeElementClass(String localName, Class<?> value, boolean writeBlanks)
Writes a simple element containing a class name, getting it by invokingClass.getCanonicalName()
.- Parameters:
localName
- element (tag) namevalue
- the classwriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementFile
public void writeElementFile(String localName, File value, boolean writeBlanks)
Writes aFile
element.- Parameters:
localName
- element (tag) namevalue
- the FilewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 2.0.0
-
writeElementFile
public void writeElementFile(String localName, File value)
Writes aFile
element.- Parameters:
localName
- element (tag) namevalue
- the File- Since:
- 2.0.0
-
writeElementDimension
public void writeElementDimension(String localName, Dimension value)
Writes a simple element containing a Dimension. The dimension will be written as [width]x[height] (e.g., 400x300) or just one numeric value if width and height are the same.- Parameters:
localName
- element (tag) namevalue
- the dimension- Since:
- 1.14.0
-
writeElementDimension
public void writeElementDimension(String localName, Dimension value, boolean writeBlanks)
Writes a simple element containing a Dimension. The dimension will be written as [width]x[height] (e.g., 400x300) or just one numeric value if width and height are the same.- Parameters:
localName
- element (tag) namevalue
- the dimensionwriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementObjectList
public void writeElementObjectList(String parentLocalName, String localName, List<?> values, boolean writeBlanks)
Writes a list of objects.- Parameters:
parentLocalName
- parent tag wrapping child elements (set tonull
for no parent)localName
- element (tag) namevalues
- the listwriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 2.0.0
-
writeElementObjectList
public void writeElementObjectList(String parentLocalName, String localName, List<?> values)
Writes a list of objects.- Parameters:
parentLocalName
- parent tag wrapping child elements (set tonull
for no parent)localName
- element (tag) namevalues
- the list- Since:
- 2.0.0
-
writeElementObject
public void writeElementObject(String localName, Object value)
Writes an element object as string. The object is converted to string using its "toString()" method and written as a characters into a single element.- Parameters:
localName
- element (tag) namevalue
- element (tag) value- Since:
- 1.14.0
- See Also:
writeObject(String, Object)
-
writeElementObject
public void writeElementObject(String localName, Object value, boolean writeBlanks)
Writes an element object as string. The object is converted to string using its "toString()" method and written as a characters into a single element.- Parameters:
localName
- element (tag) namevalue
- element (tag) valuewriteBlanks
- whether a blank value should be written as an empty element.- Since:
- 1.14.0
- See Also:
writeObject(String, Object)
-
writeElementDelimited
public void writeElementDelimited(String localName, List<?> values)
Writes an element by converting a list into a comma-separated string.- Parameters:
localName
- element namevalues
- the values to write- Since:
- 2.0.0
-
writeElementDelimited
public void writeElementDelimited(String localName, List<?> values, String delimiter)
Writes an element by converting a list into a comma-separated string.- Parameters:
localName
- element namevalues
- the values to writedelimiter
- the string separating each values- Since:
- 2.0.0
-
writeObject
public void writeObject(String localName, Object value)
Writes an object. If the object implementsIXMLConfigurable
, its is responsible for creating its own XML through itsIXMLConfigurable.saveToXML(XML)
method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
localName
- element (tag) namevalue
- the object to write- Since:
- 2.0.0
- See Also:
writeElementObject(String, Object)
-
writeObject
public void writeObject(String localName, Object value, boolean disabled)
Writes an object. If the object implementsIXMLConfigurable
, its is responsible for creating its own XML through itsIXMLConfigurable.saveToXML(XML)
method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
localName
- element (tag) namevalue
- the object to writedisabled
- whether the object should have the "diabled" attribute- Since:
- 2.0.0
- See Also:
writeElementObject(String, Object)
-
writeObjectList
public void writeObjectList(String parentLocalName, String localName, List<? extends Object> values)
Writes a list of objects. If an object implementsIXMLConfigurable
, its is responsible for creating its own XML through itsIXMLConfigurable.saveToXML(XML)
method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
parentLocalName
- parent tag wrapping child elements (set tonull
for no parent)localName
- element (tag) namevalues
- the objects to write- Since:
- 2.0.0
- See Also:
writeElementObjectList(String, String, List)
-
writeStartElement
public void writeStartElement(String localName)
- Specified by:
writeStartElement
in interfaceXMLStreamWriter
-
writeStartElement
public void writeStartElement(String localName, Class<?> clazz)
Writes a start element of the supplied name, with a "class" attribute for the given class.- Parameters:
localName
- tag nameclazz
- name of class, ornull
- Since:
- 2.0.0
-
writeStartElement
public void writeStartElement(String localName, Class<?> clazz, boolean disabled)
Writes a start element of the supplied name, with a "class" attribute for the given class, and a "disabled" attribute iftrue
.- Parameters:
localName
- tag nameclazz
- name of class, ornull
disabled
-true
to disable this class- Since:
- 2.0.0
-
writeStartElement
public void writeStartElement(String namespaceURI, String localName)
- Specified by:
writeStartElement
in interfaceXMLStreamWriter
-
writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI)
- Specified by:
writeStartElement
in interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String namespaceURI, String localName)
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI)
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String localName)
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
-
writeEndElement
public void writeEndElement()
- Specified by:
writeEndElement
in interfaceXMLStreamWriter
-
writeEndDocument
public void writeEndDocument()
- Specified by:
writeEndDocument
in interfaceXMLStreamWriter
-
close
public void close()
- Specified by:
close
in interfaceXMLStreamWriter
-
flush
public void flush()
- Specified by:
flush
in interfaceXMLStreamWriter
-
writeAttribute
public void writeAttribute(String localName, String value)
- Specified by:
writeAttribute
in interfaceXMLStreamWriter
-
writeAttribute
public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
- Specified by:
writeAttribute
in interfaceXMLStreamWriter
-
writeAttribute
public void writeAttribute(String namespaceURI, String localName, String value)
- Specified by:
writeAttribute
in interfaceXMLStreamWriter
-
writeNamespace
public void writeNamespace(String prefix, String namespaceURI)
- Specified by:
writeNamespace
in interfaceXMLStreamWriter
-
writeDefaultNamespace
public void writeDefaultNamespace(String namespaceURI)
- Specified by:
writeDefaultNamespace
in interfaceXMLStreamWriter
-
writeComment
public void writeComment(String data)
- Specified by:
writeComment
in interfaceXMLStreamWriter
-
writeProcessingInstruction
public void writeProcessingInstruction(String target)
- Specified by:
writeProcessingInstruction
in interfaceXMLStreamWriter
-
writeProcessingInstruction
public void writeProcessingInstruction(String target, String data)
- Specified by:
writeProcessingInstruction
in interfaceXMLStreamWriter
-
writeCData
public void writeCData(String data)
- Specified by:
writeCData
in interfaceXMLStreamWriter
-
writeDTD
public void writeDTD(String dtd)
- Specified by:
writeDTD
in interfaceXMLStreamWriter
-
writeEntityRef
public void writeEntityRef(String name)
- Specified by:
writeEntityRef
in interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument()
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument(String version)
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument(String encoding, String version)
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
-
writeCharacters
public void writeCharacters(String text)
- Specified by:
writeCharacters
in interfaceXMLStreamWriter
-
writeCharacters
public void writeCharacters(char[] text, int start, int len)
- Specified by:
writeCharacters
in interfaceXMLStreamWriter
-
getPrefix
public String getPrefix(String uri)
- Specified by:
getPrefix
in interfaceXMLStreamWriter
-
setPrefix
public void setPrefix(String prefix, String uri)
- Specified by:
setPrefix
in interfaceXMLStreamWriter
-
setDefaultNamespace
public void setDefaultNamespace(String uri)
- Specified by:
setDefaultNamespace
in interfaceXMLStreamWriter
-
setNamespaceContext
public void setNamespaceContext(NamespaceContext context)
- Specified by:
setNamespaceContext
in interfaceXMLStreamWriter
-
getNamespaceContext
public NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContext
in interfaceXMLStreamWriter
-
getProperty
public Object getProperty(String name)
- Specified by:
getProperty
in interfaceXMLStreamWriter
-
-