sidekick.html.parser.html
Class HtmlFormatter

java.lang.Object
  extended by sidekick.html.parser.html.HtmlVisitor
      extended by sidekick.html.parser.html.HtmlFormatter

public class HtmlFormatter
extends HtmlVisitor

HtmlFormatter is a Visitor which traverses an HtmlDocument, dumping the contents of the document to a specified output stream. It assumes that the documents has been preprocessed by HtmlCollector (which matches up beginning and end tags) and by HtmlScrubber (which formats tags in a consistent way). In particular, HtmlScrubber should be invoked with the TRIM_SPACES option to remove trailing spaces, which can confuse the formatting algorithm.

The right margin and indent increment can be specified as properties.

danson: Modified for Beauty plugin for jEdit, added ability to handle jsps. Removed the PrintWriter from the MarginWriter as PrintWriter munges line separators in its own weird way. For jEdit, I want the same line separator that has been specified for the current buffer, which is not necessarily the system line separator. Formatted content is now written to a StringBuffer and can be retrieved with the toString method.

Did some minor modification to the handling of PRE, SCRIPT, and STYLE blocks. Formatting once would be fine, formatting the same file a second time would cause extra blank lines to be added just before the closing tag. This also required some minor modification to the .jj file too.

Author:
Brian Goetz, Quiotix
See Also:
HtmlVisitor, HtmlCollector, HtmlScrubber

Field Summary
protected  sidekick.html.parser.html.TagBlockRenderer blockRenderer
           
protected  int indentSize
           
protected  boolean inPreBlock
           
protected  java.lang.String lineSeparator
           
protected  sidekick.html.parser.html.MarginWriter out
           
protected  HtmlDocument.HtmlElement previousElement
           
protected  int rightMargin
           
protected static java.util.Set<java.lang.String> tagsIndentBlock
           
protected static java.util.Set<java.lang.String> tagsNewlineBefore
           
protected static java.util.Set<java.lang.String> tagsPreformatted
           
protected static java.util.Set<java.lang.String> tagsTryMatch
           
 
Constructor Summary
HtmlFormatter()
           
 
Method Summary
 void finish()
           
 void setIndent(int indent)
           
 void setLineSeparator(java.lang.String ls)
           
 void setRightMargin(int margin)
           
 void start()
           
 java.lang.String toString()
           
 void visit(HtmlDocument.Comment c)
           
 void visit(HtmlDocument.EndTag t)
           
 void visit(HtmlDocument.Newline n)
           
 void visit(HtmlDocument.Tag t)
           
 void visit(HtmlDocument.TagBlock block)
           
 void visit(HtmlDocument.Text t)
           
 
Methods inherited from class sidekick.html.parser.html.HtmlVisitor
visit, visit, visit, visit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

out

protected sidekick.html.parser.html.MarginWriter out

rightMargin

protected int rightMargin

indentSize

protected int indentSize

lineSeparator

protected java.lang.String lineSeparator

tagsIndentBlock

protected static java.util.Set<java.lang.String> tagsIndentBlock

tagsNewlineBefore

protected static java.util.Set<java.lang.String> tagsNewlineBefore

tagsPreformatted

protected static java.util.Set<java.lang.String> tagsPreformatted

tagsTryMatch

protected static java.util.Set<java.lang.String> tagsTryMatch

blockRenderer

protected sidekick.html.parser.html.TagBlockRenderer blockRenderer

previousElement

protected HtmlDocument.HtmlElement previousElement

inPreBlock

protected boolean inPreBlock
Constructor Detail

HtmlFormatter

public HtmlFormatter()
              throws java.lang.Exception
Throws:
java.lang.Exception
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setRightMargin

public void setRightMargin(int margin)

setIndent

public void setIndent(int indent)

setLineSeparator

public void setLineSeparator(java.lang.String ls)

visit

public void visit(HtmlDocument.TagBlock block)
Overrides:
visit in class HtmlVisitor

visit

public void visit(HtmlDocument.Tag t)
Overrides:
visit in class HtmlVisitor

visit

public void visit(HtmlDocument.EndTag t)
Overrides:
visit in class HtmlVisitor

visit

public void visit(HtmlDocument.Comment c)
Overrides:
visit in class HtmlVisitor

visit

public void visit(HtmlDocument.Text t)
Overrides:
visit in class HtmlVisitor

visit

public void visit(HtmlDocument.Newline n)
Overrides:
visit in class HtmlVisitor

start

public void start()
Overrides:
start in class HtmlVisitor

finish

public void finish()
Overrides:
finish in class HtmlVisitor