Package cdc.util.xml
Class XmlWriter
- java.lang.Object
-
- cdc.util.xml.XmlWriter
-
- All Implemented Interfaces:
XmlHandler,Closeable,Flushable,AutoCloseable
public class XmlWriter extends Object implements XmlHandler, Flushable, Closeable
Basic XML writer.This class does not support all XML features. It supports specific features:
- Pretty printing.
- Name conversion for elements and attributes.
- Standard ('\n') or platform specific End of line.
- Standard compression algorithms.
- ...
Pretty printing
Pretty printing may produce unexpected results with mixed content elements.
This implementation tries to produce data as soon as possible.
Discovering that an element has a mixed content may happen late, so that indentation behavior changes after such discovery.
If one knows that an element has a mixed content, it is advised to indicate it to the writer by writing an empty string content first in that element.- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXmlWriter.FeatureEnumeration of features supported by the writer.
-
Field Summary
Fields Modifier and Type Field Description protected booleanalwaysEntitizeElementsALWAYS_ENTITIZE_ELEMENTS feature as boolean for better performances.static StringAMPstatic StringAPOSprotected static PatternCDATA_END_PATTERNstatic StringCRprotected booleandontValidateCharsDONT_VALIDATE_CHARS feature as boolean for better performances.protected booleandontValidateNamesDONT_VALIDATE_NAMES feature as boolean for better performances.static StringGTstatic StringLFstatic StringLTstatic StringQUOTstatic StringTABprotected XmlVersionversionstatic StringXML_SCHEMA_INSTANCEstatic StringXMLNSstatic StringXMLNS_XSIstatic StringXSI_SCHEMA_LOCATION
-
Constructor Summary
Constructors Constructor Description XmlWriter()Creates an XmlWriter to System.out, with UTF-8 encoding.XmlWriter(File file)Creates an XmlWriter to a file using UTF-8 encoding.XmlWriter(File file, Compressor compressor)Creates an XmlWriter to a file with UTF-8 encoding and compressor.XmlWriter(File file, String encoding)Creates an XmlWriter to a file with a given encoding.XmlWriter(File file, String encoding, Compressor compressor)Creates an XmlWriter to a file with a given encoding and compressor.XmlWriter(OutputStream os)Creates an XmlWriter to an OutputStream with UTF-8 encoding.XmlWriter(OutputStream os, String encoding)Creates an XmlWriter to an OutputStream with a given encoding.XmlWriter(PrintStream out)Creates an XmlWriter to a PrintStream, with UTF-8 encoding.XmlWriter(Writer writer)Creates an XmlWriter to a Writer, with UTF-8 encoding.XmlWriter(Writer writer, String encoding)Creates an XmlWriter to a Writer with a given encoding.XmlWriter(String filename)Creates an XmlWriter to a file with UTF-8 encoding.XmlWriter(String filename, Compressor compressor)Creates an XmlWriter to a file with a UTF-8 encoding and compressor.XmlWriter(String filename, String encoding)Creates an XmlWriter to a file with a given encoding.XmlWriter(String filename, String encoding, Compressor compressor)Creates an XmlWriter to a file with a given encoding and compressor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAttribute(String name, String value)Adds a String attribute to current element.voidaddCDataContent(String content)Adds content to current CData.voidaddCommentContent(String content)Adds content to current comment.voidaddElementContent(String content)voidaddProcessingInstruction(String target, String content)voidaddProcessingInstructionContent(String content)voidbeginCData()Starts the writing of a new CData.voidbeginComment()Starts the writing of a new comment.voidbeginDocument(XmlVersion version)Begins an XML document, using writer's encoding.voidbeginElement(String name)voidbeginProcessingInstruction(String target)voidclose()protected static voiddataError(String message)voidendCData()Closes the current CData.voidendComment()Closes the current comment.voidendDocument()voidendElement()voidendProcessingInstruction()voidflush()CaseConvertergetConverter()Returns the case converter.StringgetEndOfLine()StringgetIndentString()booleanisEnabled(XmlWriter.Feature feature)Returnstrueif a feature is enabled,falseotherwise.voidreset()voidsetCaseConverter(CaseConverter converter)voidsetEnabled(XmlWriter.Feature... features)voidsetEnabled(XmlWriter.Feature feature, boolean enabled)Enables or disables a feature.voidsetEndOfLine(String eol)Set the string to use for end of lines.voidsetIndentString(String s)Set the indent string.voidsetTabSize(int tabSize)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface cdc.util.xml.XmlHandler
addAttribute, addAttribute, addAttribute, addAttribute, addAttribute, addAttribute, addAttribute, addAttribute, addAttribute, addCData, addComment, addDefaultNamespace, addElement, addElementIfNonEmpty, addNamespace, addStyleSheet, beginDocument
-
-
-
-
Field Detail
-
CDATA_END_PATTERN
protected static final Pattern CDATA_END_PATTERN
-
LF
public static final String LF
- See Also:
- Constant Field Values
-
CR
public static final String CR
- See Also:
- Constant Field Values
-
TAB
public static final String TAB
- See Also:
- Constant Field Values
-
GT
public static final String GT
- See Also:
- Constant Field Values
-
LT
public static final String LT
- See Also:
- Constant Field Values
-
QUOT
public static final String QUOT
- See Also:
- Constant Field Values
-
APOS
public static final String APOS
- See Also:
- Constant Field Values
-
AMP
public static final String AMP
- See Also:
- Constant Field Values
-
XMLNS
public static final String XMLNS
- See Also:
- Constant Field Values
-
XMLNS_XSI
public static final String XMLNS_XSI
- See Also:
- Constant Field Values
-
XML_SCHEMA_INSTANCE
public static final String XML_SCHEMA_INSTANCE
- See Also:
- Constant Field Values
-
XSI_SCHEMA_LOCATION
public static final String XSI_SCHEMA_LOCATION
- See Also:
- Constant Field Values
-
version
protected XmlVersion version
-
dontValidateNames
protected boolean dontValidateNames
DONT_VALIDATE_NAMES feature as boolean for better performances.
-
dontValidateChars
protected boolean dontValidateChars
DONT_VALIDATE_CHARS feature as boolean for better performances.
-
alwaysEntitizeElements
protected boolean alwaysEntitizeElements
ALWAYS_ENTITIZE_ELEMENTS feature as boolean for better performances.
-
-
Constructor Detail
-
XmlWriter
public XmlWriter(Writer writer, String encoding)
Creates an XmlWriter to a Writer with a given encoding.- Parameters:
writer- The writer.encoding- The encoding to use.
-
XmlWriter
public XmlWriter(Writer writer)
Creates an XmlWriter to a Writer, with UTF-8 encoding.- Parameters:
writer- The writer.
-
XmlWriter
public XmlWriter() throws IOExceptionCreates an XmlWriter to System.out, with UTF-8 encoding.- Throws:
UnsupportedEncodingException- If UTF-8 is not supported, which should not happen.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(PrintStream out) throws IOException
Creates an XmlWriter to a PrintStream, with UTF-8 encoding.- Parameters:
out- The PrintStream to use. Must be closed be the caller.- Throws:
UnsupportedEncodingException- If UTF-8 encoding is not supported, which should not happen.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(OutputStream os, String encoding) throws IOException
Creates an XmlWriter to an OutputStream with a given encoding.- Parameters:
os- The outputStream. Must be closed by caller.encoding- The encoding to use.- Throws:
UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(OutputStream os) throws IOException
Creates an XmlWriter to an OutputStream with UTF-8 encoding.- Parameters:
os- The outputStream. Must be closed by caller.- Throws:
UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(String filename, String encoding, Compressor compressor) throws IOException
Creates an XmlWriter to a file with a given encoding and compressor.- Parameters:
filename- Name of the file.encoding- The encoding to use.compressor- The compressor.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(String filename, String encoding) throws IOException
Creates an XmlWriter to a file with a given encoding.- Parameters:
filename- Name of the file.encoding- The encoding to use.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(String filename, Compressor compressor) throws IOException
Creates an XmlWriter to a file with a UTF-8 encoding and compressor.- Parameters:
filename- Name of the file.compressor- The compressor.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(String filename) throws IOException
Creates an XmlWriter to a file with UTF-8 encoding.- Parameters:
filename- Name of the file.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(File file, String encoding, Compressor compressor) throws IOException
Creates an XmlWriter to a file with a given encoding and compressor.- Parameters:
file- File to use.encoding- The encoding to use.compressor- The compressor to use.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(File file, String encoding) throws IOException
Creates an XmlWriter to a file with a given encoding.- Parameters:
file- File to use.encoding- The encoding to use.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(File file, Compressor compressor) throws IOException
Creates an XmlWriter to a file with UTF-8 encoding and compressor.- Parameters:
file- File to use.compressor- The compressor to use.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If the named encoding is not supported.IOException- When an IO exception occurs.
-
XmlWriter
public XmlWriter(File file) throws IOException
Creates an XmlWriter to a file using UTF-8 encoding.- Parameters:
file- File to use.- Throws:
FileNotFoundException- If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.UnsupportedEncodingException- If UTF-8 encoding is not supported, which should not happen.IOException- When an IO exception occurs.
-
-
Method Detail
-
dataError
protected static void dataError(String message)
- Parameters:
message- The exception message.- Throws:
InvalidDataException- in all cases.
-
setEnabled
public final void setEnabled(XmlWriter.Feature feature, boolean enabled)
Enables or disables a feature.- Parameters:
feature- The feature to enable/disable.enabled- If true, enable the feature.
-
setEnabled
public final void setEnabled(XmlWriter.Feature... features)
-
isEnabled
public final boolean isEnabled(XmlWriter.Feature feature)
Returnstrueif a feature is enabled,falseotherwise.- Parameters:
feature- The feature.- Returns:
trueiffeatureis enabled,falseotherwise.
-
setIndentString
public final void setIndentString(String s)
Set the indent string.- Parameters:
s- The indent string.
-
getIndentString
public final String getIndentString()
-
setTabSize
public final void setTabSize(int tabSize)
-
setEndOfLine
public final void setEndOfLine(String eol)
Set the string to use for end of lines.This may be useful to force an OS specific symbol.
This has interactions withXmlWriter.Feature.USE_XML_EOL.
WARNING: It is the responsibility of programmer to use a valid string.- Parameters:
eol- The end of line string.
-
getEndOfLine
public final String getEndOfLine()
- Returns:
- The end of line string.
-
getConverter
public final CaseConverter getConverter()
Returns the case converter. Used if USE_CONVERTER feature is enabled.- Returns:
- The case converter.
-
setCaseConverter
public final void setCaseConverter(CaseConverter converter)
-
beginDocument
public final void beginDocument(XmlVersion version) throws IOException
Begins an XML document, using writer's encoding.- Specified by:
beginDocumentin interfaceXmlHandler- Parameters:
version- The XML version to use.- Throws:
IOException- If an I/O error occurs.
-
beginProcessingInstruction
public final void beginProcessingInstruction(String target) throws IOException
- Specified by:
beginProcessingInstructionin interfaceXmlHandler- Throws:
IOException
-
addProcessingInstructionContent
public final void addProcessingInstructionContent(String content) throws IOException
- Specified by:
addProcessingInstructionContentin interfaceXmlHandler- Throws:
IOException
-
endProcessingInstruction
public final void endProcessingInstruction() throws IOException- Specified by:
endProcessingInstructionin interfaceXmlHandler- Throws:
IOException
-
addProcessingInstruction
public final void addProcessingInstruction(String target, String content) throws IOException
- Specified by:
addProcessingInstructionin interfaceXmlHandler- Throws:
IOException
-
beginComment
public final void beginComment() throws IOExceptionStarts the writing of a new comment.- Specified by:
beginCommentin interfaceXmlHandler- Throws:
IOException- If an I/O error occurs.
-
addCommentContent
public final void addCommentContent(String content)
Adds content to current comment.- Specified by:
addCommentContentin interfaceXmlHandler- Parameters:
content- The text to add to current comment.
-
endComment
public final void endComment() throws IOExceptionCloses the current comment.- Specified by:
endCommentin interfaceXmlHandler- Throws:
IOException- If an I/O error occurs.
-
beginCData
public final void beginCData() throws IOExceptionStarts the writing of a new CData.- Specified by:
beginCDatain interfaceXmlHandler- Throws:
IOException- If an I/O error occurs.
-
addCDataContent
public final void addCDataContent(String content)
Adds content to current CData.- Specified by:
addCDataContentin interfaceXmlHandler- Parameters:
content- The text to add to current CData.
-
endCData
public final void endCData() throws IOExceptionCloses the current CData.- Specified by:
endCDatain interfaceXmlHandler- Throws:
IOException- If an I/O error occurs.
-
beginElement
public final void beginElement(String name) throws IOException
- Specified by:
beginElementin interfaceXmlHandler- Throws:
IOException
-
addAttribute
public final void addAttribute(String name, String value) throws IOException
Adds a String attribute to current element.- Specified by:
addAttributein interfaceXmlHandler- Parameters:
name- Attribute name.value- Attribute value.- Throws:
IOException- If an I/O error occurs.
-
addElementContent
public final void addElementContent(String content) throws IOException
- Specified by:
addElementContentin interfaceXmlHandler- Throws:
IOException
-
endElement
public final void endElement() throws IOException- Specified by:
endElementin interfaceXmlHandler- Throws:
IOException
-
endDocument
public final void endDocument() throws IOException- Specified by:
endDocumentin interfaceXmlHandler- Throws:
IOException
-
flush
public void flush() throws IOException- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
reset
public void reset()
-
-