APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.text.html

public Class HTMLWriter

extends AbstractWriter
Class Inheritance
Imports
java.io.IOException, .Writer, java.util.Enumeration, .Stack, .Vector, javax.swing.text.AbstractWriter, .AttributeSet, .BadLocationException, .Document, .Element, .ElementIterator, .MutableAttributeSet, .Segment, .SimpleAttributeSet, .Style, .StyleConstants, .StyleContext

This is a writer for HTMLDocuments.
Author
Sunita Mani

Field Summary

Modifier and TypeField and Description
private Stack<Element>
private boolean
private MutableAttributeSet
convAttr

Buffer for the purpose of attribute conversion

private boolean
private boolean
private boolean
indentNext

Determines whether the indentation needs to be incremented.

private boolean
private boolean
private boolean
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

Hides javax.swing.text.AbstractWriter.segment.

Used when writing out content.
private Vector<HTML.Tag>
private Vector<HTML.Tag>
private Vector<Object>
tagValues

Values for the tags.

private char[]
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).

private boolean
wroteHead

Set to true after the head has been output.

Inherited from javax.swing.text.AbstractWriter:
NEWLINE

Constructor Summary

AccessConstructor and Description
public
HTMLWriter(Writer
a Writer
w
,
HTMLDocument
an HTMLDocument
doc
)

Creates a new HTMLWriter.

public
HTMLWriter(Writer
a Writer
w
,
HTMLDocument
an HTMLDocument
doc
,
int
the document location from which to fetch the content
pos
,
int
the amount to write out
len
)

Creates a new HTMLWriter.

Method Summary

Modifier and TypeMethod and Description
private static void
addAttribute(MutableAttributeSet to, Object key, Object value)

Add an attribute only if it doesn't exist so that we don't loose information replacing it with SimpleAttributeSet.EMPTY

protected void
closeOutUnwantedEmbeddedTags(AttributeSet
a set of attributes
attr
)

Searches the attribute set and for each tag that is stored in the tag vector.

protected void
comment(Element
an Element
elem
)

Writes out comments.

pack-priv 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
an Element
elem
)

Writes out all empty elements (all tags that have no corresponding end tag).

protected void
endTag(Element
an Element
elem
)

Writes out an end tag for the element.

private boolean
private void
indentSmart()

Writes indent only once per line.

protected boolean

Returns:

true if tag is block tag, false otherwise.
isBlockTag
(AttributeSet
an 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.

private boolean
isPreTagWithParagraphTag(AttributeSet attr)

Determines if the element associated with the attributeset is a P tag and it is within Pre tag.

protected boolean

Returns:

true if the StyleConstants.NameAttribute is equal to the tag that is passed in as a parameter.
matchNameAttribute
(AttributeSet
a set of attributes
attr
,
HTML.Tag
an 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)

Overrides javax.swing.text.AbstractWriter.output.

This method is overridden 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
the AttributeSet associated with the form element
attr
)

Writes out the content of the SELECT form element.

protected void
startTag(Element
an Element
elem
)

Writes out a start tag for the element.

protected boolean

Returns:

true if the element is a synthesized element.
synthesizedElement
(Element
an element
elem
)

Returns true if the element is a synthesized element.

protected void
text(Element
an Element
elem
)

Overrides javax.swing.text.AbstractWriter.text.

Writes out text.

protected void
textAreaContent(AttributeSet
an AttributeSet
attr
)

Writes out text that is contained in a TEXTAREA form element.

public void
write()

Implements abstract javax.swing.text.AbstractWriter.write.

Iterates over the Element tree and controls the writing out of all the tags and its attributes.

pack-priv void
writeAdditionalComments()

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

protected void
writeAttributes(AttributeSet
an AttributeSet
attr
)

Overrides javax.swing.text.AbstractWriter.writeAttributes.

Writes out the attribute set.

pack-priv void
writeComment(String
the comment
string
)

Writes out comment string.

protected void
writeEmbeddedTags(AttributeSet
a set of attributes
attr
)

Searches for embedded tags in the AttributeSet and writes them out.

protected void
pack-priv void
writeMaps(Enumeration<?> maps)

Outputs the maps as elements.

protected void
writeOption(Option
an Option
option
)

Writes out the content of the Option form element.

pack-priv boolean
writeStyle(String name, Style style, boolean outputStyle)

Outputs the named style.

pack-priv void
pack-priv void
writeStyles(StyleSheet sheet)

Outputs the styles as a single element.

pack-priv void
Inherited from javax.swing.text.AbstractWriter:
decrIndentgetCanWrapLinesgetCurrentLineLengthgetDocumentgetElementIteratorgetEndOffsetgetIndentLevelgetIndentSpacegetLineLengthgetLineSeparatorgetStartOffsetgetTextgetWriterincrIndentindentinRangeisLineEmptysetCanWrapLinessetCurrentLineLengthsetIndentSpacesetLineLengthsetLineSeparatorwritewritewrite

Field Detail

blockElementStackback to summary
private Stack<Element> blockElementStack
completeDocback to summary
private boolean completeDoc
convAttrback to summary
private MutableAttributeSet convAttr

Buffer for the purpose of attribute conversion

inContentback to summary
private boolean inContent
indentedback to summary
private boolean indented
indentNextback to summary
private boolean indentNext

Determines whether 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.

inPreback to summary
private boolean inPre
inTextAreaback to summary
private boolean inTextArea
newlineOutputedback to summary
private boolean newlineOutputed
oConvAttrback to summary
private MutableAttributeSet oConvAttr

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

preEndOffsetback to summary
private int preEndOffset

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

replaceEntitiesback to summary
private boolean replaceEntities

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

segmentback to summary
private Segment segment

Hides javax.swing.text.AbstractWriter.segment.

Used when writing out content.

tagsback to summary
private Vector<HTML.Tag> tags
tagsToRemoveback to summary
private Vector<HTML.Tag> tagsToRemove
tagValuesback to summary
private Vector<Object> tagValues

Values for the tags.

tempCharsback to summary
private char[] tempChars

Hides javax.swing.text.AbstractWriter.tempChars.

Temporary buffer.

writeCSSback to summary
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).

wroteHeadback to summary
private boolean wroteHead

Set to true after the head has been output.

Constructor Detail

HTMLWriterback to summary
public HTMLWriter(Writer w, HTMLDocument doc)

Creates a new HTMLWriter.

Parameters
w:Writer

a Writer

doc:HTMLDocument

an HTMLDocument

HTMLWriterback to summary
public HTMLWriter(Writer w, HTMLDocument doc, int pos, int len)

Creates a new HTMLWriter.

Parameters
w:Writer

a Writer

doc:HTMLDocument

an HTMLDocument

pos:int

the document location from which to fetch the content

len:int

the amount to write out

Method Detail

addAttributeback to summary
private static void addAttribute(MutableAttributeSet to, Object key, Object value)

Add an attribute only if it doesn't exist so that we don't loose information replacing it with SimpleAttributeSet.EMPTY

closeOutUnwantedEmbeddedTagsback to summary
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 is not found, then the tag is removed from the vector and a corresponding end tag is written out.

Parameters
attr:AttributeSet

a set of attributes

Exceptions
IOException:
on any I/O error
commentback to summary
protected void comment(Element elem) throws BadLocationException, IOException

Writes out comments.

Parameters
elem:Element

an Element

Exceptions
BadLocationException:
if pos represents an invalid location within the document.
IOException:
on any I/O error
convertToHTMLback to summary
pack-priv 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.

convertToHTML32back to summary
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.

convertToHTML40back to summary
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.

createFontAttributeback to summary
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.

emptyTagback to summary
protected void emptyTag(Element elem) throws BadLocationException, IOException

Writes out all empty elements (all tags that have no corresponding end tag).

Parameters
elem:Element

an Element

Exceptions
BadLocationException:
if pos represents an invalid location within the document.
IOException:
on any I/O error
endTagback to summary
protected void endTag(Element elem) throws IOException

Writes out an end tag for the element.

Parameters
elem:Element

an Element

Exceptions
IOException:
on any I/O error
indentNeedsIncrementingback to summary
private boolean indentNeedsIncrementing(Element current, Element next)
indentSmartback to summary
private void indentSmart() throws IOException

Writes indent only once per line.

isBlockTagback to summary
protected boolean isBlockTag(AttributeSet attr)

Determines if the HTML.Tag associated with the element is a block tag.

Parameters
attr:AttributeSet

an AttributeSet

Returns:boolean

true if tag is block tag, false otherwise.

isFormElementWithContentback to summary
private boolean isFormElementWithContent(AttributeSet attr)

Determines if the element associated with the attributeset is a TEXTAREA or SELECT. If true, returns true else false

isPreTagWithParagraphTagback to summary
private boolean isPreTagWithParagraphTag(AttributeSet attr)

Determines if the element associated with the attributeset is a P tag and it is within Pre tag. If true, returns true else false

matchNameAttributeback to summary
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.

Parameters
attr:AttributeSet

a set of attributes

tag:HTML.Tag

an HTML tag

Returns:boolean

true if the StyleConstants.NameAttribute is equal to the tag that is passed in as a parameter.

noMatchForTagInAttributesback to summary
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.

outputback to summary
protected void output(char[] chars, int start, int length) throws IOException

Overrides javax.swing.text.AbstractWriter.output.

This method is overridden to map any character entities, such as < to &lt;. super.output will be invoked to write the content.

Exceptions
IOException:

Doc from javax.swing.text.AbstractWriter.output.

on any I/O error

Since
1.3
outputback to summary
private void output(String string) throws IOException

This directly invokes super's output after converting string to a char[].

selectContentback to summary
protected void selectContent(AttributeSet attr) throws IOException

Writes out the content of the SELECT form element.

Parameters
attr:AttributeSet

the AttributeSet associated with the form element

Exceptions
IOException:
on any I/O error
startTagback to summary
protected void startTag(Element elem) throws IOException, BadLocationException

Writes out a start tag for the element. Ignores all synthesized elements.

Parameters
elem:Element

an Element

Exceptions
IOException:
on any I/O error
BadLocationException:
if pos represents an invalid location within the document.
synthesizedElementback to summary
protected boolean synthesizedElement(Element elem)

Returns true if the element is a synthesized element. Currently we are only testing for the p-implied tag.

Parameters
elem:Element

an element

Returns:boolean

true if the element is a synthesized element.

textback to summary
protected void text(Element elem) throws BadLocationException, IOException

Overrides javax.swing.text.AbstractWriter.text.

Writes out text. If a range is specified when the constructor is invoked, then only the appropriate range of text is written out.

Parameters
elem:Element

an Element

Exceptions
BadLocationException:
if pos represents an invalid location within the document.
IOException:
on any I/O error
textAreaContentback to summary
protected void textAreaContent(AttributeSet attr) throws BadLocationException, IOException

Writes out text that is contained in a TEXTAREA form element.

Parameters
attr:AttributeSet

an AttributeSet

Exceptions
BadLocationException:
if pos represents an invalid location within the document.
IOException:
on any I/O error
writeback to summary
public void write() throws IOException, BadLocationException

Implements abstract javax.swing.text.AbstractWriter.write.

Iterates over the Element tree and controls the writing out of all the tags and its attributes.

Exceptions
IOException:
on any I/O error
BadLocationException:
if pos represents an invalid location within the document.
writeAdditionalCommentsback to summary
pack-priv void writeAdditionalComments() throws IOException

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

writeAttributesback to summary
protected void writeAttributes(AttributeSet attr) throws IOException

Overrides javax.swing.text.AbstractWriter.writeAttributes.

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.

Parameters
attr:AttributeSet

an AttributeSet

Exceptions
IOException:
on any I/O error
writeCommentback to summary
pack-priv void writeComment(String string) throws IOException

Writes out comment string.

Parameters
string:String

the comment

Exceptions
IOException:
on any I/O error
BadLocationException:
if pos represents an invalid location within the document.
writeEmbeddedTagsback to summary
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.

Parameters
attr:AttributeSet

a set of attributes

Exceptions
IOException:
on any I/O error
writeLineSeparatorback to summary
protected void writeLineSeparator() throws IOException

Overrides javax.swing.text.AbstractWriter.writeLineSeparator.

Writes the line separator. This is overridden to make sure we don't replace the newline content in case it is outside normal ascii.

Exceptions
IOException:

Doc from javax.swing.text.AbstractWriter.writeLineSeparator.

on any I/O error

Since
1.3
writeMapsback to summary
pack-priv 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.

writeOptionback to summary
protected void writeOption(Option option) throws IOException

Writes out the content of the Option form element.

Parameters
option:Option

an Option

Exceptions
IOException:
on any I/O error
writeStyleback to summary
pack-priv 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.

writeStyleEndTagback to summary
pack-priv void writeStyleEndTag() throws IOException
writeStylesback to summary
pack-priv 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.

writeStyleStartTagback to summary
pack-priv void writeStyleStartTag() throws IOException