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 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_VALIDATE_CHARS feature as boolean for better performances.protected boolean
dontValidateNames
DONT_VALIDATE_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, String value)
Adds a String attribute to current element.void
addCDataContent(String content)
Adds content to current CData.void
addCommentContent(String content)
Adds content to current comment.void
addElementContent(String content)
void
addProcessingInstruction(String target, String content)
void
addProcessingInstructionContent(String content)
void
beginCData()
Starts the writing of a new CData.void
beginComment()
Starts the writing of a new comment.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)
void
setEnabled(XmlWriter.Feature... features)
void
setEnabled(XmlWriter.Feature feature, boolean enabled)
Enables or disables a feature.void
setEndOfLine(String eol)
Set the string to use for end of lines.void
setIndentString(String s)
Set the indent string.void
setTabSize(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.
-
-
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)
- 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)
Returnstrue
if a feature is enabled,false
otherwise.- Parameters:
feature
- The feature.- Returns:
true
iffeature
is enabled,false
otherwise.
-
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:
beginDocument
in 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:
beginProcessingInstruction
in interfaceXmlHandler
- Throws:
IOException
-
addProcessingInstructionContent
public final void addProcessingInstructionContent(String content) throws IOException
- Specified by:
addProcessingInstructionContent
in interfaceXmlHandler
- Throws:
IOException
-
endProcessingInstruction
public final void endProcessingInstruction() throws IOException
- Specified by:
endProcessingInstruction
in interfaceXmlHandler
- Throws:
IOException
-
addProcessingInstruction
public final void addProcessingInstruction(String target, String content) throws IOException
- Specified by:
addProcessingInstruction
in interfaceXmlHandler
- Throws:
IOException
-
beginComment
public final void beginComment() throws IOException
Starts the writing of a new comment.- Specified by:
beginComment
in interfaceXmlHandler
- Throws:
IOException
- If an I/O error occurs.
-
addCommentContent
public final void addCommentContent(String content)
Adds content to current comment.- Specified by:
addCommentContent
in interfaceXmlHandler
- Parameters:
content
- The text to add to current comment.
-
endComment
public final void endComment() throws IOException
Closes the current comment.- Specified by:
endComment
in interfaceXmlHandler
- Throws:
IOException
- If an I/O error occurs.
-
beginCData
public final void beginCData() throws IOException
Starts the writing of a new CData.- Specified by:
beginCData
in interfaceXmlHandler
- Throws:
IOException
- If an I/O error occurs.
-
addCDataContent
public final void addCDataContent(String content)
Adds content to current CData.- Specified by:
addCDataContent
in interfaceXmlHandler
- Parameters:
content
- The text to add to current CData.
-
endCData
public final void endCData() throws IOException
Closes the current CData.- Specified by:
endCData
in interfaceXmlHandler
- Throws:
IOException
- If an I/O error occurs.
-
beginElement
public final void beginElement(String name) throws IOException
- Specified by:
beginElement
in interfaceXmlHandler
- Throws:
IOException
-
addAttribute
public final void addAttribute(String name, String value) throws IOException
Adds a String attribute to current element.- Specified by:
addAttribute
in 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:
addElementContent
in interfaceXmlHandler
- Throws:
IOException
-
endElement
public final void endElement() throws IOException
- Specified by:
endElement
in interfaceXmlHandler
- Throws:
IOException
-
endDocument
public final void endDocument() throws IOException
- Specified by:
endDocument
in interfaceXmlHandler
- 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()
-
-