Class XmlWriter
- Direct Known Subclasses:
BufferingXmlWriter, EncodingXmlWriter
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:
- Fix the problem, by splitting the section (which can be done for CDATA sections, and to some degree, comments)
- Stop outputting, and return an index to the illegal piece of data (if there is no easy way to fix the problem: for example, for processing instruction)
- Just output content even though it will not result in well-formed output. This should only be done if the calling application has specifically requested verifications to be disabled.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final char(package private) final booleanWhether to add a space after empty element (before closing "/>") or not.protected WriterOptional escaping writer used for escaping characters like '"' '&' and '<' in attribute values.protected final booleanFlag that defines whether close() on this writer should call close on the underlying output object (stream, writer)protected final booleanprotected final booleanprotected final booleanprotected final WriterConfigprotected final String(package private) final booleanWhether to escape CR (\r) character.protected final booleanprotected intNumber of characters output prior to currently buffered outputprotected intprotected intOffset of the first character on this line.protected final booleanprotected XmlWriterWrapperLazy-constructed wrapper object, which will route all calls to Writer API, to matchingwriteRawmethods of this XmlWriter instance.protected XmlWriterWrapperLazy-constructed wrapper object, which will route all calls to Writer API, to matchingwriteCharactersmethods of this XmlWriter instance.protected WriterOptional escaping writer used for escaping characters like '<' '&' and '>' in textual content.(package private) final booleanWhether to use double quotes in XML declaration or not.protected booleanIndicates whether output is to be compliant; if false, is to be xml 1.0 compliant, if true, xml 1.1 compliant.protected static final intprotected static final intprotected static final intprotected static final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedXmlWriter(WriterConfig cfg, String encoding, boolean autoclose) -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose(boolean forceRealClose) Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.voidabstract voidflush()intintprotected abstract intprotected abstract OutputStreamintgetRow()protected abstract Writerprotected charhandleInvalidChar(int c) Method called to handle invalid character in textual content requested to be output.protected voidreportNwfContent(String msg) protected voidreportNwfName(String msg) This is the method called when an output method call violates name well-formedness checks andWstxOutputProperties.P_OUTPUT_VALIDATE_NAMESis is enabled.protected voidthrowOutputError(String msg) protected voidthrowOutputError(String format, Object arg) final voidverifyNameValidity(String name, boolean checkNs) Method called to verify that the name is a legal XML name.final WriterMethod that can be called to get a wrapper instance that can be used to essentially call thewriteRawmethod.final Writerabstract voidwriteAttribute(String localName, char[] value, int offset, int len) abstract voidwriteAttribute(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)abstract voidwriteAttribute(String prefix, String localName, char[] value, int offset, int len) abstract voidwriteAttribute(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)abstract intwriteCData(char[] cbuf, int offset, int len) abstract intwriteCData(String data) abstract voidabstract voidabstract voidwriteCharacters(char[] cbuf, int offset, int len) abstract voidwriteCharacters(String data) abstract intwriteComment(String data) Method that will try to output the content as specified.abstract voidabstract voidabstract voidOlder "legacy" output method for outputting DOCTYPE declaration.abstract voidabstract voidwriteEndTag(String localName) abstract voidwriteEndTag(String prefix, String localName) abstract voidwriteEntityReference(String name) abstract intabstract voidabstract voidwritePIStart(String target, boolean addSpace) abstract voidwriteRaw(char[] cbuf, int offset, int len) voidabstract voidabstract voidwriteRawAscii(char[] cbuf, int offset, int len) LikewriteRaw(String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range.abstract voidabstract voidabstract voidwriteStartTagStart(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)abstract voidwriteStartTagStart(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)abstract voidwriteTypedAttribute(String prefix, String localName, String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) Method similar towriteAttribute(String,String,char[],int,int)but where is known not to require escaping.abstract voidwriteTypedAttribute(String prefix, String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) Method similar towriteAttribute(String,String,char[],int,int)but where is known not to require escaping.abstract voidwriteTypedAttribute(String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) Method similar towriteAttribute(String,String,char[],int,int)but where is known not to require escaping.abstract voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) LikewriteRaw(String, int, int), 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.abstract voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) LikewriteRaw(String, int, int), 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.abstract voidwriteXmlDeclaration(String version, String enc, String standalone)
-
Field Details
-
SURR1_FIRST
protected static final int SURR1_FIRST- See Also:
-
SURR1_LAST
protected static final int SURR1_LAST- See Also:
-
SURR2_FIRST
protected static final int SURR2_FIRST- See Also:
-
SURR2_LAST
protected static final int SURR2_LAST- See Also:
-
DEFAULT_QUOTE_CHAR
protected static final char DEFAULT_QUOTE_CHAR- See Also:
-
mConfig
-
mEncoding
-
mNsAware
protected final boolean mNsAware -
mCheckStructure
protected final boolean mCheckStructure -
mCheckContent
protected final boolean mCheckContent -
mCheckNames
protected final boolean mCheckNames -
mFixContent
protected final boolean mFixContent -
mEscapeCR
final boolean mEscapeCRWhether to escape CR (\r) character. -
mAddSpaceAfterEmptyElem
final boolean mAddSpaceAfterEmptyElemWhether to add a space after empty element (before closing "/>") or not. -
mUseDoubleQuotesInXmlDecl
final boolean mUseDoubleQuotesInXmlDeclWhether to use double quotes in XML declaration or not. -
mAutoCloseOutput
protected final boolean mAutoCloseOutputFlag that defines whether close() on this writer should call close on the underlying output object (stream, 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. -
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. -
mXml11
protected boolean mXml11Indicates whether output is to be compliant; if false, is to be xml 1.0 compliant, if true, xml 1.1 compliant. -
mRawWrapper
Lazy-constructed wrapper object, which will route all calls to Writer API, to matchingwriteRawmethods of this XmlWriter instance. -
mTextWrapper
Lazy-constructed wrapper object, which will route all calls to Writer API, to matchingwriteCharactersmethods of this XmlWriter instance. -
mLocPastChars
protected int mLocPastCharsNumber of characters output prior to currently buffered output -
mLocRowNr
protected int mLocRowNr -
mLocRowStartOffset
protected int mLocRowStartOffsetOffset of the first character on this line. May be negative, if the offset was in a buffer that has been flushed out.
-
-
Constructor Details
-
XmlWriter
- Throws:
IOException
-
-
Method Details
-
enableXml11
public void enableXml11() -
getOutputStream
- Returns:
- Underlying OutputStream used for physical output, if the writer was constructed using one
-
getWriter
- Returns:
- Underlying Writer used for physical output, if the writer was constructed with one, or one was created to be used with an OutputStream.
-
close
Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.- Throws:
IOException
-
flush
- Throws:
IOException
-
writeRaw
- Throws:
IOException
-
writeRaw
- Throws:
IOException
-
writeRaw
- Throws:
IOException
-
writeRawAscii
LikewriteRaw(String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range.- Throws:
IOException
-
writeCDataStart
- Throws:
IOException
-
writeCDataEnd
- Throws:
IOException
-
writeCommentStart
- Throws:
IOException
-
writeCommentEnd
- Throws:
IOException
-
writePIStart
- Throws:
IOException
-
writePIEnd
- Throws:
IOException
-
writeCData
- 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:
IOExceptionXMLStreamException
-
writeCData
public abstract int writeCData(char[] cbuf, int offset, int len) throws IOException, XMLStreamException - Throws:
IOExceptionXMLStreamException
-
writeCharacters
- Throws:
IOException
-
writeCharacters
- Throws:
IOException
-
writeComment
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.- Throws:
IOExceptionXMLStreamException
-
writeDTD
Older "legacy" output method for outputting DOCTYPE declaration. Assumes that the passed-in String contains a complete DOCTYPE declaration properly quoted.- Throws:
IOExceptionXMLStreamException
-
writeDTD
public abstract void writeDTD(String rootName, String systemId, String publicId, String internalSubset) throws IOException, XMLStreamException - Throws:
IOExceptionXMLStreamException
-
writeEntityReference
- Throws:
IOExceptionXMLStreamException
-
writePI
- Throws:
IOExceptionXMLStreamException
-
writeXmlDeclaration
public abstract void writeXmlDeclaration(String version, String enc, String standalone) throws IOException - Throws:
IOException
-
writeStartTagStart
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)
- Throws:
IOExceptionXMLStreamException
-
writeStartTagStart
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)
- Throws:
IOExceptionXMLStreamException
-
writeStartTagEnd
- Throws:
IOException
-
writeStartTagEmptyEnd
- Throws:
IOException
-
writeEndTag
- Throws:
IOException
-
writeEndTag
- Throws:
IOException
-
writeAttribute
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)
- Throws:
IOExceptionXMLStreamException
-
writeAttribute
public abstract void writeAttribute(String localName, char[] value, int offset, int len) throws IOException, XMLStreamException - Throws:
IOExceptionXMLStreamException
-
writeAttribute
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)
- Throws:
IOExceptionXMLStreamException
-
writeAttribute
public abstract void writeAttribute(String prefix, String localName, char[] value, int offset, int len) throws IOException, XMLStreamException - Throws:
IOExceptionXMLStreamException
-
writeTypedElement
public abstract void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException LikewriteRaw(String, int, int), 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- Encoder that will produce content- Throws:
IOException
-
writeTypedElement
public abstract void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException LikewriteRaw(String, int, int), 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- 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:
IOExceptionXMLStreamException
-
writeTypedAttribute
public abstract void writeTypedAttribute(String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException Method similar towriteAttribute(String,String,char[],int,int)but where is known not to require escaping. No validation needs to be performed.- Throws:
IOExceptionXMLStreamException
-
writeTypedAttribute
public abstract void writeTypedAttribute(String prefix, String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException Method similar towriteAttribute(String,String,char[],int,int)but where is known not to require escaping. No validation needs to be performed.- Throws:
IOExceptionXMLStreamException
-
writeTypedAttribute
public abstract 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 Method similar towriteAttribute(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.- Throws:
IOExceptionXMLStreamException
-
getOutputPtr
protected abstract int getOutputPtr() -
getRow
public int getRow() -
getColumn
public int getColumn() -
getAbsOffset
public int getAbsOffset() -
wrapAsRawWriter
Method that can be called to get a wrapper instance that can be used to essentially call thewriteRawmethod. -
wrapAsTextWriter
-
verifyNameValidity
Method called to verify that the name is a legal XML name.- Throws:
XMLStreamException
-
reportNwfName
This is the method called when an output method call violates name well-formedness checks andWstxOutputProperties.P_OUTPUT_VALIDATE_NAMESis is enabled.- Throws:
XMLStreamException
-
reportNwfContent
- Throws:
XMLStreamException
-
throwOutputError
- Throws:
XMLStreamException
-
throwOutputError
- Throws:
XMLStreamException
-
handleInvalidChar
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 propertyWstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER.- Throws:
IOException
-