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 existing XMLStreamWriter instance. Also support XML with no root.

Since 2.0.0 checked exceptions are wrapped in an XmlException.

Since:
1.5.0
  • Constructor Details

    • EnhancedXmlStreamWriter

      public EnhancedXmlStreamWriter(Writer writer)
    • EnhancedXmlStreamWriter

      public EnhancedXmlStreamWriter(Writer writer, boolean writeBlanks)
      Creates a new xml stream writer.
      Parameters:
      writer - writer used to write XML
      writeBlanks - 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 XML
      writeBlanks - 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 Details

    • 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 name
      value - Integer attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeInteger

      public void writeAttributeInteger(String localName, Integer value)
    • writeAttributeLong

      public void writeAttributeLong(String localName, Long value, boolean writeBlanks)
      Writes a Long attribute.
      Parameters:
      localName - attribute name
      value - Long attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeLong

      public void writeAttributeLong(String localName, Long value)
    • writeAttributeFloat

      public void writeAttributeFloat(String localName, Float value, boolean writeBlanks)
      Writes a Float attribute.
      Parameters:
      localName - attribute name
      value - Float attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeFloat

      public void writeAttributeFloat(String localName, Float value)
    • writeAttributeDouble

      public void writeAttributeDouble(String localName, Double value, boolean writeBlanks)
      Writes a Double attribute.
      Parameters:
      localName - attribute name
      value - Double attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeDouble

      public void writeAttributeDouble(String localName, Double value)
    • writeAttributeBoolean

      public void writeAttributeBoolean(String localName, Boolean value, boolean writeBlanks)
      Writes a Boolean attribute.
      Parameters:
      localName - attribute name
      value - Boolean attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeBoolean

      public void writeAttributeBoolean(String localName, Boolean value)
    • writeAttributeString

      public void writeAttributeString(String localName, String value, boolean writeBlanks)
      Writes a String attribute.
      Parameters:
      localName - attribute name
      value - String attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeString

      public void writeAttributeString(String localName, String value)
    • writeAttributeClass

      public void writeAttributeClass(Class<?> value, boolean writeBlanks)
      Write a "class" attribute with the value obtained from getting it by invoking Class.getCanonicalName().
      Parameters:
      value - the class to write
      writeBlanks - 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 invoking Class.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 invoking Class.getCanonicalName().
      Parameters:
      localName - attribute name
      value - Class attribute value
      writeBlanks - whether a blank value should be written as an empty attribute.
      Since:
      1.14.0
    • writeAttributeClass

      public void writeAttributeClass(String localName, Class<?> value)
    • writeAttributeDisabled

      public void writeAttributeDisabled(Boolean value, boolean writeBlanks)
      Write a "disabled" attribute.
      Parameters:
      value - true or false
      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 or false
      Since:
      2.0.0
    • writeAttributeDisabled

      public void writeAttributeDisabled()
      Write a "disabled" attribute set to true.
      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 name
      value - 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 name
      value - attribute value
      writeBlanks - 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 name
      values - 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 name
      values - the values to write
      delimiter - the string separating each values
      Since:
      2.0.0
    • writeElementDisabled

      public void writeElementDisabled(String localName)
      Writes an empty element with a "disabled" attribute set to true.
      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 to true and a "class" attribute matching the class name.
      Parameters:
      localName - element (tag) name
      clazz - 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) name
      value - the Integer value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementInteger

      public void writeElementInteger(String localName, Integer value)
    • writeElementLong

      public void writeElementLong(String localName, Long value, boolean writeBlanks)
      Writes a simple Long element.
      Parameters:
      localName - element (tag) name
      value - the Long value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementLong

      public void writeElementLong(String localName, Long value)
    • writeElementFloat

      public void writeElementFloat(String localName, Float value, boolean writeBlanks)
      Writes a simple Float element.
      Parameters:
      localName - element (tag) name
      value - the Float value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementFloat

      public void writeElementFloat(String localName, Float value)
    • writeElementDouble

      public void writeElementDouble(String localName, Double value, boolean writeBlanks)
      Writes a simple Double element.
      Parameters:
      localName - element (tag) name
      value - the Double value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementDouble

      public void writeElementDouble(String localName, Double value)
    • writeElementBoolean

      public void writeElementBoolean(String localName, Boolean value, boolean writeBlanks)
      Writes a simple Boolean element.
      Parameters:
      localName - element (tag) name
      value - the Boolean value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementBoolean

      public void writeElementBoolean(String localName, Boolean value)
    • writeElementString

      public void writeElementString(String localName, String value, boolean writeBlanks)
      Writes a simple string element.
      Parameters:
      localName - element (tag) name
      value - the string value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementString

      public void writeElementString(String localName, String value)
    • writeElementClass

      public void writeElementClass(String localName, Class<?> value, boolean writeBlanks)
      Writes a simple element containing a class name, getting it by invoking Class.getCanonicalName().
      Parameters:
      localName - element (tag) name
      value - the class
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
    • writeElementClass

      public void writeElementClass(String localName, Class<?> value)
    • writeElementFile

      public void writeElementFile(String localName, File value, boolean writeBlanks)
      Writes a File element.
      Parameters:
      localName - element (tag) name
      value - the File
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      2.0.0
    • writeElementFile

      public void writeElementFile(String localName, File value)
      Writes a File element.
      Parameters:
      localName - element (tag) name
      value - 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) name
      value - 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) name
      value - the dimension
      writeBlanks - 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 to null for no parent)
      localName - element (tag) name
      values - the list
      writeBlanks - 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 to null for no parent)
      localName - element (tag) name
      values - 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) name
      value - element (tag) value
      Since:
      1.14.0
      See Also:
    • 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) name
      value - element (tag) value
      writeBlanks - whether a blank value should be written as an empty element.
      Since:
      1.14.0
      See Also:
    • writeElementDelimited

      public void writeElementDelimited(String localName, List<?> values)
      Writes an element by converting a list into a comma-separated string.
      Parameters:
      localName - element name
      values - 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 name
      values - the values to write
      delimiter - the string separating each values
      Since:
      2.0.0
    • writeObject

      public void writeObject(String localName, Object value)
      Writes an object. If the object implements XmlConfigurable, its is responsible for creating its own XML through its XmlConfigurable.saveToXML(Xml) method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.
      Parameters:
      localName - element (tag) name
      value - the object to write
      Since:
      2.0.0
      See Also:
    • writeObject

      public void writeObject(String localName, Object value, boolean disabled)
      Writes an object. If the object implements XmlConfigurable, its is responsible for creating its own XML through its XmlConfigurable.saveToXML(Xml) method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.
      Parameters:
      localName - element (tag) name
      value - the object to write
      disabled - whether the object should have the "diabled" attribute
      Since:
      2.0.0
      See Also:
    • writeObjectList

      public void writeObjectList(String parentLocalName, String localName, List<? extends Object> values)
      Writes a list of objects. If an object implements XmlConfigurable, its is responsible for creating its own XML through its XmlConfigurable.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 to null for no parent)
      localName - element (tag) name
      values - the objects to write
      Since:
      2.0.0
      See Also:
    • writeStartElement

      public void writeStartElement(String localName)
      Specified by:
      writeStartElement in interface XMLStreamWriter
    • 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 name
      clazz - name of class, or null
      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 if true.
      Parameters:
      localName - tag name
      clazz - name of class, or null
      disabled - true to disable this class
      Since:
      2.0.0
    • writeStartElement

      public void writeStartElement(String namespaceURI, String localName)
      Specified by:
      writeStartElement in interface XMLStreamWriter
    • writeStartElement

      public void writeStartElement(String prefix, String localName, String namespaceURI)
      Specified by:
      writeStartElement in interface XMLStreamWriter
    • writeEmptyElement

      public void writeEmptyElement(String namespaceURI, String localName)
      Specified by:
      writeEmptyElement in interface XMLStreamWriter
    • writeEmptyElement

      public void writeEmptyElement(String prefix, String localName, String namespaceURI)
      Specified by:
      writeEmptyElement in interface XMLStreamWriter
    • writeEmptyElement

      public void writeEmptyElement(String localName)
      Specified by:
      writeEmptyElement in interface XMLStreamWriter
    • writeEndElement

      public void writeEndElement()
      Specified by:
      writeEndElement in interface XMLStreamWriter
    • writeEndDocument

      public void writeEndDocument()
      Specified by:
      writeEndDocument in interface XMLStreamWriter
    • close

      public void close()
      Specified by:
      close in interface XMLStreamWriter
    • flush

      public void flush()
      Specified by:
      flush in interface XMLStreamWriter
    • writeAttribute

      public void writeAttribute(String localName, String value)
      Specified by:
      writeAttribute in interface XMLStreamWriter
    • writeAttribute

      public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
      Specified by:
      writeAttribute in interface XMLStreamWriter
    • writeAttribute

      public void writeAttribute(String namespaceURI, String localName, String value)
      Specified by:
      writeAttribute in interface XMLStreamWriter
    • writeNamespace

      public void writeNamespace(String prefix, String namespaceURI)
      Specified by:
      writeNamespace in interface XMLStreamWriter
    • writeDefaultNamespace

      public void writeDefaultNamespace(String namespaceURI)
      Specified by:
      writeDefaultNamespace in interface XMLStreamWriter
    • writeComment

      public void writeComment(String data)
      Specified by:
      writeComment in interface XMLStreamWriter
    • writeProcessingInstruction

      public void writeProcessingInstruction(String target)
      Specified by:
      writeProcessingInstruction in interface XMLStreamWriter
    • writeProcessingInstruction

      public void writeProcessingInstruction(String target, String data)
      Specified by:
      writeProcessingInstruction in interface XMLStreamWriter
    • writeCData

      public void writeCData(String data)
      Specified by:
      writeCData in interface XMLStreamWriter
    • writeDTD

      public void writeDTD(String dtd)
      Specified by:
      writeDTD in interface XMLStreamWriter
    • writeEntityRef

      public void writeEntityRef(String name)
      Specified by:
      writeEntityRef in interface XMLStreamWriter
    • writeStartDocument

      public void writeStartDocument()
      Specified by:
      writeStartDocument in interface XMLStreamWriter
    • writeStartDocument

      public void writeStartDocument(String version)
      Specified by:
      writeStartDocument in interface XMLStreamWriter
    • writeStartDocument

      public void writeStartDocument(String encoding, String version)
      Specified by:
      writeStartDocument in interface XMLStreamWriter
    • writeCharacters

      public void writeCharacters(String text)
      Specified by:
      writeCharacters in interface XMLStreamWriter
    • writeCharacters

      public void writeCharacters(char[] text, int start, int len)
      Specified by:
      writeCharacters in interface XMLStreamWriter
    • getPrefix

      public String getPrefix(String uri)
      Specified by:
      getPrefix in interface XMLStreamWriter
    • setPrefix

      public void setPrefix(String prefix, String uri)
      Specified by:
      setPrefix in interface XMLStreamWriter
    • setDefaultNamespace

      public void setDefaultNamespace(String uri)
      Specified by:
      setDefaultNamespace in interface XMLStreamWriter
    • setNamespaceContext

      public void setNamespaceContext(NamespaceContext context)
      Specified by:
      setNamespaceContext in interface XMLStreamWriter
    • getNamespaceContext

      public NamespaceContext getNamespaceContext()
      Specified by:
      getNamespaceContext in interface XMLStreamWriter
    • getProperty

      public Object getProperty(String name)
      Specified by:
      getProperty in interface XMLStreamWriter