Class DocWriter

  • All Implemented Interfaces:
    DocListener, ElementListener, java.util.EventListener
    Direct Known Subclasses:
    PdfWriter

    public abstract class DocWriter
    extends java.lang.Object
    implements DocListener
    An abstract Writer class for documents.

    DocWriter is the abstract class of several writers such as PdfWriter and HtmlWriter. A DocWriter can be added as a DocListener to a certain Document by getting an instance (see method getInstance() in the specific writer-classes). Every Element added to the original Document will be written to the OutputStream of the listening DocWriter.

    See Also:
    Document, DocListener
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closeStream
      Closes the stream on document close
      private static byte EQUALS
      This is some byte that is often used.
      private static byte FORWARD
      This is some byte that is often used.
      private static byte GT
      This is some byte that is often used.
      protected boolean open
      Is the writer open for writing?
      protected OutputStreamCounter os
      The outputstream of this writer.
      private boolean pause
      Do we have to pause all writing actions?
      private static byte QUOTE
      This is some byte that is often used.
      private static byte SPACE
      This is some byte that is often used.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected DocWriter()  
      protected DocWriter​(Document document, java.io.OutputStream os)
      Constructs a DocWriter.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(Element element)
      Signals that an Element was added to the Document.
      void close()
      Signals that the Document was closed and that no other Elements will be added.
      static byte[] getISOBytes​(java.lang.String text)
      Converts a String into a Byte array according to the ISO-8859-1 codepage.
      boolean isCloseStream()
      Checks if the stream is to be closed on document close
      boolean isPaused()
      Checks if writing is paused.
      boolean newPage()
      Signals that an new page has to be started.
      void open()
      Signals that the Document was opened.
      void resetFooter()
      Resets the footer of this document.
      void resetHeader()
      Resets the header of this document.
      void resetPageCount()
      Sets the page number to 0.
      void setCloseStream​(boolean closeStream)
      Sets the close state of the stream after document close
      void setFooter​(HeaderFooter footer)
      Changes the footer of this document.
      void setHeader​(HeaderFooter header)
      Changes the header of this document.
      boolean setMarginMirroring​(boolean MarginMirroring)
      Parameter that allows you to do left/right margin mirroring (odd/even pages)
      boolean setMarginMirroringTopBottom​(boolean MarginMirroring)
      Parameter that allows you to do top/bottom margin mirroring (odd/even pages)
      boolean setMargins​(float marginLeft, float marginRight, float marginTop, float marginBottom)
      Sets the margins.
      void setPageCount​(int pageN)
      Sets the page number.
      boolean setPageSize​(Rectangle pageSize)
      Sets the pagesize.
      private void write​(java.lang.String string)
      Writes a String to the OutputStream.
      private void write​(java.lang.String key, java.lang.String value)
      Writes a key-value pair to the outputstream.
      • Methods inherited from class java.lang.Object

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

      • SPACE

        private static final byte SPACE
        This is some byte that is often used.
        See Also:
        Constant Field Values
      • EQUALS

        private static final byte EQUALS
        This is some byte that is often used.
        See Also:
        Constant Field Values
      • QUOTE

        private static final byte QUOTE
        This is some byte that is often used.
        See Also:
        Constant Field Values
      • FORWARD

        private static final byte FORWARD
        This is some byte that is often used.
        See Also:
        Constant Field Values
      • open

        protected boolean open
        Is the writer open for writing?
      • pause

        private final boolean pause
        Do we have to pause all writing actions?
        See Also:
        Constant Field Values
      • closeStream

        private boolean closeStream
        Closes the stream on document close
    • Constructor Detail

      • DocWriter

        protected DocWriter()
      • DocWriter

        protected DocWriter​(Document document,
                            java.io.OutputStream os)
        Constructs a DocWriter.
        Parameters:
        document - The Document that has to be written
        os - The OutputStream the writer has to write to.
    • Method Detail

      • add

        public boolean add​(Element element)
                    throws DocumentException
        Signals that an Element was added to the Document.

        This method should be overridden in the specific DocWriter classes derived from this abstract class.

        Specified by:
        add in interface ElementListener
        Parameters:
        element - A high level object to add
        Returns:
        false
        Throws:
        DocumentException - when a document isn't open yet, or has been closed
      • open

        public void open()
        Signals that the Document was opened.
        Specified by:
        open in interface DocListener
      • setPageSize

        public boolean setPageSize​(Rectangle pageSize)
        Sets the pagesize.
        Specified by:
        setPageSize in interface DocListener
        Parameters:
        pageSize - the new pagesize
        Returns:
        a boolean
      • setMargins

        public boolean setMargins​(float marginLeft,
                                  float marginRight,
                                  float marginTop,
                                  float marginBottom)
        Sets the margins.

        This does nothing. Has to be overridden if needed.

        Specified by:
        setMargins in interface DocListener
        Parameters:
        marginLeft - the margin on the left
        marginRight - the margin on the right
        marginTop - the margin on the top
        marginBottom - the margin on the bottom
        Returns:
        false
      • newPage

        public boolean newPage()
        Signals that an new page has to be started.

        This does nothing. Has to be overridden if needed.

        Specified by:
        newPage in interface DocListener
        Returns:
        true if the page was added, false if not.
      • setHeader

        public void setHeader​(HeaderFooter header)
        Changes the header of this document.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of headers.

        Specified by:
        setHeader in interface DocListener
        Parameters:
        header - the new header
      • resetHeader

        public void resetHeader()
        Resets the header of this document.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of headers.

        Specified by:
        resetHeader in interface DocListener
      • setFooter

        public void setFooter​(HeaderFooter footer)
        Changes the footer of this document.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of footers.

        Specified by:
        setFooter in interface DocListener
        Parameters:
        footer - the new footer
      • resetFooter

        public void resetFooter()
        Resets the footer of this document.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of footers.

        Specified by:
        resetFooter in interface DocListener
      • resetPageCount

        public void resetPageCount()
        Sets the page number to 0.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of pagenumbers.

        Specified by:
        resetPageCount in interface DocListener
      • setPageCount

        public void setPageCount​(int pageN)
        Sets the page number.

        This method should be overridden in the specific DocWriter classes derived from this abstract class if they actually support the use of pagenumbers.

        Specified by:
        setPageCount in interface DocListener
        Parameters:
        pageN - the new page number
      • close

        public void close()
        Signals that the Document was closed and that no other Elements will be added.
        Specified by:
        close in interface DocListener
      • getISOBytes

        public static final byte[] getISOBytes​(java.lang.String text)
        Converts a String into a Byte array according to the ISO-8859-1 codepage.
        Parameters:
        text - the text to be converted
        Returns:
        the conversion result
      • isPaused

        public boolean isPaused()
        Checks if writing is paused.
        Returns:
        true if writing temporarily has to be paused, false otherwise.
      • write

        private void write​(java.lang.String string)
                    throws java.io.IOException
        Writes a String to the OutputStream.
        Parameters:
        string - the String to write
        Throws:
        java.io.IOException - on error
      • write

        private void write​(java.lang.String key,
                           java.lang.String value)
                    throws java.io.IOException
        Writes a key-value pair to the outputstream.
        Parameters:
        key - the name of an attribute
        value - the value of an attribute
        Throws:
        java.io.IOException - on error
      • isCloseStream

        public boolean isCloseStream()
        Checks if the stream is to be closed on document close
        Returns:
        true if the stream is closed on document close
      • setCloseStream

        public void setCloseStream​(boolean closeStream)
        Sets the close state of the stream after document close
        Parameters:
        closeStream - true if the stream is closed on document close