Class XMLStringBuffer


  • public class XMLStringBuffer
    extends Object
    This class allows you to generate an XML text document by pushing and popping tags from a stack maintained internally.
    Author:
    Cedric Beust Jul 21, 2003
    • Field Detail

      • EOL

        public static final String EOL
        End of line, value of 'line.separator' system property or '\n'
    • Constructor Detail

      • XMLStringBuffer

        public XMLStringBuffer()
      • XMLStringBuffer

        public XMLStringBuffer​(String start)
        Parameters:
        start - A string of spaces indicating the indentation at which to start the generation. This constructor will not insert an
      • XMLStringBuffer

        public XMLStringBuffer​(IBuffer buffer,
                               String start)
        Parameters:
        buffer - The StringBuffer to use internally to represent the document.
        start - A string of spaces indicating the indentation at which to start the generation.
    • Method Detail

      • setXmlDetails

        public void setXmlDetails​(String v,
                                  String enc)
        Set the xml version and encoding for this document.
        Parameters:
        v - the XML version
        enc - the XML encoding
      • setDocType

        public void setDocType​(String docType)
        Set the doctype for this document.
        Parameters:
        docType - The DOCTYPE string, without the "<!DOCTYPE " ">"
      • push

        public void push​(String tagName,
                         @Nullable
                         String schema,
                         @Nullable
                         Properties attributes)
        Push a new tag. Its value is stored and will be compared against the parameter passed to pop().
        Parameters:
        tagName - The name of the tag.
        schema - The schema to use (can be null or an empty string).
        attributes - A Properties file representing the attributes (or null)
      • push

        public void push​(String tagName,
                         @Nullable
                         String schema)
        Push a new tag. Its value is stored and will be compared against the parameter passed to pop().
        Parameters:
        tagName - The name of the tag.
        schema - The schema to use (can be null or an empty string).
      • push

        public void push​(String tagName,
                         @Nullable
                         Properties attributes)
        Push a new tag. Its value is stored and will be compared against the parameter passed to pop().
        Parameters:
        tagName - The name of the tag.
        attributes - A Properties file representing the attributes (or null)
      • push

        public void push​(String tagName,
                         String... attributes)
      • push

        public void push​(String tagName)
        Push a new tag. Its value is stored and will be compared against the parameter passed to pop().
        Parameters:
        tagName - The name of the tag.
      • pop

        public void pop()
        Pop the last pushed element without verifying it if matches the previously pushed tag.
      • pop

        public void pop​(String tagName)
        Pop the last pushed element and throws an AssertionError if it doesn't match the corresponding tag that was pushed earlier.
        Parameters:
        tagName - The name of the tag this pop() is supposed to match.
      • addRequired

        public void addRequired​(String tagName,
                                @Nullable
                                String value)
        Add a required element to the current tag. An opening and closing tag will be generated even if value is null.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
      • addRequired

        public void addRequired​(String tagName,
                                @Nullable
                                String value,
                                @Nullable
                                Properties attributes)
        Add a required element to the current tag. An opening and closing tag will be generated even if value is null.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
        attributes - A Properties file containing the attributes (or null)
      • addOptional

        public void addOptional​(String tagName,
                                @Nullable
                                String value,
                                @Nullable
                                Properties attributes)
        Add an optional String element to the current tag. If value is null, nothing is added.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
        attributes - A Properties file containing the attributes (or null)
      • addOptional

        public void addOptional​(String tagName,
                                @Nullable
                                String value)
        Add an optional String element to the current tag. If value is null, nothing is added.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
      • addOptional

        public void addOptional​(String tagName,
                                @Nullable
                                Boolean value,
                                @Nullable
                                Properties attributes)
        Add an optional Boolean element to the current tag. If value is null, nothing is added.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
        attributes - A Properties file containing the attributes (or null)
      • addOptional

        public void addOptional​(String tagName,
                                @Nullable
                                Boolean value)
        Add an optional Boolean element to the current tag. If value is null, nothing is added.
        Parameters:
        tagName - The name of the tag
        value - The value for this tag
      • addEmptyElement

        public void addEmptyElement​(String tagName)
        Add an empty element tag (e.g. )
        Parameters:
        tagName - The name of the tag
      • addEmptyElement

        public void addEmptyElement​(String tagName,
                                    @Nullable
                                    Properties attributes)
        Add an empty element tag (e.g. )
        Parameters:
        tagName - The name of the tag
        attributes - A Properties file containing the attributes (or null)
      • addEmptyElement

        public void addEmptyElement​(String tagName,
                                    String... attributes)
      • addComment

        public void addComment​(String comment)
      • addString

        public void addString​(String s)
      • setDefaultComment

        public void setDefaultComment​(String defaultComment)
      • addCDATA

        public void addCDATA​(String content)
        Add a CDATA tag.
      • getStringBuffer

        public IBuffer getStringBuffer()
        Returns:
        The StringBuffer used to create the document.
      • toXML

        public String toXML()
        Returns:
        The String representation of the XML for this XMLStringBuffer.
      • getCurrentIndent

        public String getCurrentIndent()
      • toWriter

        public void toWriter​(Writer fw)