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

public Class MinimalHTMLWriter

extends AbstractWriter
Class Inheritance
Imports
java.io.Writer, .IOException, java.util.*, java.awt.Color, javax.swing.text.*

MinimalHTMLWriter is a fallback writer used by the HTMLEditorKit to write out HTML for a document that is a not produced by the EditorKit. The format for the document is:
<html>
  <head>
    <style>
       <!-- list of named styles
        p.normal {
           font-family: SansSerif;
           margin-height: 0;
           font-size: 14
        }
       -->
     </style>
  </head>
  <body>
   <p style=normal>
       Bold, italic, and underline attributes
       of the run are emitted as HTML tags.
       The remaining attributes are emitted as
       part of the style attribute of a <span> tag.
       The syntax is similar to inline styles.
   </p>
  </body>
</html>
Author
Sunita Mani

Field Summary

Modifier and TypeField and Description
private static final int
BOLD

These static finals are used to tweak and query the fontMask about which of these tags need to be generated or terminated.

private static final CSS
pack-priv int
private AttributeSet
fontAttributes

Stores the attributes of the previous run.

private int
private static final int
pack-priv int
private Hashtable<String, String>
styleNameMapping

Maps from style name as held by the Document, to the archived style name (style name written out).

private static final int
Inherited from javax.swing.text.AbstractWriter:
NEWLINE

Constructor Summary

AccessConstructor and Description
public
MinimalHTMLWriter(Writer
Writer
w
,
StyledDocument
StyledDocument
doc
)

Creates a new MinimalHTMLWriter.

public
MinimalHTMLWriter(Writer
Writer
w
,
StyledDocument
StyledDocument
doc
,
int
The location in the document to fetch the content.
pos
,
int
The amount to write out.
len
)

Creates a new MinimalHTMLWriter.

Method Summary

Modifier and TypeMethod and Description
private String
addStyleName(String style)

Adds the style named style to the style mapping.

protected void
endFontTag()

This is no longer used, instead <span> will be written out.

private void
endSpanTag()

Writes out an end tag for the <span> tag.

protected boolean

Returns:

true if we are currently in a <font> tag.
inFontTag
()

Returns true if we are currently in a <font> tag.

protected boolean

Returns:

true if the element is a text element.
isText
(Element
an element
elem
)

Returns true if the element is a text element.

private boolean
isValidCharacter(char character)

private String
mapStyleName(String style)

Returns the mapped style name corresponding to style.

private void
setFontMask(AttributeSet attr)

Tweaks the appropriate bits of fontMask to reflect whether the text is to be displayed in bold, italic, and/or with an underline.

protected void
startFontTag(String
a font style
style
)

This is no longer used, instead <span> will be written out.

private void
startSpanTag(String style)

Writes out a start tag for the <font> tag.

protected void
text(Element elem)

Overrides javax.swing.text.AbstractWriter.text.

Writes out text.

public void
write()

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

Generates HTML output from a StyledDocument.

protected void
writeAttributes(AttributeSet attr)

Overrides javax.swing.text.AbstractWriter.writeAttributes.

Writes out all the attributes for the following types: StyleConstants.ParagraphConstants, StyleConstants.CharacterConstants, StyleConstants.FontConstants, StyleConstants.ColorConstants.

protected void
writeBody()

Iterates over the elements in the document and processes elements based on whether they are branch elements or leaf elements.

protected void
writeComponent(Element
an element of type StyleConstants.ComponentElementName
elem
)

Responsible for handling Component Elements; deliberately unimplemented.

protected void
writeContent(Element
an element
elem
,
boolean
indentation will be added if needsIndenting is true
needsIndenting
)

Writes out the attribute set in an HTML-compliant manner.

private void
writeEndMask(int mask)

Writes out end tags for <u>, <i>, and <b> based on the mask settings.

protected void
writeEndParagraph()

Emits an end tag for a <p> tag.

protected void
writeEndTag(String
an end tag
endTag
)

Writes out an end tag appropriately indented.

protected void
writeHeader()

Writes out the <head> and <style> tags, and then invokes writeStyles() to write out all the named styles as the content of the <style> tag.

protected void
writeHTMLTags(AttributeSet
a set of attributes
attr
)

Generates bold <b>, italic <i>, and <u> tags for the text based on its attribute settings.

protected void
writeImage(Element
an element of type StyleConstants.IconElementName
elem
)

Responsible for handling Icon Elements; deliberately unimplemented.

protected void
writeLeaf(Element
an element
elem
)

Responsible for writing out other non-text leaf elements.

protected void
writeNonHTMLAttributes(AttributeSet
a set of attributes
attr
)

Writes out the remaining character-level attributes (attributes other than bold, italic, and underline) in an HTML-compliant way.

private void
writeStartMask(int mask)

Writes out start tags <u>, <i>, and <b> based on the mask settings.

protected void
writeStartParagraph(Element
an element
elem
)

Emits the start tag for a paragraph.

protected void
writeStartTag(String
a start tag
tag
)

Writes out a start tag appropriately indented.

protected void
writeStyles()

Writes out all the named styles as the content of the <style> tag.

Inherited from javax.swing.text.AbstractWriter:
decrIndentgetCanWrapLinesgetCurrentLineLengthgetDocumentgetElementIteratorgetEndOffsetgetIndentLevelgetIndentSpacegetLineLengthgetLineSeparatorgetStartOffsetgetTextgetWriterincrIndentindentinRangeisLineEmptyoutputsetCanWrapLinessetCurrentLineLengthsetIndentSpacesetLineLengthsetLineSeparatorwritewritewritewriteLineSeparator

Field Detail

BOLDback to summary
private static final int BOLD

These static finals are used to tweak and query the fontMask about which of these tags need to be generated or terminated.

cssback to summary
private static final CSS css
endOffsetback to summary
pack-priv int endOffset

Hides javax.swing.text.AbstractWriter.endOffset.

fontAttributesback to summary
private AttributeSet fontAttributes

Stores the attributes of the previous run. Used to compare with the current run's attributeset. If identical, then a <span> tag is not emitted.

fontMaskback to summary
private int fontMask
ITALICback to summary
private static final int ITALIC
startOffsetback to summary
pack-priv int startOffset

Hides javax.swing.text.AbstractWriter.startOffset.

styleNameMappingback to summary
private Hashtable<String, String> styleNameMapping

Maps from style name as held by the Document, to the archived style name (style name written out). These may differ.

UNDERLINEback to summary
private static final int UNDERLINE

Constructor Detail

MinimalHTMLWriterback to summary
public MinimalHTMLWriter(Writer w, StyledDocument doc)

Creates a new MinimalHTMLWriter.

Parameters
w:Writer

Writer

doc:StyledDocument

StyledDocument

MinimalHTMLWriterback to summary
public MinimalHTMLWriter(Writer w, StyledDocument doc, int pos, int len)

Creates a new MinimalHTMLWriter.

Parameters
w:Writer

Writer

doc:StyledDocument

StyledDocument

pos:int

The location in the document to fetch the content.

len:int

The amount to write out.

Method Detail

addStyleNameback to summary
private String addStyleName(String style)

Adds the style named style to the style mapping. This returns the name that should be used when outputting. CSS does not allow the full Unicode set to be used as a style name.

endFontTagback to summary
protected void endFontTag() throws IOException

This is no longer used, instead <span> will be written out.

Writes out an end tag for the <font> tag.

Exceptions
IOException:
on any I/O error
endSpanTagback to summary
private void endSpanTag() throws IOException

Writes out an end tag for the <span> tag.

Exceptions
IOException:
on any I/O error
inFontTagback to summary
protected boolean inFontTag()

Returns true if we are currently in a <font> tag.

Returns:boolean

true if we are currently in a <font> tag.

isTextback to summary
protected boolean isText(Element elem)

Returns true if the element is a text element.

Parameters
elem:Element

an element

Returns:boolean

true if the element is a text element.

isValidCharacterback to summary
private boolean isValidCharacter(char character)
mapStyleNameback to summary
private String mapStyleName(String style)

Returns the mapped style name corresponding to style.

setFontMaskback to summary
private void setFontMask(AttributeSet attr)

Tweaks the appropriate bits of fontMask to reflect whether the text is to be displayed in bold, italic, and/or with an underline.

startFontTagback to summary
protected void startFontTag(String style) throws IOException

This is no longer used, instead <span> will be written out.

Writes out a start tag for the <font> tag. Because font tags cannot be nested, this method closes out any enclosing font tag before writing out a new start tag.

Parameters
style:String

a font style

Exceptions
IOException:
on any I/O error
startSpanTagback to summary
private void startSpanTag(String style) throws IOException

Writes out a start tag for the <font> tag. Because font tags cannot be nested, this method closes out any enclosing font tag before writing out a new start tag.

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

Overrides javax.swing.text.AbstractWriter.text.

Writes out text.

Exceptions
IOException:
on any I/O error
BadLocationException:

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

if pos represents an invalid location within the document.

writeback to summary
public void write() throws IOException, BadLocationException

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

Generates HTML output from a StyledDocument.

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

Overrides javax.swing.text.AbstractWriter.writeAttributes.

Writes out all the attributes for the following types: StyleConstants.ParagraphConstants, StyleConstants.CharacterConstants, StyleConstants.FontConstants, StyleConstants.ColorConstants. The attribute name and value are separated by a colon. Each pair is separated by a semicolon.

Exceptions
IOException:
on any I/O error
writeBodyback to summary
protected void writeBody() throws IOException, BadLocationException

Iterates over the elements in the document and processes elements based on whether they are branch elements or leaf elements. This method specially handles leaf elements that are text.

Exceptions
IOException:
on any I/O error
BadLocationException:
if we are in an invalid location within the document.
writeComponentback to summary
protected void writeComponent(Element elem) throws IOException

Responsible for handling Component Elements; deliberately unimplemented. How this method is implemented is a matter of policy.

Parameters
elem:Element

an element of type StyleConstants.ComponentElementName

Exceptions
IOException:
if I/O error occurred.
writeContentback to summary
protected void writeContent(Element elem, boolean needsIndenting) throws IOException, BadLocationException

Writes out the attribute set in an HTML-compliant manner.

Parameters
elem:Element

an element

needsIndenting:boolean

indentation will be added if needsIndenting is true

Exceptions
IOException:
on any I/O error
BadLocationException:
if pos represents an invalid location within the document.
writeEndMaskback to summary
private void writeEndMask(int mask) throws IOException

Writes out end tags for <u>, <i>, and <b> based on the mask settings.

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

Emits an end tag for a <p> tag. Before writing out the tag, this method ensures that all other tags that have been opened are appropriately closed off.

Exceptions
IOException:
on any I/O error
writeEndTagback to summary
protected void writeEndTag(String endTag) throws IOException

Writes out an end tag appropriately indented. Also decrements the indent level.

Parameters
endTag:String

an end tag

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

Writes out the <head> and <style> tags, and then invokes writeStyles() to write out all the named styles as the content of the <style> tag. The content is surrounded by valid HTML comment markers to ensure that the document is viewable in applications/browsers that do not support the tag.

Exceptions
IOException:
on any I/O error
writeHTMLTagsback to summary
protected void writeHTMLTags(AttributeSet attr) throws IOException

Generates bold <b>, italic <i>, and <u> tags for the text based on its attribute settings.

Parameters
attr:AttributeSet

a set of attributes

Exceptions
IOException:
on any I/O error
writeImageback to summary
protected void writeImage(Element elem) throws IOException

Responsible for handling Icon Elements; deliberately unimplemented. How to implement this method is an issue of policy. For example, if you're generating an <img> tag, how should you represent the src attribute (the location of the image)? In certain cases it could be a URL, in others it could be read from a stream.

Parameters
elem:Element

an element of type StyleConstants.IconElementName

Exceptions
IOException:
if I/O error occurred.
writeLeafback to summary
protected void writeLeaf(Element elem) throws IOException

Responsible for writing out other non-text leaf elements.

Parameters
elem:Element

an element

Exceptions
IOException:
on any I/O error
writeNonHTMLAttributesback to summary
protected void writeNonHTMLAttributes(AttributeSet attr) throws IOException

Writes out the remaining character-level attributes (attributes other than bold, italic, and underline) in an HTML-compliant way. Given that attributes such as font family and font size have no direct mapping to HTML tags, a <span> tag is generated and its style attribute is set to contain the list of remaining attributes just like inline styles.

Parameters
attr:AttributeSet

a set of attributes

Exceptions
IOException:
on any I/O error
writeStartMaskback to summary
private void writeStartMask(int mask) throws IOException

Writes out start tags <u>, <i>, and <b> based on the mask settings.

Exceptions
IOException:
on any I/O error
writeStartParagraphback to summary
protected void writeStartParagraph(Element elem) throws IOException

Emits the start tag for a paragraph. If the paragraph has a named style associated with it, then this method also generates a class attribute for the <p> tag and sets its value to be the name of the style.

Parameters
elem:Element

an element

Exceptions
IOException:
on any I/O error
writeStartTagback to summary
protected void writeStartTag(String tag) throws IOException

Writes out a start tag appropriately indented. Also increments the indent level.

Parameters
tag:String

a start tag

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

Writes out all the named styles as the content of the <style> tag.

Exceptions
IOException:
on any I/O error