Package kiyut.sketsa.io

## Class DOMPrettyPrinter

- java.lang.Object
- - kiyut.sketsa.io.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 Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected boolean` | `canIndent` | Whether the next markup can be indented.
    `protected int` | `column` | The current column.
    `protected int` | `documentWidth` | The document width.
    `protected boolean` | `format` | Whether the output must be formatted.
    `protected int` | `level` | The indentation level.
    `protected int` | `line` | The current line.
    `protected StringBuilder` | `margin` | The margin.
    `protected String` | `newline` | The newline characters.
    `protected NodeRange` | `nodeRange` |
    `protected String` | `publicId` | The public id.
    `protected long` | `size` |
    `protected String` | `systemId` | The system id.
    `protected int` | `tabulationWidth` | The tabulation width.
    `protected int` | `type` | The type of the current lexical unit.
    `protected Writer` | `writer` | The writer used to output the document.
    `protected String` | `xmlDeclaration` | The XML declaration.

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `DOMPrettyPrinter()` | Creates a new instance of DOMPrettyPrinter

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected String` | `contentToString​(String s)` | Returns the given content value transformed to replace invalid characters with entities.
    `protected void` | `formatText​(String text)` | Write the String into allowable document width and margin.
    `protected void` | `formatText​(String text, boolean reformat)` | Write the String.
    `int` | `getDocumentWidth()` | Returns the document width.
    `boolean` | `getFormat()` | Returns whether the output must be formatted.
    `String` | `getNewline()` | Returns the newline characters.
    `int` | `getTabulationWidth()` | Returns the tabulation width.
    `protected boolean` | `isAttributeValueLineBreakable​(Attr attr)` | Return wheter the specified Attr attributeValue is line breakable on word boundary
    `protected boolean` | `isWhiteSpace​(char[] text)` | Tells whether the given character represents white spaces.
    `protected void` | `marginAppend()` |
    `protected void` | `marginDelete()` |
    `protected int` | `newlines​(char[] text)` | Returns the number of newlines in the given char array.
    `protected String` | `normalizeAttributeValue​(String s)` | Return the normalizeAttributeValue It replace multiple space into single space and cariage return/linefeed into single space.
    `void` | `print​(Document doc, Writer w)` | Prints an SVG document to the given writer.
    `protected void` | `printCharacter​(char c)` | Prints a single character.
    `protected void` | `printCharacters​(char[] ca)` | Prints a char array.
    `protected void` | `printDocType()` |
    `protected void` | `printNewline()` | Prints a newline.
    `protected void` | `printSpaces​(char[] text, boolean opt)` | Prints white spaces.
    `protected void` | `printString​(String s)` | Prints a string.
    `protected void` | `printTopSpaces​(char[] text)` | Prints top level white spaces.
    `protected void` | `printXMLDecl()` |
    `void` | `setDocumentWidth​(int i)` | Sets the document width.
    `void` | `setFormat​(boolean b)` | Sets whether the output must be formatted.
    `void` | `setNewline​(String s)` | Sets the newline characters.
    `void` | `setNodeRange​(NodeRange nodeRange)` |
    `void` | `setPublicId​(String s)` | Sets the public ID.
    `void` | `setSystemId​(String s)` | Sets the system ID.
    `void` | `setTabulationWidth​(int i)` | Sets the tabulation width.
    `void` | `setXMLDeclaration​(String s)` | Sets the XML declaration text.
    `protected void` | `writeCDATASection​(Node n)` | write CDATASection node
    `protected void` | `writeCharacterData​(Node n)` | write CharacterData node
    `protected void` | `writeComment​(Node n)` | write comment node
    `protected void` | `writeElement​(Node n)` | write Element node
    `protected void` | `writeEntityReference​(Node n)` | write EntityReference node
    `protected void` | `writeNode​(Node n)` | Writes a node using the given writer.
    `void` | `writePI​(Node n)` | write ProcessingInstruction node

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 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.

    - #### margin

          protected StringBuilder margin

      The margin.

    - #### 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.

    - #### nodeRange

          protected NodeRange nodeRange

  - ### 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