Class DOMPrettyPrinter


  • public class DOMPrettyPrinter
    extends Object
    This class represents an SVGDOM to source files pretty-printer. Most of the code here is adapted from batik svg2svg Transcoder and DOMUtilities.
    Author:
    Kiyut
    • Field Detail

      • writer

        protected Writer writer
        The writer used to output the document.
      • newline

        protected String newline
        The newline characters.
      • format

        protected boolean format
        Whether the output must be formatted.
      • tabulationWidth

        protected int tabulationWidth
        The tabulation width.
      • documentWidth

        protected int documentWidth
        The document width.
      • publicId

        protected String publicId
        The public id.
      • systemId

        protected String systemId
        The system id.
      • xmlDeclaration

        protected String xmlDeclaration
        The XML declaration.
      • type

        protected int type
        The type of the current lexical unit.
      • level

        protected int level
        The indentation level.
      • line

        protected int line
        The current line.
      • column

        protected int column
        The current column.
      • size

        protected long size
      • canIndent

        protected boolean canIndent
        Whether the next markup can be indented.
    • Constructor Detail

      • DOMPrettyPrinter

        public DOMPrettyPrinter()
        Creates a new instance of DOMPrettyPrinter
    • Method Detail

      • setNodeRange

        public void setNodeRange​(NodeRange nodeRange)
      • setXMLDeclaration

        public void setXMLDeclaration​(String s)
        Sets the XML declaration text.
        Parameters:
        s - the XMLDeclaration
      • setPublicId

        public void setPublicId​(String s)
        Sets the public ID.
        Parameters:
        s - the PublicId
      • setSystemId

        public void setSystemId​(String s)
        Sets the system ID.
        Parameters:
        s - the SystemId
      • setNewline

        public void setNewline​(String s)
        Sets the newline characters.
        Parameters:
        s - the newline characters
      • getNewline

        public String getNewline()
        Returns the newline characters.
        Returns:
        the newline characters.
      • setFormat

        public void setFormat​(boolean b)
        Sets whether the output must be formatted.
        Parameters:
        b - true or false
      • getFormat

        public boolean getFormat()
        Returns whether the output must be formatted.
        Returns:
        whether the output must be formatted.
      • setTabulationWidth

        public void setTabulationWidth​(int i)
        Sets the tabulation width.
        Parameters:
        i - tabulation width.
      • getTabulationWidth

        public int getTabulationWidth()
        Returns the tabulation width.
        Returns:
        the tabulation width.
      • setDocumentWidth

        public void setDocumentWidth​(int i)
        Sets the document width.
        Parameters:
        i - the document width
      • getDocumentWidth

        public int getDocumentWidth()
        Returns the document width.
        Returns:
        the document width
      • newlines

        protected int newlines​(char[] text)
        Returns the number of newlines in the given char array.
        Parameters:
        text - char array
        Returns:
        the number of newlines in the given char array
      • printCharacter

        protected void printCharacter​(char c)
                               throws IOException
        Prints a single character.
        Parameters:
        c - Character
        Throws:
        IOException - if failed
      • printNewline

        protected void printNewline()
                             throws IOException
        Prints a newline.
        Throws:
        IOException - If an I/O error occurs
      • printString

        protected void printString​(String s)
                            throws IOException
        Prints a string.
        Parameters:
        s - the String
        Throws:
        IOException - If an I/O error occurs
      • printCharacters

        protected void printCharacters​(char[] ca)
                                throws IOException
        Prints a char array.
        Parameters:
        ca - char array
        Throws:
        IOException - If an I/O error occurs
      • printSpaces

        protected void printSpaces​(char[] text,
                                   boolean opt)
                            throws IOException
        Prints white spaces.
        Parameters:
        text - The space text.
        opt - whether the space is optional.
        Throws:
        IOException - If an I/O error occurs
      • printTopSpaces

        protected void printTopSpaces​(char[] text)
                               throws IOException
        Prints top level white spaces.
        Parameters:
        text - The space text.
        Throws:
        IOException - If an I/O error occurs
      • marginAppend

        protected void marginAppend()
      • marginDelete

        protected void marginDelete()
      • contentToString

        protected String contentToString​(String s)
        Returns the given content value transformed to replace invalid characters with entities.
        Parameters:
        s - String to be formated
        Returns:
        formated String
      • normalizeAttributeValue

        protected String normalizeAttributeValue​(String s)
        Return the normalizeAttributeValue It replace multiple space into single space and cariage return/linefeed into single space. It also replace occurance of quote(") into single quote (')
        Parameters:
        s - the attribute value
        Returns:
        the normalizeAttributeValue
      • formatText

        protected void formatText​(String text)
                           throws IOException
        Write the String into allowable document width and margin. The line break is done on word boundary
        Parameters:
        text - the String
        Throws:
        IOException - If an I/O error occurs
      • formatText

        protected void formatText​(String text,
                                  boolean reformat)
                           throws IOException
        Write the String. The line break is done on word boundary
        Parameters:
        text - the String
        reformat - true or false for allowable document width and margin.
        Throws:
        IOException - If an I/O error occurs
      • isWhiteSpace

        protected boolean isWhiteSpace​(char[] text)
        Tells whether the given character represents white spaces.
        Parameters:
        text - the char array
        Returns:
        true if whitespace otherwise false
      • isAttributeValueLineBreakable

        protected boolean isAttributeValueLineBreakable​(Attr attr)
        Return wheter the specified Attr attributeValue is line breakable on word boundary
        Parameters:
        attr - The attribute
        Returns:
        true if line breakable otherwise false
      • print

        public void print​(Document doc,
                          Writer w)
                   throws org.apache.batik.transcoder.TranscoderException,
                          IOException
        Prints an SVG document to the given writer.
        Parameters:
        doc - the SVG Document
        w - the Writer
        Throws:
        org.apache.batik.transcoder.TranscoderException - if transcoding failed
        IOException - If an I/O error occurs
      • printXMLDecl

        protected void printXMLDecl()
                             throws org.apache.batik.transcoder.TranscoderException,
                                    IOException
        Throws:
        org.apache.batik.transcoder.TranscoderException
        IOException
      • printDocType

        protected void printDocType()
                             throws org.apache.batik.transcoder.TranscoderException,
                                    IOException
        Throws:
        org.apache.batik.transcoder.TranscoderException
        IOException
      • writeNode

        protected void writeNode​(Node n)
                          throws IOException
        Writes a node using the given writer.
        Parameters:
        n - THe Node
        Throws:
        IOException - If an I/O error occurs
      • writeElement

        protected void writeElement​(Node n)
                             throws IOException
        write Element node
        Parameters:
        n - the Node
        Throws:
        IOException - If an I/O error occurs
      • writeComment

        protected void writeComment​(Node n)
                             throws IOException
        write comment node
        Parameters:
        n - The Node
        Throws:
        IOException - If an I/O error occurs
      • writeCharacterData

        protected void writeCharacterData​(Node n)
                                   throws IOException
        write CharacterData node
        Parameters:
        n - the Node
        Throws:
        IOException - If an I/O error occurs
      • writeEntityReference

        protected void writeEntityReference​(Node n)
                                     throws IOException
        write EntityReference node
        Parameters:
        n - the Node
        Throws:
        IOException - If an I/O error occurs
      • writeCDATASection

        protected void writeCDATASection​(Node n)
                                  throws IOException
        write CDATASection node
        Parameters:
        n - the Node
        Throws:
        IOException - if failed
      • writePI

        public void writePI​(Node n)
                     throws IOException
        write ProcessingInstruction node
        Parameters:
        n - the Node
        Throws:
        IOException - If an I/O error occurs