Top Description Fields Constructors Methods
com.ctc.wstx.sw

public abstract Class XmlWriter

extends Object
Class Inheritance
Known Direct Subclasses
com.ctc.wstx.sw.BufferingXmlWriter, com.ctc.wstx.sw.EncodingXmlWriter
Imports
java.io.IOException, .OutputStream, .Writer, java.text.MessageFormat, javax.xml.stream.XMLStreamException, org.codehaus.stax2.io.EscapingWriterFactory, org.codehaus.stax2.ri.typed.AsciiValueEncoder, org.codehaus.stax2.validation.XMLValidator, com.ctc.wstx.api.InvalidCharHandler, .WriterConfig, .WstxOutputProperties, com.ctc.wstx.cfg.ErrorConsts, .OutputConfigFlags, com.ctc.wstx.exc.WstxIOException, com.ctc.wstx.io.WstxInputData

This is the base class for actual physical xml outputters. These instances will only handle actual writing (possibly including encoding) of the serialized textual xml, and will in general not verify content being output. The exception are the character-by-character checks that are most efficiently done at encoding level (such as character escaping, and checks for illegal character combinations), which are handled at this level.

Note that implementations can have different operating modes: specifically, when dealing with illegal content (such as "--" in a comment, "?>" in processing instruction, or "]]>" within CDATA section), implementations can do one of 3 things:

Field Summary

Modifier and TypeField and Description
protected static final char
pack-priv final boolean
mAddSpaceAfterEmptyElem

Whether to add a space after empty element (before closing "/>") or not.

protected Writer
mAttrValueWriter

Optional escaping writer used for escaping characters like '"' '&' and '<' in attribute values.

protected final boolean
mAutoCloseOutput

Flag that defines whether close() on this writer should call close on the underlying output object (stream, writer)

protected final boolean
protected final boolean
protected final boolean
protected final WriterConfig
protected final String
pack-priv final boolean
mEscapeCR

Whether to escape CR (\r) character.

protected final boolean
protected int
mLocPastChars

Number of characters output prior to currently buffered output

protected int
protected int
mLocRowStartOffset

Offset of the first character on this line.

protected final boolean
protected XmlWriterWrapper
mRawWrapper

Lazy-constructed wrapper object, which will route all calls to Writer API, to matching writeRaw methods of this XmlWriter instance.

protected XmlWriterWrapper
mTextWrapper

Lazy-constructed wrapper object, which will route all calls to Writer API, to matching writeCharacters methods of this XmlWriter instance.

protected Writer
mTextWriter

Optional escaping writer used for escaping characters like '<' '&' and '>' in textual content.

pack-priv final boolean
mUseDoubleQuotesInXmlDecl

Whether to use double quotes in XML declaration or not.

protected boolean
mXml11

Indicates whether output is to be compliant; if false, is to be xml 1.0 compliant, if true, xml 1.1 compliant.

protected static final int
protected static final int
protected static final int
protected static final int

Constructor Summary

AccessConstructor and Description
protected
XmlWriter(WriterConfig cfg, String encoding, boolean autoclose)

Method Summary

Modifier and TypeMethod and Description
public abstract void
close(boolean forceRealClose)

Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.

public void
public abstract void
public int
public int
protected abstract int
protected abstract OutputStream

Returns:

Underlying OutputStream used for physical output, if the writer was constructed using one
getOutputStream
()

public int
protected abstract Writer

Returns:

Underlying Writer used for physical output, if the writer was constructed with one, or one was created to be used with an OutputStream.
getWriter
()

protected char
handleInvalidChar(int c)

Method called to handle invalid character in textual content requested to be output.

protected void
protected void
reportNwfName(String msg)

This is the method called when an output method call violates name well-formedness checks and WstxOutputProperties#P_OUTPUT_VALIDATE_NAMES is is enabled.

protected void
protected void
public final void
verifyNameValidity(String name, boolean checkNs)

Method called to verify that the name is a legal XML name.

public final Writer
wrapAsRawWriter()

Method that can be called to get a wrapper instance that can be used to essentially call the writeRaw method.

public final Writer
public abstract void
writeAttribute(String localName, String value)

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

public abstract void
writeAttribute(String localName, char[] value, int offset, int len)

public abstract void
writeAttribute(String prefix, String localName, String value)

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

public abstract void
writeAttribute(String prefix, String localName, char[] value, int offset, int len)

public abstract int

Returns:

offset of the (first) illegal content segment ("]]>") in passed content and not in repairing mode; or -1 if none or is repairing
writeCData
(String
Contents of the CDATA section to write out
data
)

public abstract int
writeCData(char[] cbuf, int offset, int len)

public abstract void
public abstract void
public abstract void
public abstract void
writeCharacters(char[] cbuf, int offset, int len)

public abstract int
writeComment(String data)

Method that will try to output the content as specified.

public abstract void
public abstract void
public abstract void
writeDTD(String data)

Older "legacy" output method for outputting DOCTYPE declaration.

public abstract void
writeDTD(String rootName, String systemId, String publicId, String internalSubset)

public abstract void
writeEndTag(String localName)

public abstract void
writeEndTag(String prefix, String localName)

public abstract void
public abstract int
writePI(String target, String data)

public abstract void
public abstract void
writePIStart(String target, boolean addSpace)

public abstract void
writeRaw(String str, int offset, int len)

public void
public abstract void
writeRaw(char[] cbuf, int offset, int len)

public abstract void
writeRawAscii(char[] cbuf, int offset, int len)

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range.

public abstract void
public abstract void
public abstract void
writeStartTagStart(String localName)

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

public abstract void
writeStartTagStart(String prefix, String localName)

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

public abstract void
writeTypedAttribute(String localName, AsciiValueEncoder enc)

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping.

public abstract void
writeTypedAttribute(String prefix, String localName, AsciiValueEncoder enc)

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping.

public abstract void
writeTypedAttribute(String prefix, String localName, String nsURI, AsciiValueEncoder enc, XMLValidator validator, char[] copyBuffer)

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping.

public abstract void
writeTypedElement(AsciiValueEncoder
Encoder that will produce content
enc
)

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.

public abstract void
writeTypedElement(AsciiValueEncoder
Encoder that will produce content
enc
,
XMLValidator
Validator to use for validating serialized textual content (can not be null)
validator
,
char[]
Temporary buffer that writer can use for temporary copies as necessary
copyBuffer
)

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.

public abstract void
writeXmlDeclaration(String version, String enc, String standalone)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_QUOTE_CHARback to summary
protected static final char DEFAULT_QUOTE_CHAR
mAddSpaceAfterEmptyElemback to summary
pack-priv final boolean mAddSpaceAfterEmptyElem

Whether to add a space after empty element (before closing "/>") or not.

mAttrValueWriterback to summary
protected Writer mAttrValueWriter

Optional escaping writer used for escaping characters like '"' '&' and '<' in attribute values. Constructed if calling code has installed a special escaping writer factory for text content. Null if the default escaper is to be used.

mAutoCloseOutputback to summary
protected final boolean mAutoCloseOutput

Flag that defines whether close() on this writer should call close on the underlying output object (stream, writer)

mCheckContentback to summary
protected final boolean mCheckContent
mCheckNamesback to summary
protected final boolean mCheckNames
mCheckStructureback to summary
protected final boolean mCheckStructure
mConfigback to summary
protected final WriterConfig mConfig
mEncodingback to summary
protected final String mEncoding
mEscapeCRback to summary
pack-priv final boolean mEscapeCR

Whether to escape CR (\r) character.

mFixContentback to summary
protected final boolean mFixContent
mLocPastCharsback to summary
protected int mLocPastChars

Number of characters output prior to currently buffered output

mLocRowNrback to summary
protected int mLocRowNr
mLocRowStartOffsetback to summary
protected int mLocRowStartOffset

Offset of the first character on this line. May be negative, if the offset was in a buffer that has been flushed out.

mNsAwareback to summary
protected final boolean mNsAware
mRawWrapperback to summary
protected XmlWriterWrapper mRawWrapper

Lazy-constructed wrapper object, which will route all calls to Writer API, to matching writeRaw methods of this XmlWriter instance.

mTextWrapperback to summary
protected XmlWriterWrapper mTextWrapper

Lazy-constructed wrapper object, which will route all calls to Writer API, to matching writeCharacters methods of this XmlWriter instance.

mTextWriterback to summary
protected Writer mTextWriter

Optional escaping writer used for escaping characters like '<' '&' and '>' in textual content. Constructed if calling code has installed a special escaping writer factory for text content. Null if the default escaper is to be used.

mUseDoubleQuotesInXmlDeclback to summary
pack-priv final boolean mUseDoubleQuotesInXmlDecl

Whether to use double quotes in XML declaration or not.

mXml11back to summary
protected boolean mXml11

Indicates whether output is to be compliant; if false, is to be xml 1.0 compliant, if true, xml 1.1 compliant.

SURR1_FIRSTback to summary
protected static final int SURR1_FIRST
SURR1_LASTback to summary
protected static final int SURR1_LAST
SURR2_FIRSTback to summary
protected static final int SURR2_FIRST
SURR2_LASTback to summary
protected static final int SURR2_LAST

Constructor Detail

XmlWriterback to summary
protected XmlWriter(WriterConfig cfg, String encoding, boolean autoclose) throws IOException

Method Detail

closeback to summary
public abstract void close(boolean forceRealClose) throws IOException

Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.

enableXml11back to summary
public void enableXml11()
flushback to summary
public abstract void flush() throws IOException
getAbsOffsetback to summary
public int getAbsOffset()
getColumnback to summary
public int getColumn()
getOutputPtrback to summary
protected abstract int getOutputPtr()
getOutputStreamback to summary
protected abstract OutputStream getOutputStream()
Returns:OutputStream

Underlying OutputStream used for physical output, if the writer was constructed using one

getRowback to summary
public int getRow()
getWriterback to summary
protected abstract Writer getWriter()
Returns:Writer

Underlying Writer used for physical output, if the writer was constructed with one, or one was created to be used with an OutputStream.

handleInvalidCharback to summary
protected char handleInvalidChar(int c) throws IOException

Method called to handle invalid character in textual content requested to be output. Content may be part of textual events (CHARACTER, CDATA), attribute value, COMMENT content or PROCESSING_INSTRUCTION data. The default behavior is to just throw an exception, but this can be configured via property WstxOutputProperties#P_OUTPUT_INVALID_CHAR_HANDLER.

reportNwfContentback to summary
protected void reportNwfContent(String msg) throws XMLStreamException
reportNwfNameback to summary
protected void reportNwfName(String msg) throws XMLStreamException

This is the method called when an output method call violates name well-formedness checks and WstxOutputProperties#P_OUTPUT_VALIDATE_NAMES is is enabled.

throwOutputErrorback to summary
protected void throwOutputError(String msg) throws XMLStreamException
throwOutputErrorback to summary
protected void throwOutputError(String format, Object arg) throws XMLStreamException
verifyNameValidityback to summary
public final void verifyNameValidity(String name, boolean checkNs) throws XMLStreamException

Method called to verify that the name is a legal XML name.

wrapAsRawWriterback to summary
public final Writer wrapAsRawWriter()

Method that can be called to get a wrapper instance that can be used to essentially call the writeRaw method.

wrapAsTextWriterback to summary
public final Writer wrapAsTextWriter()
writeAttributeback to summary
public abstract void writeAttribute(String localName, String value) throws IOException, XMLStreamException

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

writeAttributeback to summary
public abstract void writeAttribute(String localName, char[] value, int offset, int len) throws IOException, XMLStreamException
writeAttributeback to summary
public abstract void writeAttribute(String prefix, String localName, String value) throws IOException, XMLStreamException

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

writeAttributeback to summary
public abstract void writeAttribute(String prefix, String localName, char[] value, int offset, int len) throws IOException, XMLStreamException
writeCDataback to summary
public abstract int writeCData(String data) throws IOException, XMLStreamException
Parameters
data:String

Contents of the CDATA section to write out

Returns:int

offset of the (first) illegal content segment ("]]>") in passed content and not in repairing mode; or -1 if none or is repairing

writeCDataback to summary
public abstract int writeCData(char[] cbuf, int offset, int len) throws IOException, XMLStreamException
writeCDataEndback to summary
public abstract void writeCDataEnd() throws IOException
writeCDataStartback to summary
public abstract void writeCDataStart() throws IOException
writeCharactersback to summary
public abstract void writeCharacters(String data) throws IOException
writeCharactersback to summary
public abstract void writeCharacters(char[] cbuf, int offset, int len) throws IOException
writeCommentback to summary
public abstract int writeComment(String data) throws IOException, XMLStreamException

Method that will try to output the content as specified. If the content passed in has embedded "--" in it, it will either add an intervening space between consequtive hyphens (if content fixing is enabled), or return the offset of the first hyphen in multi-hyphen sequence.

writeCommentEndback to summary
public abstract void writeCommentEnd() throws IOException
writeCommentStartback to summary
public abstract void writeCommentStart() throws IOException
writeDTDback to summary
public abstract void writeDTD(String data) throws IOException, XMLStreamException

Older "legacy" output method for outputting DOCTYPE declaration. Assumes that the passed-in String contains a complete DOCTYPE declaration properly quoted.

writeDTDback to summary
public abstract void writeDTD(String rootName, String systemId, String publicId, String internalSubset) throws IOException, XMLStreamException
writeEndTagback to summary
public abstract void writeEndTag(String localName) throws IOException
writeEndTagback to summary
public abstract void writeEndTag(String prefix, String localName) throws IOException
writeEntityReferenceback to summary
public abstract void writeEntityReference(String name) throws IOException, XMLStreamException
writePIback to summary
public abstract int writePI(String target, String data) throws IOException, XMLStreamException
writePIEndback to summary
public abstract void writePIEnd() throws IOException
writePIStartback to summary
public abstract void writePIStart(String target, boolean addSpace) throws IOException
writeRawback to summary
public abstract void writeRaw(String str, int offset, int len) throws IOException
writeRawback to summary
public void writeRaw(String str) throws IOException
writeRawback to summary
public abstract void writeRaw(char[] cbuf, int offset, int len) throws IOException
writeRawAsciiback to summary
public abstract void writeRawAscii(char[] cbuf, int offset, int len) throws IOException

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range.

writeStartTagEmptyEndback to summary
public abstract void writeStartTagEmptyEnd() throws IOException
writeStartTagEndback to summary
public abstract void writeStartTagEnd() throws IOException
writeStartTagStartback to summary
public abstract void writeStartTagStart(String localName) throws IOException, XMLStreamException

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

writeStartTagStartback to summary
public abstract void writeStartTagStart(String prefix, String localName) throws IOException, XMLStreamException

Note

can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)

writeTypedAttributeback to summary
public abstract void writeTypedAttribute(String localName, AsciiValueEncoder enc) throws IOException, XMLStreamException

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping. No validation needs to be performed.

writeTypedAttributeback to summary
public abstract void writeTypedAttribute(String prefix, String localName, AsciiValueEncoder enc) throws IOException, XMLStreamException

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping. No validation needs to be performed.

writeTypedAttributeback to summary
public abstract void writeTypedAttribute(String prefix, String localName, String nsURI, AsciiValueEncoder enc, XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException

Method similar to writeAttribute(String, String, char[], int, int) but where is known not to require escaping. Validation of the attribute value must be done by calling given validator appropriately.

writeTypedElementback to summary
public abstract void writeTypedElement(AsciiValueEncoder enc) throws IOException

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.

Parameters
enc:AsciiValueEncoder

Encoder that will produce content

writeTypedElementback to summary
public abstract void writeTypedElement(AsciiValueEncoder enc, XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException

Like writeRaw, but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.

Parameters
enc:AsciiValueEncoder

Encoder that will produce content

validator:XMLValidator

Validator to use for validating serialized textual content (can not be null)

copyBuffer:char[]

Temporary buffer that writer can use for temporary copies as necessary

writeXmlDeclarationback to summary
public abstract void writeXmlDeclaration(String version, String enc, String standalone) throws IOException