Class MinimalHTMLWriter
java.lang.Object
javax.swing.text.AbstractWriter
javax.swing.text.html.MinimalHTMLWriter
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>
-
Field Summary
Fields declared in class AbstractWriter
NEWLINE
Modifier and TypeFieldDescriptionprotected static final char
How the text packages models newlines. -
Constructor Summary
ConstructorsConstructorDescriptionMinimalHTMLWriter
(Writer w, StyledDocument doc) Creates a new MinimalHTMLWriter.MinimalHTMLWriter
(Writer w, StyledDocument doc, int pos, int len) Creates a new MinimalHTMLWriter. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
This is no longer used, instead <span> will be written out.protected boolean
Returns true if we are currently in a <font> tag.protected boolean
Returns true if the element is a text element.protected void
startFontTag
(String style) This is no longer used, instead <span> will be written out.protected void
Writes out text.void
write()
Generates HTML output from a StyledDocument.protected void
writeAttributes
(AttributeSet attr) Writes out all the attributes for the following types: StyleConstants.ParagraphConstants, StyleConstants.CharacterConstants, StyleConstants.FontConstants, StyleConstants.ColorConstants.protected void
Iterates over the elements in the document and processes elements based on whether they are branch elements or leaf elements.protected void
writeComponent
(Element elem) Responsible for handling Component Elements; deliberately unimplemented.protected void
writeContent
(Element elem, boolean needsIndenting) Writes out the attribute set in an HTML-compliant manner.protected void
Emits an end tag for a <p> tag.protected void
writeEndTag
(String endTag) Writes out an end tag appropriately indented.protected void
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 attr) Generates bold <b>, italic <i>, and <u> tags for the text based on its attribute settings.protected void
writeImage
(Element elem) Responsible for handling Icon Elements; deliberately unimplemented.protected void
Responsible for writing out other non-text leaf elements.protected void
Writes out the remaining character-level attributes (attributes other than bold, italic, and underline) in an HTML-compliant way.protected void
writeStartParagraph
(Element elem) Emits the start tag for a paragraph.protected void
writeStartTag
(String tag) Writes out a start tag appropriately indented.protected void
Writes out all the named styles as the content of the <style> tag.Methods declared in class AbstractWriter
decrIndent, getCanWrapLines, getCurrentLineLength, getDocument, getElementIterator, getEndOffset, getIndentLevel, getIndentSpace, getLineLength, getLineSeparator, getStartOffset, getText, getWriter, incrIndent, indent, inRange, isLineEmpty, output, setCanWrapLines, setCurrentLineLength, setIndentSpace, setLineLength, setLineSeparator, write, write, write, writeLineSeparator
Modifier and TypeMethodDescriptionprotected void
Decrements the indent level.protected boolean
Returns whether or not the lines can be wrapped.protected int
Returns the current line length.protected Document
Fetches the document.protected ElementIterator
Fetches the ElementIterator.int
Returns the last offset to be output.protected int
Returns the current indentation level.protected int
Returns the amount of space to indent.protected int
Returns the maximum line length.Returns the string used to represent newlines.int
Returns the first offset to be output.protected String
Returns the text associated with the element.protected Writer
Returns the Writer that is used to output the content.protected void
Increments the indent level.protected void
indent()
Does indentation.protected boolean
This method determines whether the current element is in the range specified.protected boolean
Returns true if the current line should be considered empty.protected void
output
(char[] content, int start, int length) The last stop in writing out content.protected void
setCanWrapLines
(boolean newValue) Sets whether or not lines can be wrapped.protected void
setCurrentLineLength
(int length) Sets the current line length.protected void
setIndentSpace
(int space) Enables subclasses to specify how many spaces an indent maps to.protected void
setLineLength
(int l) Enables subclasses to set the number of characters they want written per line.void
setLineSeparator
(String value) Sets the String used to represent newlines.protected void
write
(char ch) Writes out a character.protected void
write
(char[] chars, int startIndex, int length) All write methods call into this one.protected void
Writes out a string.protected void
Writes the line separator.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Modifier and TypeMethodDescriptionprotected Object
clone()
Creates and returns a copy of this object.boolean
Indicates whether some other object is "equal to" this one.protected void
finalize()
Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class
<?> getClass()
Returns the runtime class of thisObject
.int
hashCode()
Returns a hash code value for this object.final void
notify()
Wakes up a single thread that is waiting on this object's monitor.final void
Wakes up all threads that are waiting on this object's monitor.toString()
Returns a string representation of the object.final void
wait()
Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final void
wait
(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final void
wait
(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Constructor Details
-
MinimalHTMLWriter
Creates a new MinimalHTMLWriter.- Parameters:
w
- Writerdoc
- StyledDocument
-
MinimalHTMLWriter
Creates a new MinimalHTMLWriter.- Parameters:
w
- Writerdoc
- StyledDocumentpos
- The location in the document to fetch the content.len
- The amount to write out.
-
-
Method Details
-
write
Generates HTML output from a StyledDocument.- Specified by:
write
in classAbstractWriter
- Throws:
IOException
- on any I/O errorBadLocationException
- if pos represents an invalid location within the document.
-
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.- Overrides:
writeAttributes
in classAbstractWriter
- Parameters:
attr
- an AttributeSet.- Throws:
IOException
- on any I/O error
-
text
Writes out text.- Overrides:
text
in classAbstractWriter
- Parameters:
elem
- an Element.- Throws:
IOException
- on any I/O errorBadLocationException
- if pos represents an invalid location within the document.
-
writeStartTag
Writes out a start tag appropriately indented. Also increments the indent level.- Parameters:
tag
- a start tag- Throws:
IOException
- on any I/O error
-
writeEndTag
Writes out an end tag appropriately indented. Also decrements the indent level.- Parameters:
endTag
- an end tag- Throws:
IOException
- on any I/O error
-
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. 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.- Throws:
IOException
- on any I/O error
-
writeStyles
Writes out all the named styles as the content of the <style> tag.- Throws:
IOException
- on any I/O error
-
writeBody
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.- Throws:
IOException
- on any I/O errorBadLocationException
- if we are in an invalid location within the document.
-
writeEndParagraph
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.- Throws:
IOException
- on any I/O error
-
writeStartParagraph
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
- an element- Throws:
IOException
- on any I/O error
-
writeLeaf
Responsible for writing out other non-text leaf elements.- Parameters:
elem
- an element- Throws:
IOException
- on any I/O error
-
writeImage
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
- an element of type StyleConstants.IconElementName- Throws:
IOException
- if I/O error occurred.
-
writeComponent
Responsible for handling Component Elements; deliberately unimplemented. How this method is implemented is a matter of policy.- Parameters:
elem
- an element of type StyleConstants.ComponentElementName- Throws:
IOException
- if I/O error occurred.
-
isText
Returns true if the element is a text element.- Parameters:
elem
- an element- Returns:
true
if the element is a text element.
-
writeContent
protected void writeContent(Element elem, boolean needsIndenting) throws IOException, BadLocationException Writes out the attribute set in an HTML-compliant manner.- Parameters:
elem
- an elementneedsIndenting
- indentation will be added ifneedsIndenting
istrue
- Throws:
IOException
- on any I/O errorBadLocationException
- if pos represents an invalid location within the document.
-
writeHTMLTags
Generates bold <b>, italic <i>, and <u> tags for the text based on its attribute settings.- Parameters:
attr
- a set of attributes- Throws:
IOException
- on any I/O error
-
writeNonHTMLAttributes
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
- a set of attributes- Throws:
IOException
- on any I/O error
-
inFontTag
protected boolean inFontTag()Returns true if we are currently in a <font> tag.- Returns:
true
if we are currently in a <font> tag.
-
endFontTag
This is no longer used, instead <span> will be written out.Writes out an end tag for the <font> tag.
- Throws:
IOException
- on any I/O error
-
startFontTag
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
- a font style- Throws:
IOException
- on any I/O error
-