Class XMLElement

  • Direct Known Subclasses:
    HTMLElement, ReportElement

    public class XMLElement
    extends java.lang.Object
    Simple API to create well formed XML streams with minimal memory overhead. A XMLElement instance represents a single element in a XML document. XMLElement can be used directly or might be subclassed for schema specific convenience methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closed  
      private static java.lang.String DOCTYPE
      DOCTYPE declaration template
      private static java.lang.String HEADER
      XML header template
      private static java.lang.String HEADER_STANDALONE
      XML header template for standalone documents
      private XMLElement lastchild  
      private java.lang.String name  
      private boolean openTagDone  
      private boolean root  
      protected java.io.Writer writer
      Writer for content output
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private XMLElement​(java.io.Writer writer, java.lang.String name, boolean root)  
        XMLElement​(java.lang.String name, java.lang.String pubId, java.lang.String system, boolean standalone, java.lang.String encoding, java.io.OutputStream output)
      Creates a root element of a XML document.
      protected XMLElement​(java.lang.String name, XMLElement parent)
      Creates a new child element within a XML document.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addChildElement​(XMLElement child)  
      void attr​(java.lang.String name, int value)
      Adds an attribute to this element.
      void attr​(java.lang.String name, long value)
      Adds an attribute to this element.
      void attr​(java.lang.String name, java.lang.String value)
      Adds an attribute to this element.
      void close()
      Closes this element if it has not been closed before.
      XMLElement element​(java.lang.String name)
      Creates a new child element for this element.
      private void finishOpenTag()  
      private void quote​(java.lang.String text)  
      void text​(java.lang.String text)
      Adds the given text as a child to this node.
      • Methods inherited from class java.lang.Object

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

      • HEADER

        private static final java.lang.String HEADER
        XML header template
        See Also:
        Constant Field Values
      • HEADER_STANDALONE

        private static final java.lang.String HEADER_STANDALONE
        XML header template for standalone documents
        See Also:
        Constant Field Values
      • DOCTYPE

        private static final java.lang.String DOCTYPE
        DOCTYPE declaration template
        See Also:
        Constant Field Values
      • writer

        protected final java.io.Writer writer
        Writer for content output
      • name

        private final java.lang.String name
      • openTagDone

        private boolean openTagDone
      • closed

        private boolean closed
      • root

        private final boolean root
    • Constructor Detail

      • XMLElement

        private XMLElement​(java.io.Writer writer,
                           java.lang.String name,
                           boolean root)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • XMLElement

        public XMLElement​(java.lang.String name,
                          java.lang.String pubId,
                          java.lang.String system,
                          boolean standalone,
                          java.lang.String encoding,
                          java.io.OutputStream output)
                   throws java.io.IOException
        Creates a root element of a XML document.
        Parameters:
        name - element name
        pubId - optional schema public identifier
        system - optional schema system identifier
        standalone - if true the document is declared as standalone
        encoding - character encoding used for output
        output - output stream will be closed if the root element is closed
        Throws:
        java.io.IOException - in case of problems with the underlying output
      • XMLElement

        protected XMLElement​(java.lang.String name,
                             XMLElement parent)
                      throws java.io.IOException
        Creates a new child element within a XML document. May only be called before the parent element has been closed.
        Parameters:
        name - element name
        parent - parent of this element
        Throws:
        java.io.IOException - in case of problems with the underlying output or if the parent element is already closed
    • Method Detail

      • addChildElement

        private void addChildElement​(XMLElement child)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • finishOpenTag

        private void finishOpenTag()
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • quote

        private void quote​(java.lang.String text)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • attr

        public final void attr​(java.lang.String name,
                               java.lang.String value)
                        throws java.io.IOException
        Adds an attribute to this element. May only be called before an child element is added or this element has been closed. The attribute value will be quoted. If the value is null the attribute will not be added.
        Parameters:
        name - attribute name
        value - attribute value or null
        Throws:
        java.io.IOException - in case of problems with the underlying output or if the element is already closed.
      • attr

        public final void attr​(java.lang.String name,
                               int value)
                        throws java.io.IOException
        Adds an attribute to this element. May only be called before an child element is added or this element has been closed. The attribute value is the decimal representation of the given int value.
        Parameters:
        name - attribute name
        value - attribute value
        Throws:
        java.io.IOException - in case of problems with the underlying output or if the element is already closed.
      • attr

        public final void attr​(java.lang.String name,
                               long value)
                        throws java.io.IOException
        Adds an attribute to this element. May only be called before an child element is added or this element has been closed. The attribute value is the decimal representation of the given long value.
        Parameters:
        name - attribute name
        value - attribute value
        Throws:
        java.io.IOException - in case of problems with the underlying output or if the element is already closed.
      • text

        public final void text​(java.lang.String text)
                        throws java.io.IOException
        Adds the given text as a child to this node. The text will be quoted. May only be called before this element has been closed.
        Parameters:
        text - text to add
        Throws:
        java.io.IOException - in case of problems with the underlying output or if the element is already closed.
      • element

        public XMLElement element​(java.lang.String name)
                           throws java.io.IOException
        Creates a new child element for this element. Might be overridden in subclasses to return a instance of the subclass.
        Parameters:
        name - name of the child element
        Returns:
        child element instance
        Throws:
        java.io.IOException - in case of problems with the underlying output
      • close

        public final void close()
                         throws java.io.IOException
        Closes this element if it has not been closed before.
        Throws:
        java.io.IOException - in case of problems with the underlying output