Class XmlWriter
- java.lang.Object
-
- cdc.util.xml.XmlWriter
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
public class XmlWriter extends Object implements 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 class
XmlWriter.Feature
Enumeration of features supported by the writer.
-
Field Summary
Fields Modifier and Type Field Description static String
AMP
static String
APOS
protected static Pattern
CDATA_END_PATTERN
static String
CR
protected boolean
dontValidateChars
DONT_VALUDATE_CHARS feature as boolean for better performances.protected boolean
dontValidateNames
DONT_VALUDATE_NAMES feature as boolean for better performances.static String
GT
static String
LF
static String
LT
static String
QUOT
static String
TAB
protected XmlVersion
version
static String
XML_SCHEMA_INSTANCE
static String
XMLNS
static String
XMLNS_XSI
static String
XSI_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 void
addAttribute(String name, boolean value)
Adds a boolean attribute to current element.void
addAttribute(String name, byte value)
Adds a byte attribute to current element.void
addAttribute(String name, char value)
Adds a char attribute to current element.void
addAttribute(String name, double value)
Adds a double attribute to current element.void
addAttribute(String name, float value)
Adds a float attribute to current element.void
addAttribute(String name, int value)
Adds an int attribute to current element.void
addAttribute(String name, long value)
Adds a long attribute to current element.void
addAttribute(String name, short value)
Adds a short attribute to current element.void
addAttribute(String name, Object value)
Adds an Object attribute to current element.void
addAttribute(String name, String value)
Adds a String attribute to current element.void
addCData(String content)
Inserts a CData section with a content.void
addCDataContent(String content)
Adds content to current CData.void
addComment(String content)
Inserts a comment section with a content.void
addCommentContent(String content)
Adds content to current comment.void
addDefaultNamespace(String uri)
void
addElement(String name, String content)
void
addElementContent(String content)
void
addElementIfNonEmpty(String name, String content)
void
addNamespace(String prefix, String uri)
Adds a namespace declaration.void
addProcessingInstruction(String target, String content)
void
addProcessingInstructionContent(String content)
void
addStyleSheet(String xsl)
Helper function used to insert a StyleSheet Processing Instruction.void
beginCData()
Starts the writing of a new CData.void
beginComment()
Starts the writing of a new comment.void
beginDocument()
Begins an XML document, using writer's encoding and version 1.0.void
beginDocument(XmlVersion version)
Begins an XML document, using writer's encoding.void
beginElement(String name)
void
beginProcessingInstruction(String target)
void
close()
protected static void
dataError(String message)
void
endCData()
Closes the current CData.void
endComment()
Closes the current comment.void
endDocument()
void
endElement()
void
endProcessingInstruction()
void
flush()
CaseConverter
getConverter()
Returns the case converter.String
getEndOfLine()
String
getIndentString()
boolean
isEnabled(XmlWriter.Feature feature)
Returnstrue
if a feature is enabled,false
otherwise.void
reset()
void
setCaseConverter(CaseConverter converter)
XmlWriter
setEnabled(XmlWriter.Feature... features)
XmlWriter
setEnabled(XmlWriter.Feature feature, boolean enabled)
Enables or disables a feature.void
setEndOfLine(String eol)
Set the string to use for end of lines.XmlWriter
setIndentString(String s)
Set the indent string.void
setTabSize(int tabSize)
-
-
-
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_VALUDATE_NAMES feature as boolean for better performances.
-
dontValidateChars
protected boolean dontValidateChars
DONT_VALUDATE_CHARS 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 IOException
Creates 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)
-
setEnabled
public final XmlWriter setEnabled(XmlWriter.Feature feature, boolean enabled)
Enables or disables a feature.- Parameters:
feature
- The feature to enable/disable.enabled
- If true, enable the feature.- Returns:
- This writer.
-
setEnabled
public final XmlWriter setEnabled(XmlWriter.Feature... features)
-
isEnabled
public final boolean isEnabled(XmlWriter.Feature feature)
Returnstrue
if a feature is enabled,false
otherwise.- Parameters:
feature
- The feature.- Returns:
true
iffeature
is enabled,false
otherwise.
-
setIndentString
public final XmlWriter setIndentString(String s)
Set the indent string.- Parameters:
s
- The indent string.- Returns:
- This writer.
-
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.- Parameters:
version
- The XML version to use.- Throws:
IOException
- If an I/O error occurs.
-
beginDocument
public final void beginDocument() throws IOException
Begins an XML document, using writer's encoding and version 1.0.- Throws:
IOException
- If an I/O error occurs.
-
beginProcessingInstruction
public final void beginProcessingInstruction(String target) throws IOException
- Throws:
IOException
-
addProcessingInstructionContent
public final void addProcessingInstructionContent(String content) throws IOException
- Throws:
IOException
-
endProcessingInstruction
public final void endProcessingInstruction() throws IOException
- Throws:
IOException
-
addProcessingInstruction
public final void addProcessingInstruction(String target, String content) throws IOException
- Throws:
IOException
-
addStyleSheet
public final void addStyleSheet(String xsl) throws IOException
Helper function used to insert a StyleSheet Processing Instruction.- Parameters:
xsl
- The URL of the style sheet to use.- Throws:
IOException
- If an I/O error occurs.
-
beginComment
public final void beginComment() throws IOException
Starts the writing of a new comment.- Throws:
IOException
- If an I/O error occurs.
-
addCommentContent
public final void addCommentContent(String content)
Adds content to current comment.- Parameters:
content
- The text to add to current comment.
-
endComment
public final void endComment() throws IOException
Closes the current comment.- Throws:
IOException
- If an I/O error occurs.
-
addComment
public final void addComment(String content) throws IOException
Inserts a comment section with a content.- Parameters:
content
- The comment content.- Throws:
IOException
- If an I/O error occurs.
-
beginCData
public final void beginCData() throws IOException
Starts the writing of a new CData.- Throws:
IOException
- If an I/O error occurs.
-
addCDataContent
public final void addCDataContent(String content)
Adds content to current CData.- Parameters:
content
- The text to add to current CData.
-
endCData
public final void endCData() throws IOException
Closes the current CData.- Throws:
IOException
- If an I/O error occurs.
-
addCData
public final void addCData(String content) throws IOException
Inserts a CData section with a content.- Parameters:
content
- The CData content.- Throws:
IOException
- If an I/O error occurs.
-
beginElement
public final void beginElement(String name) throws IOException
- Throws:
IOException
-
addAttribute
public final void addAttribute(String name, Object value) throws IOException
Adds an Object attribute to current element.If object is null adds an empty attribute.
Otherwise, uses toString() to convert value to a String.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, String value) throws IOException
Adds a String attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, char value) throws IOException
Adds a char attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, boolean value) throws IOException
Adds a boolean attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, long value) throws IOException
Adds a long attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, int value) throws IOException
Adds an int attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, short value) throws IOException
Adds a short attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, byte value) throws IOException
Adds a byte attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, double value) throws IOException
Adds a double attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addAttribute
public final void addAttribute(String name, float value) throws IOException
Adds a float attribute to current element.- Parameters:
name
- Attribute name.value
- Attribute value.- Throws:
IOException
- If an I/O error occurs.
-
addNamespace
public final void addNamespace(String prefix, String uri) throws IOException
Adds a namespace declaration.- Parameters:
prefix
- Namespace prefixuri
- Associated URI.- Throws:
IOException
- If an I/O error occurs.
-
addDefaultNamespace
public final void addDefaultNamespace(String uri) throws IOException
- Throws:
IOException
-
addElementContent
public final void addElementContent(String content) throws IOException
- Throws:
IOException
-
endElement
public final void endElement() throws IOException
- Throws:
IOException
-
addElement
public final void addElement(String name, String content) throws IOException
- Throws:
IOException
-
addElementIfNonEmpty
public final void addElementIfNonEmpty(String name, String content) throws IOException
- Throws:
IOException
-
endDocument
public final void endDocument() throws IOException
- Throws:
IOException
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
reset
public void reset()
-
-