public class BeanWriter extends AbstractBeanWriter
BeanWriter
outputs beans as XML to an io stream.
The output for each bean is an xml fragment (rather than a well-formed xml-document). This allows bean representations to be appended to a document by writing each in turn to the stream. So to create a well formed xml document, you'll need to write the prolog to the stream first. If you append more than one bean to the stream, then you'll need to add a wrapping root element as well.
The line ending to be used is set by setEndOfLine(java.lang.String)
.
The output can be formatted (with whitespace) for easy reading
by calling enablePrettyPrint()
.
The output will be indented.
The indent string used is set by setIndent(java.lang.String)
.
Bean graphs can sometimes contain cycles.
Care must be taken when serializing cyclic bean graphs
since this can lead to infinite recursion.
The approach taken by BeanWriter
is to automatically
assign an ID
attribute value to beans.
When a cycle is encountered,
an element is written that has the IDREF
attribute set to the
id assigned earlier.
The names of the ID
and IDREF
attributes used
can be customized by the XMLBeanInfo
.
The id's used can also be customized by the user
via IDGenerator
subclasses.
The implementation used can be set by the IdGenerator
property.
BeanWriter defaults to using SequentialIDGenerator
which supplies id values in numeric sequence.
If generated ID
attribute values are not acceptable in the output,
then this can be disabled by setting the WriteIDs
property to false.
If a cyclic reference is encountered in this case then a
CyclicReferenceException
will be thrown.
When the WriteIDs
property is set to false,
it is recommended that this exception is caught by the caller.
Modifier and Type | Field and Description |
---|---|
private static String |
AMPERSAND_ENTITY
Escaped
& entity |
private static String |
APOSTROPHE_ENTITY
Escaped
' entity |
private boolean |
autoFlush
should we flush after writing bean
|
private String |
endOfLine
text used for end of lines.
|
private static String |
EOL
text used for end of lines.
|
private static String |
GREATER_THAN_ENTITY
Escaped
> entity |
private String |
indent
indentation text
|
private static String |
LESS_THAN_ENTITY
Escaped
< entity |
private Log |
log
Log used for logging (Doh!)
|
private static String |
QUOTE_ENTITY
Escaped
" entity |
private Writer |
writer
Where the output goes
|
Constructor and Description |
---|
BeanWriter()
Constructor uses
System.out for output. |
BeanWriter(OutputStream out)
Constuctor uses given
OutputStream for output. |
BeanWriter(Writer writer)
Constructor sets writer used for output.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying output stream
|
void |
enablePrettyPrint()
Switch on formatted output.
|
protected String |
escapeAttributeValue(Object value)
Escape the
toString of the given object. |
protected String |
escapeBodyValue(Object value)
Escape the
toString of the given object. |
protected void |
expressAttribute(String qualifiedName,
String value)
Writes an attribute to the stream.
|
protected void |
expressBodyText(String text)
Write element body text
|
protected void |
expressElementEnd()
Write an empty element end to the stream
|
protected void |
expressElementEnd(String qualifiedName)
Write an element end tag to the stream
|
protected void |
expressElementStart(String qualifiedName)
Express an element tag start using given qualified name
|
protected void |
expressTagClose()
Write a tag close to the stream
|
void |
flush()
Allows output to be flushed on the underlying output stream
|
String |
getEndOfLine()
Gets the string used to mark end of lines.
|
String |
getIndent()
Gets the indent string
|
Log |
getLog()
Set the log implementation used.
|
void |
setEndOfLine(String endOfLine)
Sets the string used for end of lines
Produces a warning the specified value contains an invalid whitespace character
|
void |
setIndent(String indent)
Sets the string used for pretty print indents
|
void |
setLog(Log log)
Set the log implementation used.
|
void |
write(Object bean)
Write the given object to the stream (and then flush).
|
protected void |
writeIndent()
Writes out
indent 's to the current indentLevel |
protected void |
writePrintln()
Writes out an empty line.
|
void |
writeXmlDeclaration(String xmlDeclaration)
A helper method that allows you to write the XML Declaration.
|
end, getAbstractBeanWriterLog, getIdGenerator, getIndentLevel, getWriteIDs, getXMLIntrospector, popBean, pushBean, setAbstractBeanWriterLog, setIdGenerator, setWriteIDs, setXMLIntrospector, start, write, write, write, writeAttribute, writeAttributes, writeContent, writeIDREFElement, writeRestOfElement
private static final String LESS_THAN_ENTITY
<
entityprivate static final String GREATER_THAN_ENTITY
>
entityprivate static final String AMPERSAND_ENTITY
&
entityprivate static final String APOSTROPHE_ENTITY
'
entityprivate static final String QUOTE_ENTITY
"
entityprivate Writer writer
private static final String EOL
\n
private String endOfLine
\n
private String indent
private boolean autoFlush
private Log log
public BeanWriter()
Constructor uses System.out
for output.
public BeanWriter(OutputStream out)
Constuctor uses given OutputStream
for output.
out
- write out representations to this streampublic BeanWriter(Writer writer)
Constructor sets writer used for output.
writer
- write out representations to this writerpublic void writeXmlDeclaration(String xmlDeclaration) throws IOException
xmlDeclaration
- is the XML declaration string typically of
the form "<xml version='1.0' encoding='UTF-8' ?>IOException
- when declaration cannot be writtenpublic void flush() throws IOException
IOException
- when the flush cannot be completedpublic void close() throws IOException
IOException
- when writer cannot be closedpublic void write(Object bean) throws IOException, SAXException, IntrospectionException
write
in class AbstractBeanWriter
bean
- write this Object
to the streamIOException
- if an IO problem causes failureSAXException
- if a SAX problem causes failureIntrospectionException
- if bean cannot be introspectedpublic void enablePrettyPrint()
Switch on formatted output. This sets the end of line and the indent. The default is adding 2 spaces and a newline
public String getEndOfLine()
public void setEndOfLine(String endOfLine)
endOfLine
- the String
public String getIndent()
public void setIndent(String indent)
indent
- use this string
for indentspublic Log getLog()
Set the log implementation used.
org.apache.commons.logging.Log
level constantpublic void setLog(Log log)
Set the log implementation used.
log
- Log
implementation to useprotected void expressElementStart(String qualifiedName) throws IOException
expressElementStart
in class AbstractBeanWriter
qualifiedName
- the fully qualified name of the element to writeIOException
- when stream write failsprotected void expressTagClose() throws IOException
expressTagClose
in class AbstractBeanWriter
IOException
- when stream write failsprotected void expressElementEnd(String qualifiedName) throws IOException
expressElementEnd
in class AbstractBeanWriter
qualifiedName
- the name of the elementIOException
- when stream write failsprotected void expressElementEnd() throws IOException
expressElementEnd
in class AbstractBeanWriter
IOException
- when stream write failsprotected void expressBodyText(String text) throws IOException
expressBodyText
in class AbstractBeanWriter
text
- write out this body textIOException
- when the stream write failsprotected void expressAttribute(String qualifiedName, String value) throws IOException
expressAttribute
in class AbstractBeanWriter
qualifiedName
- fully qualified attribute namevalue
- attribute valueIOException
- when the stream write failsprotected void writePrintln() throws IOException
endOfLine
.writePrintln
in class AbstractBeanWriter
IOException
- when stream write failsprotected void writeIndent() throws IOException
indent
's to the current indentLevel
writeIndent
in class AbstractBeanWriter
IOException
- when stream write failsprotected String escapeBodyValue(Object value)
Escape the toString
of the given object.
For use as body text.
value
- escape value.toString()