org.jvoicexml.xml
Class XmlWriter

java.lang.Object
  extended by java.io.Writer
      extended by org.jvoicexml.xml.XmlWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable
Direct Known Subclasses:
XmlStringWriter

public class XmlWriter
extends Writer

This writer captures context used when writing XML text, such as state used to "pretty print" output or to identify entities which are defined. Pretty printing is useful when displaying structure in XML documents that need to be read or edited by people (rather than only by machines).

Version:
$Revision: 154 $

Copyright © 2005-2006 JVoiceXML group - http://jvoicexml.sourceforge.net/

Author:
David Brownell, Dirk Schnelle
See Also:
XmlWritable

Field Summary
static int DEFAULT_BLOCK_INDENT
          Default numberofspaces for block indent.
static String EOL
          The end of line terminator retrieved by evaluating the system property line.separator.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
XmlWriter(Writer out)
          Constructs an xml writethat doesn't pretty-print output.
XmlWriter(Writer out, int spaces)
          Constructs an xml write that supports pretty-printing output with the given number of spaces for block indentation.
 
Method Summary
 void close()
          Close the stream, flushing it first.
 void decIndentLevel()
          Decrease the indent level by 1, does nothing if the indent level is 0.
 void flush()
          Flush the stream.
 int getBlockSpaces()
          Returns the number of spaces per indent level, for use in pretty printing XML text.
 int getIndentLevel()
          Returns the current indent level, in terms of blocks, for use in pretty printing XML text.
 Writer getWriter()
          Returns the writer to which output should be written.
 void incIndentLevel()
          Increase the indent level by 1.
 boolean isEntityDeclared(String name)
          Returns true if the specified entity was already declared in this output context, so that entity references may be written rather than their expanded values.
 boolean isPrettyOutput()
          Returns true if writes using the context should "pretty print", displaying structure through indentation as appropriate.
 void printIndent()
          If pretty printing is enabled, this writes a newline followed by indentLevel spaces.
 void setBlockSpaces(int spaces)
          Assigns the number of spaces per indent level, for use in pretty printing XML text.
 void setIndentLevel(int level)
          Assigns the current indent level, in terms of blocks, for use in pretty printing XML text.
 void write(char[] cbuf)
          Write an array of characters.
 void write(char[] cbuf, int off, int len)
          Write a portion of an array of characters.
 void write(int c)
          Write a single character.
 void write(String str)
          Write a string.
 void write(String str, int off, int len)
          Write a portion of a string.
 void writeAttribute(String name, String value)
          Writes a attribute in the form of name="value".
 void writeHeader()
          Write a standard header for an XML document.
 
Methods inherited from class java.io.Writer
append, append, append
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOL

public static final String EOL
The end of line terminator retrieved by evaluating the system property line.separator.


DEFAULT_BLOCK_INDENT

public static final int DEFAULT_BLOCK_INDENT
Default numberofspaces for block indent.

See Also:
Constant Field Values
Constructor Detail

XmlWriter

public XmlWriter(Writer out)
Constructs an xml writethat doesn't pretty-print output.

Parameters:
out - Writer to which output should be written.

XmlWriter

public XmlWriter(Writer out,
                 int spaces)
Constructs an xml write that supports pretty-printing output with the given number of spaces for block indentation.

Parameters:
out - Writer to which output should be written.
spaces - Number of spaces for blocks, for use in pretty printing XML text.
Method Detail

getWriter

public final Writer getWriter()
Returns the writer to which output should be written.

Returns:
Writer to which output should be written.

isEntityDeclared

public final boolean isEntityDeclared(String name)
Returns true if the specified entity was already declared in this output context, so that entity references may be written rather than their expanded values. The predefined XML entities are always declared.

Parameters:
name - Name of the entity.
Returns:
true if the specified entity wasalready declared.

getBlockSpaces

public final int getBlockSpaces()
Returns the number of spaces per indent level, for use in pretty printing XML text.

Returns:
Number of spaces.

setBlockSpaces

public final void setBlockSpaces(int spaces)
Assigns the number of spaces per indent level, for use in pretty printing XML text.

Parameters:
spaces - Number of spaces.

getIndentLevel

public final int getIndentLevel()
Returns the current indent level, in terms of blocks, for use in pretty printing XML text.

Returns:
Current indent level.

setIndentLevel

public final void setIndentLevel(int level)
Assigns the current indent level, in terms of blocks, for use in pretty printing XML text.

Parameters:
level - New indent level.

incIndentLevel

public final void incIndentLevel()
Increase the indent level by 1.


decIndentLevel

public final void decIndentLevel()
Decrease the indent level by 1, does nothing if the indent level is 0.


printIndent

public final void printIndent()
                       throws IOException
If pretty printing is enabled, this writes a newline followed by indentLevel spaces. At the beginning of a line, groups of eight consecutive spaces are replaced by tab characters, for storage efficiency.

Note that this method should not be used except in cases where the additional whitespace is guaranteed to be semantically meaningless. This is the default, and is controlled through the xml:space attribute, inherited from parent elements. When this attribute value is preserve, this method should not be used. Otherwise, text normalization is expected to remove excess whitespace such as that added by this call.

Throws:
IOException - Error writing to the writer.

isPrettyOutput

public final boolean isPrettyOutput()
Returns true if writes using the context should "pretty print", displaying structure through indentation as appropriate.

Returns:
true if writes should pretty print

close

public final void close()
                 throws IOException
Close the stream, flushing it first.

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException - If an I/O error occurs

flush

public final void flush()
                 throws IOException
Flush the stream.

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer
Throws:
IOException - If an I/O error occurs

write

public final void write(String str)
                 throws IOException
Write a string.

Overrides:
write in class Writer
Parameters:
str - String to be written
Throws:
IOException - If an I/O error occurs

write

public final void write(String str,
                        int off,
                        int len)
                 throws IOException
Write a portion of a string.

Overrides:
write in class Writer
Parameters:
str - A String
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs

write

public final void write(char[] cbuf)
                 throws IOException
Write an array of characters.

Overrides:
write in class Writer
Parameters:
cbuf - Array of characters to be written
Throws:
IOException - If an I/O error occurs

write

public final void write(char[] cbuf,
                        int off,
                        int len)
                 throws IOException
Write a portion of an array of characters.

Specified by:
write in class Writer
Parameters:
cbuf - Array of characters
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs

write

public final void write(int c)
                 throws IOException
Write a single character.

Overrides:
write in class Writer
Parameters:
c - int specifying a character to be written.
Throws:
IOException - If an I/O error occurs

writeHeader

public final void writeHeader()
                       throws IOException
Write a standard header for an XML document.

Throws:
IOException - Error writing to the writer
IOException - Error writing to the writer.

writeAttribute

public final void writeAttribute(String name,
                                 String value)
                          throws IOException
Writes a attribute in the form of name="value". A leading space is always written before the name. Does nothing if either the name of the attribute or its value is null.

Parameters:
name - Name of the attribute.
value - Value of the attribute.
Throws:
IOException - Error writing to the writer.


Copyright © 2005-2007 JVoiceXML group.