Class 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.

    Since 2.0.0 checked exceptions are wrapped in an XMLException.

    Since:
    1.5.0
    Author:
    Pascal Essiembre
    • 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 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 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 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:
        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) name
        value - element (tag) value
        writeBlanks - 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 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 IXMLConfigurable, its is responsible for creating its own XML through its IXMLConfigurable.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:
        writeElementObject(String, Object)
      • writeObject

        public void writeObject​(String localName,
                                Object value,
                                boolean disabled)
        Writes an object. If the object implements IXMLConfigurable, its is responsible for creating its own XML through its IXMLConfigurable.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:
        writeElementObject(String, Object)
      • writeObjectList

        public void writeObjectList​(String parentLocalName,
                                    String localName,
                                    List<? extends Object> values)
        Writes a list of objects. If an object implements IXMLConfigurable, its is responsible for creating its own XML through its IXMLConfigurable.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:
        writeElementObjectList(String, String, List)
      • 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