Class Indenter


  • class Indenter
    extends java.lang.Object
    The Indenter is used create indent strings using the stack paradigm. This allows XML documents to be generated by pushing and popping indents onto the stack. This indenter caches all indent strings created so that when the same position on the stack is encountered the indent can be acquired quickly.

    The indents created by this are all prefixed with the line feed character, which allows XML tags to span exclusive lines. If the indent size specified is zero or less then no spaces, or line feed will be added to the generated indent string.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Indenter.Cache
      The Cache object is used create an indexable list which allows the indenter to quickly acquire an indent using a stack position.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Indenter.Cache cache
      Provides a quick string cache that caches using by index.
      private int count
      Represents the current number of spaces in the indent text.
      private int indent
      Number of spaces that is used for each of the indents.
      private int index
      Represents the index within the cache to get the indent.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        Indenter()
      Constructor for the Indenter object.
        Indenter​(Format format)
      Constructor for the Indenter object.
      private Indenter​(Format format, int size)
      Constructor for the Indenter object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String create()
      This is used to create an indent which can later be pushed on to the stack.
      private java.lang.String indent​(int index)
      This is used to acquire the indent at the specified index.
      java.lang.String pop()
      This is used to pop an indent from the cache.
      java.lang.String push()
      This is used to push an indent on to the cache.
      java.lang.String top()
      This returns the current indent for this indenter.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • cache

        private Indenter.Cache cache
        Provides a quick string cache that caches using by index.
      • indent

        private int indent
        Number of spaces that is used for each of the indents.
      • count

        private int count
        Represents the current number of spaces in the indent text.
      • index

        private int index
        Represents the index within the cache to get the indent.
    • Constructor Detail

      • Indenter

        public Indenter()
        Constructor for the Indenter object. This will create an indent that uses three spaces for each indent that is pushed on to the stack. This also uses a default cache size of sixteen, which should be sufficient for most files.
      • Indenter

        public Indenter​(Format format)
        Constructor for the Indenter object. This will create an indent that uses the specified number of spaces to create each entry pushed on to the stack. This uses a cache size of sixteen, which should be sufficient for most files.
        Parameters:
        format - determines the number of spaces per indent
      • Indenter

        private Indenter​(Format format,
                         int size)
        Constructor for the Indenter object. This will create an indent that uses the specified number of spaces to create each entry pushed on to the stack. This uses a cache of the specified size, which is used to optimize the object.
        Parameters:
        format - determines the number of spaces per indent
        size - this is the initial size of the indent cache
    • Method Detail

      • top

        public java.lang.String top()
        This returns the current indent for this indenter. This should be used to write elements or comments that should be at the same indentation level as the XML element that will follow.
        Returns:
        this returns the current indentation level for this
      • push

        public java.lang.String push()
        This is used to push an indent on to the cache. The first indent created by this is an empty string, this is because an indent is not required for the start of an XML file. If there are multiple roots written to the same writer then the start and end tags of a root element will exist on the same line.
        Returns:
        this is used to push an indent on to the stack
      • pop

        public java.lang.String pop()
        This is used to pop an indent from the cache. This reduces the length of the current indent and is typically used when an end tag is added to an XML document. If the number of pop requests exceeds the number of push requests then an empty string is returned from this method.
        Returns:
        this is used to pop an indent from the stack
      • indent

        private java.lang.String indent​(int index)
        This is used to acquire the indent at the specified index. If the indent does not exist at the specified index then on is created using the current value of the indent. The very first indent taken from this will be an empty string value.
        Parameters:
        index - this is the index to acquire the indent from
        Returns:
        this returns the indent from the specified index
      • create

        private java.lang.String create()
        This is used to create an indent which can later be pushed on to the stack. If the number of spaces to be added is zero then this will return a single character string with a line feed.
        Returns:
        this will create an indent to be added to the stack