/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* 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
*/
public abstract class AbstractWriter {
// If (indentLevel * indentSpace) becomes >= maxLineLength, this will
// get incremened instead of indentLevel to avoid indenting going greater
// than line length.
/**
* 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.
*/
/**
* 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 boolean canWrapLines;
/**
* True while the current line is empty. This will remain true after
* indenting.
*/
private boolean isLineEmpty;
/**
* Used when indenting. Will contain the spaces.
*/
private char[] indentChars;
/**
* Used when writing out a string.
*/
private char[] tempChars;
/**
* This is used in <code>writeLineSeparator</code> instead of
* tempChars. If tempChars were used it would mean write couldn't invoke
* <code>writeLineSeparator</code> as it might have been passed
* tempChars.
*/
private char[] newlineChars;
/**
* Used for writing text.
*/
/**
* How the text packages models newlines.
* @see #getLineSeparator
*/
/**
* Creates a new AbstractWriter.
* Initializes the ElementIterator with the default
* root of the document.
*
* @param w a Writer.
* @param doc a Document
*/
}
/**
* Creates a new AbstractWriter.
* Initializes the ElementIterator with the
* element passed in.
*
* @param w a Writer
* @param doc an Element
* @param pos The location in the document to fetch the
* content.
* @param len The amount to write out.
*/
out = w;
startOffset = pos;
if (docNewline instanceof String) {
}
else {
try {
} catch (SecurityException se) {}
// Should not get here, but if we do it means we could not
// find a newline string, use \n in this case.
newline = "\n";
}
}
canWrapLines = true;
}
/**
* Creates a new AbstractWriter.
* Initializes the ElementIterator with the
* element passed in.
*
* @param w a Writer
* @param root an Element
*/
}
/**
* Creates a new AbstractWriter.
* Initializes the ElementIterator with the
* element passed in.
*
* @param w a Writer
* @param root an Element
* @param pos The location in the document to fetch the
* content.
* @param len The amount to write out.
*/
out = w;
startOffset = pos;
canWrapLines = true;
}
/**
* Returns the first offset to be output.
*
* @since 1.3
*/
public int getStartOffset() {
return startOffset;
}
/**
* Returns the last offset to be output.
*
* @since 1.3
*/
public int getEndOffset() {
return endOffset;
}
/**
* Fetches the ElementIterator.
*
* @return the ElementIterator.
*/
return it;
}
/**
* Returns the Writer that is used to output the content.
*
* @since 1.3
*/
return out;
}
/**
* Fetches the document.
*
* @return the Document.
*/
return doc;
}
/**
* 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.
*
* @param next an Element.
* @return boolean that indicates whether the element
* is in the range.
*/
int startOffset = getStartOffset();
int endOffset = getEndOffset();
return true;
}
return false;
}
/**
* 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.
*/
/**
* Returns the text associated with the element.
* The assumption here is that the element is a
* leaf element. Throws a BadLocationException
* when encountered.
*
* @param elem an <code>Element</code>
* @exception BadLocationException if pos represents an invalid
* location within the document
* @return the text as a <code>String</code>
*/
}
/**
* Writes out text. If a range is specified when the constructor
* is invoked, then only the appropriate range of text is written
* out.
*
* @param elem an Element.
* @exception IOException on any I/O error
* @exception BadLocationException if pos represents an invalid
* location within the document.
*/
}
}
}
}
/**
* Enables subclasses to set the number of characters they
* want written per line. The default is 100.
*
* @param l the maximum line length.
*/
protected void setLineLength(int l) {
maxLineLength = l;
}
/**
* Returns the maximum line length.
*
* @since 1.3
*/
protected int getLineLength() {
return maxLineLength;
}
/**
* Sets the current line length.
*
* @since 1.3
*/
currLength = length;
}
/**
* Returns the current line length.
*
* @since 1.3
*/
protected int getCurrentLineLength() {
return currLength;
}
/**
* Returns true if the current line should be considered empty. This
* is true when <code>getCurrentLineLength</code> == 0 ||
* <code>indent</code> has been invoked on an empty line.
*
* @since 1.3
*/
protected boolean isLineEmpty() {
return isLineEmpty;
}
/**
* 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.
*
* @since 1.3
*/
}
/**
* Returns whether or not the lines can be wrapped. If this is false
* no lineSeparator's will be output.
*
* @since 1.3
*/
protected boolean getCanWrapLines() {
return canWrapLines;
}
/**
* 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.
*
* @param space an int representing the space to indent mapping.
*/
indentSpace = space;
}
/**
* Returns the amount of space to indent.
*
* @since 1.3
*/
protected int getIndentSpace() {
return indentSpace;
}
/**
* Sets the String used to reprsent newlines. This is initialized
* in the constructor from either the Document, or the System property
* line.separator.
*
* @since 1.3
*/
}
/**
* Returns the string used to represent newlines.
*
* @since 1.3
*/
return lineSeparator;
}
/**
* Increments the indent level. If indenting would cause
* <code>getIndentSpace()</code> *<code>getIndentLevel()</code> to be >
* than <code>getLineLength()</code> this will not cause an indent.
*/
protected void incrIndent() {
// Only increment to a certain point.
if (offsetIndent > 0) {
offsetIndent++;
}
else {
offsetIndent++;
--indentLevel;
}
}
}
/**
* Decrements the indent level.
*/
protected void decrIndent() {
if (offsetIndent > 0) {
--offsetIndent;
}
else {
indentLevel--;
}
}
/**
* Returns the current indentation level. That is, the number of times
* <code>incrIndent</code> has been invoked minus the number of times
* <code>decrIndent</code> has been invoked.
*
* @since 1.3
*/
protected int getIndentLevel() {
return indentLevel;
}
/**
* 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.
*
* @exception IOException on any I/O error
*/
indentChars = new char[max];
}
}
int length = getCurrentLineLength();
boolean wasEmpty = isLineEmpty();
isLineEmpty = true;
}
}
/**
* Writes out a character. This is implemented to invoke
* the <code>write</code> method that takes a char[].
*
* @param ch a char.
* @exception IOException on any I/O error
*/
tempChars = new char[128];
}
}
/**
* Writes out a string. This is implemented to invoke the
* <code>write</code> method that takes a char[].
*
* @param content a String.
* @exception IOException on any I/O error
*/
return;
}
}
}
/**
* Writes the line separator. This invokes <code>output</code> directly
* as well as setting the <code>lineLength</code> to 0.
*
* @since 1.3
*/
newlineChars = new char[length];
}
}
/**
* All write methods call into this one. If <code>getCanWrapLines()</code>
* returns false, this will call <code>output</code> with each sequence
* of <code>chars</code> that doesn't contain a NEWLINE, followed
* by a call to <code>writeLineSeparator</code>. On the other hand,
* if <code>getCanWrapLines()</code> returns true, this will split the
* string, as necessary, so <code>getLineLength</code> 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
* <code>getLineLength</code>.
*
* @since 1.3
*/
throws IOException {
if (!getCanWrapLines()) {
// We can not break string, just track if a newline
// is in it.
int lastIndex = startIndex;
while (newlineIndex != -1) {
if (newlineIndex > lastIndex) {
}
}
}
}
else {
// We can break chars if the length exceeds maxLength.
int lastIndex = startIndex;
int lineLength = getCurrentLineLength();
int maxLength = getLineLength();
endIndex);
boolean needsNewline = false;
boolean forceNewLine = false;
if (newlineIndex > lastIndex) {
}
forceNewLine = true;
}
}
}
else {
// Need to break chars, find a place to split chars at,
// from lastIndex to endIndex,
// or maxLength - lineLength whichever is smaller
int breakPoint = -1;
int counter = 0;
lastIndex])) {
}
counter++;
}
if (breakPoint != -1) {
// Found a place to break at.
needsNewline = true;
}
else {
// No where good to break.
// find the next whitespace, or write out the
// whole string.
// maxBreak will be negative if current line too
// long.
lastIndex])) {
break;
}
counter++;
}
if (breakPoint == -1) {
}
else {
breakPoint += lastIndex;
forceNewLine = true;
}
else {
needsNewline = true;
}
}
}
}
indent();
}
}
}
}
}
/**
* Writes out the set of attributes as " <name>=<value>"
* pairs. It throws an IOException when encountered.
*
* @param attr an AttributeSet.
* @exception IOException on any I/O error
*/
while (names.hasMoreElements()) {
}
}
/**
* The last stop in writing out content. All the write methods eventually
* make it to this method, which invokes <code>write</code> on the
* Writer.
* <p>This method also updates the line length based on
* <code>length</code>. 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
* <code>writeLineSeparator</code> to write out a newline, which will
* property update the current line length.
*
* @since 1.3
*/
throws IOException {
}
/**
* Support method to locate an occurence of a particular character.
*/
int endIndex) {
while(startIndex < endIndex) {
return startIndex;
}
startIndex++;
}
return -1;
}
}