javax.swing.text.html
Class HTMLWriter

java.lang.Object
  extended byjavax.swing.text.AbstractWriter
      extended byjavax.swing.text.html.HTMLWriter

public class HTMLWriter
extends AbstractWriter

This is a writer for HTMLDocuments.

Author:
Sunita Mani

Field Summary
private  Stack blockElementStack
           
private  boolean completeDoc
           
private  MutableAttributeSet convAttr
          Buffer for the purpose of attribute conversion
private  boolean inContent
           
private  boolean indentNext
          Determines whether a the indentation needs to be incremented.
private  boolean inPre
           
private  boolean inTextArea
           
private  boolean newlineOutputed
           
private  MutableAttributeSet oConvAttr
          Buffer for the purpose of attribute conversion.
private  int preEndOffset
          When inPre is true, this will indicate the end offset of the pre element.
private  boolean replaceEntities
          Set to true when entities (such as <) should be replaced.
private  Segment segment
          Used when writing out content.
private  Vector tags
           
private  Vector tagsToRemove
           
private  Vector tagValues
          Values for the tags.
private  char[] tempChars
          Temporary buffer.
private  boolean writeCSS
          If true, the writer will emit CSS attributes in preference to HTML tags/attributes (i.e.
private  boolean wroteHead
          Set to true after the head has been output.
 
Fields inherited from class javax.swing.text.AbstractWriter
NEWLINE
 
Constructor Summary
HTMLWriter(Writer w, HTMLDocument doc)
          Creates a new HTMLWriter.
HTMLWriter(Writer w, HTMLDocument doc, int pos, int len)
          Creates a new HTMLWriter.
 
Method Summary
protected  void closeOutUnwantedEmbeddedTags(AttributeSet attr)
          Searches the attribute set and for each tag that is stored in the tag vector.
protected  void comment(Element elem)
          Writes out comments.
(package private)  AttributeSet convertToHTML(AttributeSet from, MutableAttributeSet to)
          Convert the give set of attributes to be html for the purpose of writing them out.
private static void convertToHTML32(AttributeSet from, MutableAttributeSet to)
          Create an older style of HTML attributes.
private static void convertToHTML40(AttributeSet from, MutableAttributeSet to)
          Copies the given AttributeSet to a new set, converting any CSS attributes found to arguments of an HTML style attribute.
private static void createFontAttribute(CSS.Attribute a, AttributeSet from, MutableAttributeSet to)
          Create/update an HTML <font> tag attribute.
protected  void emptyTag(Element elem)
          Writes out all empty elements (all tags that have no corresponding end tag).
protected  void endTag(Element elem)
          Writes out an end tag for the element.
private  boolean indentNeedsIncrementing(Element current, Element next)
           
protected  boolean isBlockTag(AttributeSet attr)
          Determines if the HTML.Tag associated with the element is a block tag.
private  boolean isFormElementWithContent(AttributeSet attr)
          Determines if the element associated with the attributeset is a TEXTAREA or SELECT.
protected  boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag)
          Returns true if the StyleConstants.NameAttribute is equal to the tag that is passed in as a parameter.
private  boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t, Object tagValue)
          Searches the attribute set for a tag, both of which are passed in as a parameter.
protected  void output(char[] chars, int start, int length)
          This method is overriden to map any character entities, such as < to &lt;.
private  void output(String string)
          This directly invokes super's output after converting string to a char[].
protected  void selectContent(AttributeSet attr)
          Writes out the content of the SELECT form element.
protected  void startTag(Element elem)
          Writes out a start tag for the element.
protected  boolean synthesizedElement(Element elem)
          Returns true if the element is a synthesized element.
protected  void text(Element elem)
          Writes out text.
protected  void textAreaContent(AttributeSet attr)
          Writes out text that is contained in a TEXTAREA form element.
 void write()
          Iterates over the Element tree and controls the writing out of all the tags and its attributes.
(package private)  void writeAdditionalComments()
          Writes out any additional comments (comments outside of the body) stored under the property HTMLDocument.AdditionalComments.
protected  void writeAttributes(AttributeSet attr)
          Writes out the attribute set.
(package private)  void writeComment(String string)
          Writes out comment string.
protected  void writeEmbeddedTags(AttributeSet attr)
          Searches for embedded tags in the AttributeSet and writes them out.
protected  void writeLineSeparator()
          Writes the line separator.
(package private)  void writeMaps(Enumeration maps)
          Outputs the maps as elements.
protected  void writeOption(Option option)
          Writes out the content of the Option form element.
(package private)  boolean writeStyle(String name, Style style, boolean outputStyle)
          Outputs the named style.
(package private)  void writeStyleEndTag()
           
(package private)  void writeStyles(StyleSheet sheet)
          Outputs the styles as a single element.
(package private)  void writeStyleStartTag()
           
 
Methods inherited from class javax.swing.text.AbstractWriter
decrIndent, getCanWrapLines, getCurrentLineLength, getDocument, getElementIterator, getEndOffset, getIndentLevel, getIndentSpace, getLineLength, getLineSeparator, getStartOffset, getText, getWriter, incrIndent, indent, inRange, isLineEmpty, setCanWrapLines, setCurrentLineLength, setIndentSpace, setLineLength, setLineSeparator, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blockElementStack

private Stack blockElementStack

inContent

private boolean inContent

inPre

private boolean inPre

preEndOffset

private int preEndOffset
When inPre is true, this will indicate the end offset of the pre element.


inTextArea

private boolean inTextArea

newlineOutputed

private boolean newlineOutputed

completeDoc

private boolean completeDoc

tags

private Vector tags

tagValues

private Vector tagValues
Values for the tags.


segment

private Segment segment
Used when writing out content.


tagsToRemove

private Vector tagsToRemove

wroteHead

private boolean wroteHead
Set to true after the head has been output.


replaceEntities

private boolean replaceEntities
Set to true when entities (such as <) should be replaced.


tempChars

private char[] tempChars
Temporary buffer.


indentNext

private boolean indentNext
Determines whether a the indentation needs to be incremented. Basically, if next is a child of current, and next is NOT a synthesized element, the indent level will be incremented. If there is a parent-child relationship and "next" is a synthesized element, then its children must be indented. This state is maintained by the indentNext boolean.


writeCSS

private boolean writeCSS
If true, the writer will emit CSS attributes in preference to HTML tags/attributes (i.e. It will emit an HTML 4.0 style).


convAttr

private MutableAttributeSet convAttr
Buffer for the purpose of attribute conversion


oConvAttr

private MutableAttributeSet oConvAttr
Buffer for the purpose of attribute conversion. This can be used if convAttr is being used.

Constructor Detail

HTMLWriter

public HTMLWriter(Writer w,
                  HTMLDocument doc)
Creates a new HTMLWriter.

Parameters:
w - a Writer
doc - an HTMLDocument

HTMLWriter

public HTMLWriter(Writer w,
                  HTMLDocument doc,
                  int pos,
                  int len)
Creates a new HTMLWriter.

Parameters:
w - a Writer
doc - an HTMLDocument
pos - the document location from which to fetch the content
len - the amount to write out
Method Detail

write

public void write()
           throws IOException,
                  BadLocationException
Iterates over the Element tree and controls the writing out of all the tags and its attributes.

Specified by:
write in class AbstractWriter
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

writeAttributes

protected void writeAttributes(AttributeSet attr)
                        throws IOException
Writes out the attribute set. Ignores all attributes with a key of type HTML.Tag, attributes with a key of type StyleConstants, and attributes with a key of type HTML.Attribute.ENDTAG.

Overrides:
writeAttributes in class AbstractWriter
Parameters:
attr - an AttributeSet
Throws:
IOException - on any I/O error

emptyTag

protected void emptyTag(Element elem)
                 throws BadLocationException,
                        IOException
Writes out all empty elements (all tags that have no corresponding end tag).

Parameters:
elem - an Element
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

isBlockTag

protected boolean isBlockTag(AttributeSet attr)
Determines if the HTML.Tag associated with the element is a block tag.

Parameters:
attr - an AttributeSet
Returns:
true if tag is block tag, false otherwise.

startTag

protected void startTag(Element elem)
                 throws IOException,
                        BadLocationException
Writes out a start tag for the element. Ignores all synthesized elements.

Parameters:
elem - an Element
Throws:
IOException - on any I/O error
BadLocationException

textAreaContent

protected void textAreaContent(AttributeSet attr)
                        throws BadLocationException,
                               IOException
Writes out text that is contained in a TEXTAREA form element.

Parameters:
attr - an AttributeSet
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

text

protected void text(Element elem)
             throws BadLocationException,
                    IOException
Writes out text. If a range is specified when the constructor is invoked, then only the appropriate range of text is written out.

Overrides:
text in class AbstractWriter
Parameters:
elem - an Element
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

selectContent

protected void selectContent(AttributeSet attr)
                      throws IOException
Writes out the content of the SELECT form element.

Parameters:
attr - the AttributeSet associated with the form element
Throws:
IOException - on any I/O error

writeOption

protected void writeOption(Option option)
                    throws IOException
Writes out the content of the Option form element.

Parameters:
option - an Option
Throws:
IOException - on any I/O error

endTag

protected void endTag(Element elem)
               throws IOException
Writes out an end tag for the element.

Parameters:
elem - an Element
Throws:
IOException - on any I/O error

comment

protected void comment(Element elem)
                throws BadLocationException,
                       IOException
Writes out comments.

Parameters:
elem - an Element
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

writeComment

void writeComment(String string)
            throws IOException
Writes out comment string.

Parameters:
string - the comment
Throws:
IOException - on any I/O error
BadLocationException - if pos represents an invalid location within the document.

writeAdditionalComments

void writeAdditionalComments()
                       throws IOException
Writes out any additional comments (comments outside of the body) stored under the property HTMLDocument.AdditionalComments.

Throws:
IOException

synthesizedElement

protected boolean synthesizedElement(Element elem)
Returns true if the element is a synthesized element. Currently we are only testing for the p-implied tag.


matchNameAttribute

protected boolean matchNameAttribute(AttributeSet attr,
                                     HTML.Tag tag)
Returns true if the StyleConstants.NameAttribute is equal to the tag that is passed in as a parameter.


writeEmbeddedTags

protected void writeEmbeddedTags(AttributeSet attr)
                          throws IOException
Searches for embedded tags in the AttributeSet and writes them out. It also stores these tags in a vector so that when appropriate the corresponding end tags can be written out.

Throws:
IOException - on any I/O error

noMatchForTagInAttributes

private boolean noMatchForTagInAttributes(AttributeSet attr,
                                          HTML.Tag t,
                                          Object tagValue)
Searches the attribute set for a tag, both of which are passed in as a parameter. Returns true if no match is found and false otherwise.


closeOutUnwantedEmbeddedTags

protected void closeOutUnwantedEmbeddedTags(AttributeSet attr)
                                     throws IOException
Searches the attribute set and for each tag that is stored in the tag vector. If the tag isnt found, then the tag is removed from the vector and a corresponding end tag is written out.

Throws:
IOException - on any I/O error

isFormElementWithContent

private boolean isFormElementWithContent(AttributeSet attr)
Determines if the element associated with the attributeset is a TEXTAREA or SELECT. If true, returns true else false


indentNeedsIncrementing

private boolean indentNeedsIncrementing(Element current,
                                        Element next)

writeMaps

void writeMaps(Enumeration maps)
         throws IOException
Outputs the maps as elements. Maps are not stored as elements in the document, and as such this is used to output them.

Throws:
IOException

writeStyles

void writeStyles(StyleSheet sheet)
           throws IOException
Outputs the styles as a single element. Styles are not stored as elements, but part of the document. For the time being styles are written out as a comment, inside a style tag.

Throws:
IOException

writeStyle

boolean writeStyle(String name,
                   Style style,
                   boolean outputStyle)
             throws IOException
Outputs the named style. outputStyle indicates whether or not a style has been output yet. This will return true if a style is written.

Throws:
IOException

writeStyleStartTag

void writeStyleStartTag()
                  throws IOException
Throws:
IOException

writeStyleEndTag

void writeStyleEndTag()
                throws IOException
Throws:
IOException

convertToHTML

AttributeSet convertToHTML(AttributeSet from,
                           MutableAttributeSet to)
Convert the give set of attributes to be html for the purpose of writing them out. Any keys that have been converted will not appear in the resultant set. Any keys not converted will appear in the resultant set the same as the received set.

This will put the converted values into to, unless it is null in which case a temporary AttributeSet will be returned.


convertToHTML32

private static void convertToHTML32(AttributeSet from,
                                    MutableAttributeSet to)
Create an older style of HTML attributes. This will convert character level attributes that have a StyleConstants mapping over to an HTML tag/attribute. Other CSS attributes will be placed in an HTML style attribute.


createFontAttribute

private static void createFontAttribute(CSS.Attribute a,
                                        AttributeSet from,
                                        MutableAttributeSet to)
Create/update an HTML <font> tag attribute. The value of the attribute should be a MutableAttributeSet so that the attributes can be updated as they are discovered.


convertToHTML40

private static void convertToHTML40(AttributeSet from,
                                    MutableAttributeSet to)
Copies the given AttributeSet to a new set, converting any CSS attributes found to arguments of an HTML style attribute.


writeLineSeparator

protected void writeLineSeparator()
                           throws IOException
Writes the line separator. This is overriden to make sure we don't replace the newline content in case it is outside normal ascii.

Overrides:
writeLineSeparator in class AbstractWriter
Throws:
IOException

output

protected void output(char[] chars,
                      int start,
                      int length)
               throws IOException
This method is overriden to map any character entities, such as < to &lt;. super.output will be invoked to write the content.

Overrides:
output in class AbstractWriter
Throws:
IOException

output

private void output(String string)
             throws IOException
This directly invokes super's output after converting string to a char[].

Throws:
IOException