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 booleancanIndentWhether the next markup can be indented.protected intcolumnThe current column.protected intdocumentWidthThe document width.protected booleanformatWhether the output must be formatted.protected intlevelThe indentation level.protected intlineThe current line.protected StringBuildermarginThe margin.protected StringnewlineThe newline characters.protected NodeRangenodeRangeprotected StringpublicIdThe public id.protected longsizeprotected StringsystemIdThe system id.protected inttabulationWidthThe tabulation width.protected inttypeThe type of the current lexical unit.protected WriterwriterThe writer used to output the document.protected StringxmlDeclarationThe 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 StringcontentToString(String s)Returns the given content value transformed to replace invalid characters with entities.protected voidformatText(String text)Write the String into allowable document width and margin.protected voidformatText(String text, boolean reformat)Write the String.intgetDocumentWidth()Returns the document width.booleangetFormat()Returns whether the output must be formatted.StringgetNewline()Returns the newline characters.intgetTabulationWidth()Returns the tabulation width.protected booleanisAttributeValueLineBreakable(Attr attr)Return wheter the specified Attr attributeValue is line breakable on word boundaryprotected booleanisWhiteSpace(char[] text)Tells whether the given character represents white spaces.protected voidmarginAppend()protected voidmarginDelete()protected intnewlines(char[] text)Returns the number of newlines in the given char array.protected StringnormalizeAttributeValue(String s)Return the normalizeAttributeValue It replace multiple space into single space and cariage return/linefeed into single space.voidprint(Document doc, Writer w)Prints an SVG document to the given writer.protected voidprintCharacter(char c)Prints a single character.protected voidprintCharacters(char[] ca)Prints a char array.protected voidprintDocType()protected voidprintNewline()Prints a newline.protected voidprintSpaces(char[] text, boolean opt)Prints white spaces.protected voidprintString(String s)Prints a string.protected voidprintTopSpaces(char[] text)Prints top level white spaces.protected voidprintXMLDecl()voidsetDocumentWidth(int i)Sets the document width.voidsetFormat(boolean b)Sets whether the output must be formatted.voidsetNewline(String s)Sets the newline characters.voidsetNodeRange(NodeRange nodeRange)voidsetPublicId(String s)Sets the public ID.voidsetSystemId(String s)Sets the system ID.voidsetTabulationWidth(int i)Sets the tabulation width.voidsetXMLDeclaration(String s)Sets the XML declaration text.protected voidwriteCDATASection(Node n)write CDATASection nodeprotected voidwriteCharacterData(Node n)write CharacterData nodeprotected voidwriteComment(Node n)write comment nodeprotected voidwriteElement(Node n)write Element nodeprotected voidwriteEntityReference(Node n)write EntityReference nodeprotected voidwriteNode(Node n)Writes a node using the given writer.voidwritePI(Node n)write ProcessingInstruction node
-
-
-
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
-
-
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 IOExceptionPrints a single character.- Parameters:
c- Character- Throws:
IOException- if failed
-
printNewline
protected void printNewline() throws IOExceptionPrints 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 IOExceptionPrints a char array.- Parameters:
ca- char array- Throws:
IOException- If an I/O error occurs
-
printSpaces
protected void printSpaces(char[] text, boolean opt) throws IOExceptionPrints 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 IOExceptionPrints 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 Stringreformat- 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 Documentw- the Writer- Throws:
org.apache.batik.transcoder.TranscoderException- if transcoding failedIOException- If an I/O error occurs
-
printXMLDecl
protected void printXMLDecl() throws org.apache.batik.transcoder.TranscoderException, IOException- Throws:
org.apache.batik.transcoder.TranscoderExceptionIOException
-
printDocType
protected void printDocType() throws org.apache.batik.transcoder.TranscoderException, IOException- Throws:
org.apache.batik.transcoder.TranscoderExceptionIOException
-
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
-
-