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

public abstract Class AbstractWriter

extends Object
Class Inheritance
Known Direct Subclasses
javax.swing.text.html.HTMLWriter, javax.swing.text.html.MinimalHTMLWriter
Imports
java.io.Writer, .IOException, java.util.Enumeration

AbstractWriter is an abstract class that actually does the work of writing out the element tree including the attributes. In terms of how much is written out per line, the writer defaults to 100. But this value can be set by subclasses.
Author
Sunita Mani

Field Summary

Modifier and TypeField and Description
private boolean
canWrapLines

True indicates that when writing, the line can be split, false indicates that even if the line is > than max line length it should not be split.

private int
private Document
private int
private char[]
indentChars

Used when indenting.

private int
private int
private boolean
isLineEmpty

True while the current line is empty.

private ElementIterator
private String
lineSeparator

String used for end of line.

private int
protected static final char
NEWLINE

How the text packages models newlines.

private char[]
newlineChars

This is used in writeLineSeparator instead of tempChars.

private int
private Writer
private Segment
segment

Used for writing text.

private int
private char[]
tempChars

Used when writing out a string.

Constructor Summary

AccessConstructor and Description
protected
AbstractWriter(Writer
a Writer.
w
,
Document
a Document
doc
)

Creates a new AbstractWriter.

protected
AbstractWriter(Writer
a Writer
w
,
Document
an Element
doc
,
int
The location in the document to fetch the content.
pos
,
int
The amount to write out.
len
)

Creates a new AbstractWriter.

protected
AbstractWriter(Writer
a Writer
w
,
Element
an Element
root
)

Creates a new AbstractWriter.

protected
AbstractWriter(Writer
a Writer
w
,
Element
an Element
root
,
int
The location in the document to fetch the content.
pos
,
int
The amount to write out.
len
)

Creates a new AbstractWriter.

Method Summary

Modifier and TypeMethod and Description
protected void
decrIndent()

Decrements the indent level.

protected boolean

Returns:

whether or not the lines can be wrapped
getCanWrapLines
()

Returns whether or not the lines can be wrapped.

protected int

Returns:

the current line length
getCurrentLineLength
()

Returns the current line length.

protected Document

Returns:

the Document.
getDocument
()

Fetches the document.

protected ElementIterator

Returns:

the ElementIterator.
getElementIterator
()

Fetches the ElementIterator.

public int

Returns:

the last offset to be output
getEndOffset
()

Returns the last offset to be output.

protected int

Returns:

the current indentation level
getIndentLevel
()

Returns the current indentation level.

protected int

Returns:

the amount of space to indent
getIndentSpace
()

Returns the amount of space to indent.

protected int

Returns:

the maximum line length
getLineLength
()

Returns the maximum line length.

public String

Returns:

the string used to represent newlines
getLineSeparator
()

Returns the string used to represent newlines.

public int

Returns:

the first offset to be output
getStartOffset
()

Returns the first offset to be output.

protected String

Returns:

the text as a String
getText
(Element
an Element
elem
)

Returns the text associated with the element.

protected Writer

Returns:

the Writer that is used to output the content
getWriter
()

Returns the Writer that is used to output the content.

protected void
incrIndent()

Increments the indent level.

protected void
indent()

Does indentation.

private int
indexOf(char[] chars, char sChar, int startIndex, int endIndex)

Support method to locate an occurrence of a particular character.

protected boolean

Returns:

boolean that indicates whether the element is in the range.
inRange
(Element
an Element.
next
)

This method determines whether the current element is in the range specified.

protected boolean

Returns:

true if the current line should be considered empty
isLineEmpty
()

Returns true if the current line should be considered empty.

protected void
output(char[]
characters to output
content
,
int
starting index
start
,
int
length of output
length
)

The last stop in writing out content.

protected void
setCanWrapLines(boolean
new value for line wrapping
newValue
)

Sets whether or not lines can be wrapped.

protected void
setCurrentLineLength(int
the new line length
length
)

Sets the current line length.

protected void
setIndentSpace(int
an int representing the space to indent mapping.
space
)

Enables subclasses to specify how many spaces an indent maps to.

protected void
setLineLength(int
the maximum line length.
l
)

Enables subclasses to set the number of characters they want written per line.

public void
setLineSeparator(String
the new line separator
value
)

Sets the String used to represent newlines.

protected void
text(Element
an Element.
elem
)

Writes out text.

protected abstract void
write()

This abstract method needs to be implemented by subclasses.

protected void
write(char
a char.
ch
)

Writes out a character.

protected void
write(String
a String.
content
)

Writes out a string.

protected void
write(char[]
characters to output
chars
,
int
starting index
startIndex
,
int
length of output
length
)

All write methods call into this one.

protected void
writeAttributes(AttributeSet
an AttributeSet.
attr
)

Writes out the set of attributes as " <name>=<value>" pairs.

protected void
writeLineSeparator()

Writes the line separator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

canWrapLinesback to summary
private boolean canWrapLines

True indicates that when writing, the line can be split, false indicates that even if the line is > than max line length it should not be split.

currLengthback to summary
private int currLength
docback to summary
private Document doc
endOffsetback to summary
private int endOffset
indentCharsback to summary
private char[] indentChars

Used when indenting. Will contain the spaces.

indentLevelback to summary
private int indentLevel
indentSpaceback to summary
private int indentSpace
isLineEmptyback to summary
private boolean isLineEmpty

True while the current line is empty. This will remain true after indenting.

itback to summary
private ElementIterator it
lineSeparatorback to summary
private String lineSeparator

String used for end of line. If the Document has the property EndOfLineStringProperty, it will be used for newlines. Otherwise the System property line.separator will be used. The line separator can also be set.

maxLineLengthback to summary
private int maxLineLength
NEWLINEback to summary
protected static final char NEWLINE

How the text packages models newlines.

See Also
getLineSeparator
newlineCharsback to summary
private char[] newlineChars

This is used in writeLineSeparator instead of tempChars. If tempChars were used it would mean write couldn't invoke writeLineSeparator as it might have been passed tempChars.

offsetIndentback to summary
private int offsetIndent
outback to summary
private Writer out
segmentback to summary
private Segment segment

Used for writing text.

startOffsetback to summary
private int startOffset
tempCharsback to summary
private char[] tempChars

Used when writing out a string.

Constructor Detail

AbstractWriterback to summary
protected AbstractWriter(Writer w, Document doc)

Creates a new AbstractWriter. Initializes the ElementIterator with the default root of the document.

Parameters
w:Writer

a Writer.

doc:Document

a Document

AbstractWriterback to summary
protected AbstractWriter(Writer w, Document doc, int pos, int len)

Creates a new AbstractWriter. Initializes the ElementIterator with the element passed in.

Parameters
w:Writer

a Writer

doc:Document

an Element

pos:int

The location in the document to fetch the content.

len:int

The amount to write out.

AbstractWriterback to summary
protected AbstractWriter(Writer w, Element root)

Creates a new AbstractWriter. Initializes the ElementIterator with the element passed in.

Parameters
w:Writer

a Writer

root:Element

an Element

AbstractWriterback to summary
protected AbstractWriter(Writer w, Element root, int pos, int len)

Creates a new AbstractWriter. Initializes the ElementIterator with the element passed in.

Parameters
w:Writer

a Writer

root:Element

an Element

pos:int

The location in the document to fetch the content.

len:int

The amount to write out.

Method Detail

decrIndentback to summary
protected void decrIndent()

Decrements the indent level.

getCanWrapLinesback to summary
protected boolean getCanWrapLines()

Returns whether or not the lines can be wrapped. If this is false no lineSeparator's will be output.

Returns:boolean

whether or not the lines can be wrapped

Since
1.3
getCurrentLineLengthback to summary
protected int getCurrentLineLength()

Returns the current line length.

Returns:int

the current line length

Since
1.3
getDocumentback to summary
protected Document getDocument()

Fetches the document.

Returns:Document

the Document.

getElementIteratorback to summary
protected ElementIterator getElementIterator()

Fetches the ElementIterator.

Returns:ElementIterator

the ElementIterator.

getEndOffsetback to summary
public int getEndOffset()

Returns the last offset to be output.

Returns:int

the last offset to be output

Since
1.3
getIndentLevelback to summary
protected int getIndentLevel()

Returns the current indentation level. That is, the number of times incrIndent has been invoked minus the number of times decrIndent has been invoked.

Returns:int

the current indentation level

Since
1.3
getIndentSpaceback to summary
protected int getIndentSpace()

Returns the amount of space to indent.

Returns:int

the amount of space to indent

Since
1.3
getLineLengthback to summary
protected int getLineLength()

Returns the maximum line length.

Returns:int

the maximum line length

Since
1.3
getLineSeparatorback to summary
public String getLineSeparator()

Returns the string used to represent newlines.

Returns:String

the string used to represent newlines

Since
1.3
getStartOffsetback to summary
public int getStartOffset()

Returns the first offset to be output.

Returns:int

the first offset to be output

Since
1.3
getTextback to summary
protected String getText(Element elem) throws BadLocationException

Returns the text associated with the element. The assumption here is that the element is a leaf element. Throws a BadLocationException when encountered.

Parameters
elem:Element

an Element

Returns:String

the text as a String

Exceptions
BadLocationException:
if pos represents an invalid location within the document
getWriterback to summary
protected Writer getWriter()

Returns the Writer that is used to output the content.

Returns:Writer

the Writer that is used to output the content

Since
1.3
incrIndentback to summary
protected void incrIndent()

Increments the indent level. If indenting would cause getIndentSpace() *getIndentLevel() to be > than getLineLength() this will not cause an indent.

indentback to summary
protected void indent() throws IOException

Does indentation. The number of spaces written out is indent level times the space to map mapping. If the current line is empty, this will not make it so that the current line is still considered empty.

Exceptions
IOException:
on any I/O error
indexOfback to summary
private int indexOf(char[] chars, char sChar, int startIndex, int endIndex)

Support method to locate an occurrence of a particular character.

inRangeback to summary
protected boolean inRange(Element next)

This method determines whether the current element is in the range specified. When no range is specified, the range is initialized to be the entire document. inRange() returns true if the range specified intersects with the element's range.

Parameters
next:Element

an Element.

Returns:boolean

boolean that indicates whether the element is in the range.

isLineEmptyback to summary
protected boolean isLineEmpty()

Returns true if the current line should be considered empty. This is true when getCurrentLineLength == 0 || indent has been invoked on an empty line.

Returns:boolean

true if the current line should be considered empty

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

The last stop in writing out content. All the write methods eventually make it to this method, which invokes write on the Writer.

This method also updates the line length based on length. If this is invoked to output a newline, the current line length will need to be reset as will no longer be valid. If it is up to the caller to do this. Use writeLineSeparator to write out a newline, which will property update the current line length.

Parameters
content:char[]

characters to output

start:int

starting index

length:int

length of output

Exceptions
IOException:
on any I/O error
Since
1.3
setCanWrapLinesback to summary
protected void setCanWrapLines(boolean newValue)

Sets whether or not lines can be wrapped. This can be toggled during the writing of lines. For example, outputting HTML might set this to false when outputting a quoted string.

Parameters
newValue:boolean

new value for line wrapping

Since
1.3
setCurrentLineLengthback to summary
protected void setCurrentLineLength(int length)

Sets the current line length.

Parameters
length:int

the new line length

Since
1.3
setIndentSpaceback to summary
protected void setIndentSpace(int space)

Enables subclasses to specify how many spaces an indent maps to. When indentation takes place, the indent level is multiplied by this mapping. The default is 2.

Parameters
space:int

an int representing the space to indent mapping.

setLineLengthback to summary
protected void setLineLength(int l)

Enables subclasses to set the number of characters they want written per line. The default is 100.

Parameters
l:int

the maximum line length.

setLineSeparatorback to summary
public void setLineSeparator(String value)

Sets the String used to represent newlines. This is initialized in the constructor from either the Document, or the System property line.separator.

Parameters
value:String

the new line separator

Since
1.3
textback to summary
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.

Parameters
elem:Element

an Element.

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

This abstract method needs to be implemented by subclasses. Its responsibility is to iterate over the elements and use the write() methods to generate output in the desired format.

Exceptions
IOException:
if an I/O problem has occurred
BadLocationException:
for an invalid location within the document
writeback to summary
protected void write(char ch) throws IOException

Writes out a character. This is implemented to invoke the write method that takes a char[].

Parameters
ch:char

a char.

Exceptions
IOException:
on any I/O error
writeback to summary
protected void write(String content) throws IOException

Writes out a string. This is implemented to invoke the write method that takes a char[].

Parameters
content:String

a String.

Exceptions
IOException:
on any I/O error
writeback to summary
protected void write(char[] chars, int startIndex, int length) throws IOException

All write methods call into this one. If getCanWrapLines() returns false, this will call output with each sequence of chars that doesn't contain a NEWLINE, followed by a call to writeLineSeparator. On the other hand, if getCanWrapLines() returns true, this will split the string, as necessary, so getLineLength is honored. The only exception is if the current string contains no whitespace, and won't fit in which case the line length will exceed getLineLength.

Parameters
chars:char[]

characters to output

startIndex:int

starting index

length:int

length of output

Exceptions
IOException:
on any I/O error
Since
1.3
writeAttributesback to summary
protected void writeAttributes(AttributeSet attr) throws IOException

Writes out the set of attributes as " <name>=<value>" pairs. It throws an IOException when encountered.

Parameters
attr:AttributeSet

an AttributeSet.

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

Writes the line separator. This invokes output directly as well as setting the lineLength to 0.

Exceptions
IOException:
on any I/O error
Since
1.3