Class EncodingXmlWriter
- java.lang.Object
-
- com.ctc.wstx.sw.XmlWriter
-
- com.ctc.wstx.sw.EncodingXmlWriter
-
- Direct Known Subclasses:
AsciiXmlWriter
,ISOLatin1XmlWriter
public abstract class EncodingXmlWriter extends XmlWriter
Intermediate base class used when outputting to streams that use an encoding that is compatible with 7-bit single-byte Ascii encoding. That means it can be used for UTF-8, ISO-Latin1 and pure Ascii.Implementation notes:
Parts of surrogate handling are implemented here in the base class: storage for the first part of a split surrogate (only possible when character content is output split in multiple calls) is within base class. Also, simple checks for unmatched surrogate pairs are in
writeAscii
method, since it is the most convenient place to catch cases where a text segment ends with an unmatched surrogate pair half.
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
mOutputBuffer
protected int
mOutputPtr
protected int
mSurrogate
In case a split surrogate pair is output (which can only successfully occur with eitherwriteRaw
orwriteCharacters
), the first part is temporarily stored within this member variable.-
Fields inherited from class com.ctc.wstx.sw.XmlWriter
DEFAULT_QUOTE_CHAR, mAttrValueWriter, mAutoCloseOutput, mCheckContent, mCheckNames, mCheckStructure, mConfig, mEncoding, mFixContent, mLocPastChars, mLocRowNr, mLocRowStartOffset, mNsAware, mRawWrapper, mTextWrapper, mTextWriter, mXml11, SURR1_FIRST, SURR1_LAST, SURR2_FIRST, SURR2_LAST
-
-
Constructor Summary
Constructors Constructor Description EncodingXmlWriter(OutputStream out, WriterConfig cfg, String encoding, boolean autoclose)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected int
calcSurrogate(int secondSurr)
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.void
flush()
protected void
flushBuffer()
protected int
getOutputPtr()
This method is needed by the super class, to calculate hard byte/char offsets.protected OutputStream
getOutputStream()
protected Writer
getWriter()
protected void
throwUnpairedSurrogate()
protected void
throwUnpairedSurrogate(int code)
protected void
writeAscii(byte b)
protected void
writeAscii(byte b1, byte b2)
protected void
writeAscii(String str)
protected int
writeAsEntity(int c)
Entity writing can be optimized quite nicely, since it only needs to output ascii characters.void
writeAttribute(String localName, char[] value, int offset, int len)
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)void
writeAttribute(String prefix, String localName, char[] value, int offset, int len)
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)protected abstract void
writeAttrValue(char[] value, int offset, int len)
protected abstract void
writeAttrValue(String data)
int
writeCData(char[] cbuf, int offset, int len)
int
writeCData(String data)
protected abstract int
writeCDataContent(char[] cbuf, int start, int len)
protected abstract int
writeCDataContent(String data)
void
writeCDataEnd()
void
writeCDataStart()
void
writeCharacters(char[] cbuf, int offset, int len)
void
writeCharacters(String data)
int
writeComment(String data)
Method that will try to output the content as specified.protected abstract int
writeCommentContent(String data)
void
writeCommentEnd()
void
writeCommentStart()
void
writeDTD(String data)
Older "legacy" output method for outputting DOCTYPE declaration.void
writeDTD(String rootName, String systemId, String publicId, String internalSubset)
void
writeEndTag(String localName)
void
writeEndTag(String prefix, String localName)
void
writeEntityReference(String name)
protected void
writeName(String name)
protected void
writeNameUnchecked(String name)
int
writePI(String target, String data)
protected abstract int
writePIData(String data)
void
writePIEnd()
void
writePIStart(String target, boolean addSpace)
abstract void
writeRaw(char[] cbuf, int offset, int len)
abstract void
writeRaw(String str, int offset, int len)
void
writeRawAscii(char[] buf, int offset, int len)
LikeXmlWriter.writeRaw(java.lang.String, int, int)
, but caller guarantees that the contents additionally are known to be in 7-bit ascii range.void
writeStartTagEmptyEnd()
void
writeStartTagEnd()
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)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)protected abstract void
writeTextContent(char[] cbuf, int start, int len)
protected abstract void
writeTextContent(String data)
void
writeTypedAttribute(String prefix, String localName, String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer)
Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)
but where is known not to require escaping.void
writeTypedAttribute(String prefix, String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)
Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)
but where is known not to require escaping.void
writeTypedAttribute(String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)
Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)
but where is known not to require escaping.void
writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)
Non-validating version of typed write methodvoid
writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer)
Validating version of typed write methodvoid
writeXmlDeclaration(String version, String encoding, String standalone)
-
Methods inherited from class com.ctc.wstx.sw.XmlWriter
enableXml11, getAbsOffset, getColumn, getRow, handleInvalidChar, reportNwfContent, reportNwfName, throwOutputError, throwOutputError, verifyNameValidity, wrapAsRawWriter, wrapAsTextWriter, writeRaw
-
-
-
-
Field Detail
-
mOutputBuffer
protected byte[] mOutputBuffer
-
mOutputPtr
protected int mOutputPtr
-
mSurrogate
protected int mSurrogate
In case a split surrogate pair is output (which can only successfully occur with eitherwriteRaw
orwriteCharacters
), the first part is temporarily stored within this member variable.
-
-
Constructor Detail
-
EncodingXmlWriter
public EncodingXmlWriter(OutputStream out, WriterConfig cfg, String encoding, boolean autoclose) throws IOException
- Throws:
IOException
-
-
Method Detail
-
getOutputPtr
protected int getOutputPtr()
This method is needed by the super class, to calculate hard byte/char offsets.- Specified by:
getOutputPtr
in classXmlWriter
-
getOutputStream
protected final OutputStream getOutputStream()
- Specified by:
getOutputStream
in classXmlWriter
- Returns:
- Underlying OutputStream used for physical output, if the writer was constructed using one
-
getWriter
protected final Writer getWriter()
-
close
public void close(boolean forceRealClose) throws IOException
Description copied from class:XmlWriter
Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.- Specified by:
close
in classXmlWriter
- Throws:
IOException
-
flush
public final void flush() throws IOException
- Specified by:
flush
in classXmlWriter
- Throws:
IOException
-
writeRaw
public abstract void writeRaw(char[] cbuf, int offset, int len) throws IOException
- Specified by:
writeRaw
in classXmlWriter
- Throws:
IOException
-
writeRaw
public abstract void writeRaw(String str, int offset, int len) throws IOException
- Specified by:
writeRaw
in classXmlWriter
- Throws:
IOException
-
writeCDataStart
public final void writeCDataStart() throws IOException
- Specified by:
writeCDataStart
in classXmlWriter
- Throws:
IOException
-
writeCDataEnd
public final void writeCDataEnd() throws IOException
- Specified by:
writeCDataEnd
in classXmlWriter
- Throws:
IOException
-
writeCommentStart
public final void writeCommentStart() throws IOException
- Specified by:
writeCommentStart
in classXmlWriter
- Throws:
IOException
-
writeCommentEnd
public final void writeCommentEnd() throws IOException
- Specified by:
writeCommentEnd
in classXmlWriter
- Throws:
IOException
-
writePIStart
public final void writePIStart(String target, boolean addSpace) throws IOException
- Specified by:
writePIStart
in classXmlWriter
- Throws:
IOException
-
writePIEnd
public final void writePIEnd() throws IOException
- Specified by:
writePIEnd
in classXmlWriter
- Throws:
IOException
-
writeCData
public int writeCData(String data) throws IOException
- Specified by:
writeCData
in classXmlWriter
- Parameters:
data
- Contents of the CDATA section to write out- Returns:
- offset of the (first) illegal content segment ("]]>") in passed content and not in repairing mode; or -1 if none or is repairing
- Throws:
IOException
-
writeCData
public int writeCData(char[] cbuf, int offset, int len) throws IOException
- Specified by:
writeCData
in classXmlWriter
- Throws:
IOException
-
writeCharacters
public final void writeCharacters(String data) throws IOException
- Specified by:
writeCharacters
in classXmlWriter
- Throws:
IOException
-
writeCharacters
public final void writeCharacters(char[] cbuf, int offset, int len) throws IOException
- Specified by:
writeCharacters
in classXmlWriter
- Throws:
IOException
-
writeComment
public int writeComment(String data) throws IOException
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.- Specified by:
writeComment
in classXmlWriter
- Throws:
IOException
-
writeDTD
public void writeDTD(String data) throws IOException
Description copied from class:XmlWriter
Older "legacy" output method for outputting DOCTYPE declaration. Assumes that the passed-in String contains a complete DOCTYPE declaration properly quoted.- Specified by:
writeDTD
in classXmlWriter
- Throws:
IOException
-
writeDTD
public void writeDTD(String rootName, String systemId, String publicId, String internalSubset) throws IOException, XMLStreamException
- Specified by:
writeDTD
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeEntityReference
public void writeEntityReference(String name) throws IOException, XMLStreamException
- Specified by:
writeEntityReference
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeXmlDeclaration
public void writeXmlDeclaration(String version, String encoding, String standalone) throws IOException
- Specified by:
writeXmlDeclaration
in classXmlWriter
- Throws:
IOException
-
writePI
public int writePI(String target, String data) throws IOException, XMLStreamException
- Specified by:
writePI
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeStartTagStart
public void writeStartTagStart(String localName) throws IOException, XMLStreamException
Description copied from class:XmlWriter
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)
- Specified by:
writeStartTagStart
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeStartTagStart
public void writeStartTagStart(String prefix, String localName) throws IOException, XMLStreamException
Description copied from class:XmlWriter
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)
- Specified by:
writeStartTagStart
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeStartTagEnd
public void writeStartTagEnd() throws IOException
- Specified by:
writeStartTagEnd
in classXmlWriter
- Throws:
IOException
-
writeStartTagEmptyEnd
public void writeStartTagEmptyEnd() throws IOException
- Specified by:
writeStartTagEmptyEnd
in classXmlWriter
- Throws:
IOException
-
writeEndTag
public void writeEndTag(String localName) throws IOException
- Specified by:
writeEndTag
in classXmlWriter
- Throws:
IOException
-
writeEndTag
public void writeEndTag(String prefix, String localName) throws IOException
- Specified by:
writeEndTag
in classXmlWriter
- Throws:
IOException
-
writeAttribute
public void writeAttribute(String localName, String value) throws IOException, XMLStreamException
Description copied from class:XmlWriter
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)
- Specified by:
writeAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeAttribute
public void writeAttribute(String localName, char[] value, int offset, int len) throws IOException, XMLStreamException
- Specified by:
writeAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeAttribute
public void writeAttribute(String prefix, String localName, String value) throws IOException, XMLStreamException
Description copied from class:XmlWriter
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)
- Specified by:
writeAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeAttribute
public void writeAttribute(String prefix, String localName, char[] value, int offset, int len) throws IOException, XMLStreamException
- Specified by:
writeAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException
Non-validating version of typed write method- Specified by:
writeTypedElement
in classXmlWriter
- Parameters:
enc
- Encoder that will produce content- Throws:
IOException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException
Validating version of typed write method- Specified by:
writeTypedElement
in classXmlWriter
- Parameters:
enc
- Encoder that will produce contentvalidator
- Validator to use for validating serialized textual content (can not be null)copyBuffer
- Temporary buffer that writer can use for temporary copies as necessary- Throws:
IOException
XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException
Description copied from class:XmlWriter
Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)
but where is known not to require escaping. No validation needs to be performed.- Specified by:
writeTypedAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String prefix, String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException
Description copied from class:XmlWriter
Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)
but where is known not to require escaping. No validation needs to be performed.- Specified by:
writeTypedAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String prefix, String localName, String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException
Description copied from class:XmlWriter
Method similar toXmlWriter.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.- Specified by:
writeTypedAttribute
in classXmlWriter
- Throws:
IOException
XMLStreamException
-
flushBuffer
protected final void flushBuffer() throws IOException
- Throws:
IOException
-
writeAscii
protected final void writeAscii(byte b) throws IOException
- Throws:
IOException
-
writeAscii
protected final void writeAscii(byte b1, byte b2) throws IOException
- Throws:
IOException
-
writeAscii
protected final void writeAscii(String str) throws IOException
- Throws:
IOException
-
writeRawAscii
public final void writeRawAscii(char[] buf, int offset, int len) throws IOException
Description copied from class:XmlWriter
LikeXmlWriter.writeRaw(java.lang.String, int, int)
, but caller guarantees that the contents additionally are known to be in 7-bit ascii range.- Specified by:
writeRawAscii
in classXmlWriter
- Throws:
IOException
-
writeAsEntity
protected final int writeAsEntity(int c) throws IOException
Entity writing can be optimized quite nicely, since it only needs to output ascii characters.- Returns:
- New value of
mOutputPtr
- Throws:
IOException
-
writeName
protected final void writeName(String name) throws IOException, XMLStreamException
- Throws:
IOException
XMLStreamException
-
writeNameUnchecked
protected final void writeNameUnchecked(String name) throws IOException
- Throws:
IOException
-
calcSurrogate
protected final int calcSurrogate(int secondSurr) throws IOException
- Throws:
IOException
-
throwUnpairedSurrogate
protected final void throwUnpairedSurrogate() throws IOException
- Throws:
IOException
-
throwUnpairedSurrogate
protected final void throwUnpairedSurrogate(int code) throws IOException
- Throws:
IOException
-
writeAttrValue
protected abstract void writeAttrValue(String data) throws IOException
- Throws:
IOException
-
writeAttrValue
protected abstract void writeAttrValue(char[] value, int offset, int len) throws IOException
- Throws:
IOException
-
writeCDataContent
protected abstract int writeCDataContent(String data) throws IOException
- Throws:
IOException
-
writeCDataContent
protected abstract int writeCDataContent(char[] cbuf, int start, int len) throws IOException
- Throws:
IOException
-
writeCommentContent
protected abstract int writeCommentContent(String data) throws IOException
- Throws:
IOException
-
writePIData
protected abstract int writePIData(String data) throws IOException, XMLStreamException
- Throws:
IOException
XMLStreamException
-
writeTextContent
protected abstract void writeTextContent(String data) throws IOException
- Throws:
IOException
-
writeTextContent
protected abstract void writeTextContent(char[] cbuf, int start, int len) throws IOException
- Throws:
IOException
-
-