|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.Writer java.io.FilterWriter org.apache.commons.xmlio.out.XMLWriter
public class XMLWriter
FilterWriter
adding formatted and encoded XML export
functionality to the underlying writer. Formatting and
encoding is done as straight forward as possible.
Everything you know better than this class must be done by you, e.g. you will
have to tell XMLWriter
where you wish to have
newlines.In effect, no unexpected so called
intelligent behavior is to be feared. Another effect is high speed.
A simple example: Suppose your XMLWriter
object is xmlWriter.
The following sequence of code
xmlWriter.writeStartTag("<root>");
xmlWriter.writeStartTag("<next1>", false);
xmlWriter.writeEmptyTag("<emptyTag/>", false);
xmlWriter.writeEndTag("</next1>");
xmlWriter.writeStartTag("</root>");
will write this to the underlying writer
<root>
<next1><emptyTag/></next1>
</root>
Caution: Do not forget to call FilterWriter.flush()
at the end of your
exporting process as otherwise no data might be written.
Field Summary | |
---|---|
protected int |
depth
Current depth of the tree. |
protected int |
indent
Current indentation. |
static boolean |
NEWLINE
|
protected boolean |
nlAfterEmptyTag
|
protected boolean |
nlAfterEndTag
|
protected boolean |
nlAfterStartTag
|
static boolean |
NO_NEWLINE
|
protected boolean |
prettyPrintMode
|
protected int |
tabWidth
|
protected boolean |
xmlDeclWritten
Flag indicating if the XML declaration has already been writter. |
Fields inherited from class java.io.FilterWriter |
---|
out |
Fields inherited from class java.io.Writer |
---|
lock |
Constructor Summary | |
---|---|
XMLWriter(Writer writer)
Creates a new filter writer for XML export. |
Method Summary | |
---|---|
static String |
createEmptyTag(String tagName)
Convenience method for creating an empty tag having no attributes. |
static String |
createEmptyTag(String tagName,
Attributes attributes)
Convenience method for creating an empty tag. |
static String |
createEmptyTag(String tagName,
String[][] attrPairs)
Convenience method for creating an empty tag. |
static String |
createEmptyTag(String tagName,
String[] attrNames,
String[] attrValues)
Convenience method for creating an empty tag. |
static String |
createEmptyTag(String tagName,
String attrName,
String attrValue)
Convenience method for creating an empty tag. |
static String |
createEndTag(String tagName)
Convenience method for creating an end tag. |
static String |
createStartTag(String tagName)
Convenience method for creating a start tag having no attributes. |
static String |
createStartTag(String tagName,
Attributes attributes)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
Attributes attributes,
boolean isEmpty)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
Attributes attributes,
boolean isEmpty,
boolean encodeAttrs,
char quoteChar)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
String[][] attrPairs)
Convenience method for creating a non empty start tag. |
static String |
createStartTag(String tagName,
String[][] attrPairs,
boolean isEmpty)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
String[][] attrPairs,
boolean isEmpty,
boolean encodeAttrs,
char quoteChar)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
String[] attrNames,
String[] attrValues)
Convenience method for creating a non empty start tag. |
static String |
createStartTag(String tagName,
String[] attrNames,
String[] attrValues,
boolean isEmpty)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
String[] attrNames,
String[] attrValues,
boolean isEmpty,
boolean encodeAttrs,
char quoteChar)
Convenience method for creating a start tag. |
static String |
createStartTag(String tagName,
String attrName,
String attrValue)
Convenience method for creating a non empty start tag. |
static String |
createStartTag(String tagName,
String attrName,
String attrValue,
boolean isEmpty)
Convenience method for creating a start tag. |
static void |
generateAndWriteElementWithCData(XMLWriter xmlWriter,
String tagName,
String[][] attrPairs,
String cData)
Convenience method for creating and writing a whole element. |
static void |
generateAndWriteElementWithCData(XMLWriter xmlWriter,
String tagName,
String[] attrNames,
String[] attrValues,
String cData)
Convenience method for creating and writing a whole element. |
boolean |
getNlAfterEmptyTag()
Gets property described in setNlAfterEmptyTag(boolean) . |
boolean |
getNlAfterEndTag()
Gets property described in setNlAfterEndTag(boolean) . |
boolean |
getNlAfterStartTag()
Gets property described in setNlAfterStartTag(boolean) . |
boolean |
getPrettyPrintMode()
Gets property described in setPrettyPrintMode(boolean) . |
int |
getTabWidth()
Gets property described in setTabWidth(int) . |
boolean |
isXMLDeclarationWritten()
Indicates whether the XML declaration has been written, yet. |
void |
setNlAfterEmptyTag(boolean nlAfterEmptyTag)
Sets if a newline is inserted after an empty start element by default. |
void |
setNlAfterEndTag(boolean nlAfterEndTag)
Sets if a newline is inserted after an end tag by default. |
void |
setNlAfterStartTag(boolean nlAfterStartTag)
Sets if a newline is inserted after a non empty start tag by default. |
void |
setPrettyPrintMode(boolean prettyPrintMode)
Switches on/off pretty print mode. |
void |
setTabWidth(int tabWidth)
Sets the amount of spaces to increase indentation with element level. |
void |
setXMLDeclarationWritten(boolean xmlDeclWritten)
Manually sets or resets whether XML declaration has been written. |
void |
writeCData(String cData)
Writes character data with encoding. |
void |
writeComment(String comment)
Writes comment encoded as comment. |
void |
writeElementWithCData(String startTag,
String cData,
String endTag)
Writes a full element consisting of a start tag, character data and an end tag. |
void |
writeElementWithPCData(String startTag,
String pcData,
String endTag)
Writes a full element consisting of a start tag, character data and an end tag. |
void |
writeEmptyElement(String emptyTag)
Writes an empty element. |
void |
writeEmptyElement(String emptyTag,
boolean nl)
Writes an empty element. |
void |
writeEndTag(String endTag)
Writes an end tag. |
void |
writeEndTag(String endTag,
boolean nl)
Writes an end tag. |
void |
writeNl()
Writes a single newline. |
void |
writePCData(String pcData)
Writes character data without encoding. |
void |
writePI(String target,
String data)
Writes a processing instruction. |
void |
writeProlog(String prolog)
Writes prolog data like doctype delcaration and DTD parts followed by a newline. |
void |
writeStartTag(String startTag)
Writes a start tag. |
void |
writeStartTag(String startTag,
boolean nl)
Writes a start tag. |
void |
writeXMLDeclaration()
Writes XML declaration. |
Methods inherited from class java.io.FilterWriter |
---|
close, flush, write, write, write |
Methods inherited from class java.io.Writer |
---|
append, append, append, append, append, append, write, write |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final boolean NEWLINE
public static final boolean NO_NEWLINE
protected int tabWidth
protected int depth
protected int indent
protected boolean prettyPrintMode
protected boolean nlAfterEmptyTag
protected boolean nlAfterStartTag
protected boolean nlAfterEndTag
protected boolean xmlDeclWritten
isXMLDeclarationWritten()
.
It might be useful to
avoid writing twice or more times in different contexts writing
to same writer.
writeXMLDeclaration()
.
Constructor Detail |
---|
public XMLWriter(Writer writer)
writer
- the underlying writer the formatted XML is exported toMethod Detail |
---|
public static final String createEndTag(String tagName)
tagName
- name of the end tagpublic static final String createStartTag(String tagName)
tagName
- name of the start tagpublic static final String createEmptyTag(String tagName)
<tagName/>
.
tagName
- name of the tagpublic static final String createStartTag(String tagName, String[] attrNames, String[] attrValues, boolean isEmpty)
tagName
- name of the start tagattrNames
- names of attributes to be included into start tagattrValues
- values of attributes to be included into start tag -
there should be just as many entries as in attrNames
,
if a value is null
corresponding attribute will not be includedisEmpty
- decides wheter this is start tag is for an empty elementpublic static final String createStartTag(String tagName, String[] attrNames, String[] attrValues)
tagName
- name of the start tagattrNames
- names of attributes to be included into start tagattrValues
- values of attributes to be included into start tag -
there should be just as many entries as in attrNames
,
if a value is null
corresponding attribute will not be includedpublic static final String createEmptyTag(String tagName, String[] attrNames, String[] attrValues)
tagName
- name of the tagattrNames
- names of attributes to be included into tagattrValues
- values of attributes to be included into tag -
there should be just as many entries as in attrNames
,
if a value is null
corresponding attribute will not be includedcreateEmptyTag(String)
public static final String createStartTag(String tagName, String attrName, String attrValue, boolean isEmpty)
tagName
- name of the start tagattrName
- name of attribute to be included into start tagattrValue
- value of attribute to be included into start tag,
if attrValue is null
attribute will not be includedisEmpty
- decides wheter this is start tag is for an empty elementpublic static final String createStartTag(String tagName, String attrName, String attrValue)
tagName
- name of the start tagattrName
- name of attribute to be included into start tagattrValue
- value of attribute to be included into start tag,
if attrValue is null
attribute will not be includedpublic static final String createEmptyTag(String tagName, String attrName, String attrValue)
tagName
- name of the tagattrName
- name of attribute to be included into tagattrValue
- value of attribute to be included into tag,
if attrValue is null
attribute will not be includedcreateEmptyTag(String)
public static final String createStartTag(String tagName, String[] attrNames, String[] attrValues, boolean isEmpty, boolean encodeAttrs, char quoteChar)
tagName
- name of the start tagattrNames
- names of attributes to be included into start tagattrValues
- values of attributes to be included into start tag -
there should be just as many entries as in attrNames
,
if a value is null
corresponding attribute will not be includedisEmpty
- decides wheter this is start tag is for an empty elementencodeAttrs
- set this to have your attribute values encoded for XMLquoteChar
- if you choose encoding this is the char that quotes
your attributespublic static final String createStartTag(String tagName, String[][] attrPairs, boolean isEmpty)
tagName
- name of the start tagattrPairs
- name/value pairs of attributes to be included into start tag -
if a value is null
corresponding attribute will not be includedisEmpty
- decides wheter this is start tag is for an empty elementpublic static final String createStartTag(String tagName, String[][] attrPairs)
tagName
- name of the start tagattrPairs
- name/value pairs of attributes to be included into start tag -
if a value is null
corresponding attribute will not be includedpublic static final String createEmptyTag(String tagName, String[][] attrPairs)
tagName
- name of the tagattrPairs
- name/value pairs of attributes to be included into tag -
if a value is null
corresponding attribute will not be includedcreateEmptyTag(String)
public static final String createStartTag(String tagName, String[][] attrPairs, boolean isEmpty, boolean encodeAttrs, char quoteChar)
tagName
- name of the start tagattrPairs
- name/value pairs of attributes to be included into start tag -
if a value is null
corresponding attribute will not be includedisEmpty
- decides wheter this is start tag is for an empty elementencodeAttrs
- set this to have your attribute values encoded for XMLquoteChar
- if you choose encoding this is the char that quotes
your attributespublic static final String createEmptyTag(String tagName, Attributes attributes)
tagName
- name of the tagattributes
- SAX attributes to be included into start tagcreateEmptyTag(String)
public static final String createStartTag(String tagName, Attributes attributes)
tagName
- name of the start tagattributes
- SAX attributes to be included into start tagpublic static final String createStartTag(String tagName, Attributes attributes, boolean isEmpty)
tagName
- name of the start tagattributes
- SAX attributes to be included into start tagisEmpty
- decides wheter this is start tag is for an empty elementpublic static final String createStartTag(String tagName, Attributes attributes, boolean isEmpty, boolean encodeAttrs, char quoteChar)
tagName
- name of the start tagattributes
- SAX attributes to be included into start tagisEmpty
- decides wheter this is start tag is for an empty elementencodeAttrs
- set this to have your attribute values encoded for XMLquoteChar
- if you choose encoding this is the char that quotes
your attributespublic static final void generateAndWriteElementWithCData(XMLWriter xmlWriter, String tagName, String[][] attrPairs, String cData) throws IOException
XMLOutputStreamWriter.generateAndWriteElementWithCData(writer, "tag", "cdata");
<tag>cdata</tag>
xmlWriter
- writer to write generated stuff totagName
- name of the elementattrPairs
- name/value pairs of attributes to be included into start tag -
if a value is null
corresponding attribute will not be includedcData
- the character data of the element
IOException
writeElementWithCData(String, String, String)
,
createStartTag(String, String[][])
,
createEndTag(String)
public static final void generateAndWriteElementWithCData(XMLWriter xmlWriter, String tagName, String[] attrNames, String[] attrValues, String cData) throws IOException
xmlWriter
- writer to write generated stuff totagName
- name of the elementattrNames
- names of attributes to be included into start tagattrValues
- values of attributes to be included into start tag -
there should be just as many entries as in attrNames
,
if a value is null
corresponding attribute will not be includedcData
- the character data of the element
IOException
generateAndWriteElementWithCData(XMLWriter, String, String[][], String)
,
writeElementWithCData(String, String, String)
,
createStartTag(String, String[], String[])
,
createEndTag(String)
public void setPrettyPrintMode(boolean prettyPrintMode)
public boolean getPrettyPrintMode()
setPrettyPrintMode(boolean)
.
public void setTabWidth(int tabWidth)
setPrettyPrintMode(boolean)
is set to true.
public int getTabWidth()
setTabWidth(int)
.
public void setNlAfterEmptyTag(boolean nlAfterEmptyTag)
public boolean getNlAfterEmptyTag()
setNlAfterEmptyTag(boolean)
.
public void setNlAfterEndTag(boolean nlAfterEndTag)
public boolean getNlAfterEndTag()
setNlAfterEndTag(boolean)
.
public void setNlAfterStartTag(boolean nlAfterStartTag)
public boolean getNlAfterStartTag()
setNlAfterStartTag(boolean)
.
public void writeXMLDeclaration() throws IOException
<?xml version="1.0"?>
writeProlog(String)
.xmlDeclWritten
to
true
.
IOException
public boolean isXMLDeclarationWritten()
public void setXMLDeclarationWritten(boolean xmlDeclWritten)
writeXMLDeclaration()
, but to give you
the full freedom, this can be done here as well.
Use isXMLDeclarationWritten()
to check it.
public void writeProlog(String prolog) throws IOException
FilterWriter.write(int)
methods.
IOException
public void writeNl() throws IOException
IOException
public void writeComment(String comment) throws IOException
comment
encoded as comment.
IOException
public void writePI(String target, String data) throws IOException
IOException
public void writeStartTag(String startTag, boolean nl) throws IOException
startTag
- the complete start tag, e.g. <start>
nl
- decides whether there should be a newline after the tag
IOException
public void writeStartTag(String startTag) throws IOException
startTag
- the complete start tag, e.g. <start>
IOException
setNlAfterStartTag(boolean)
public void writeEndTag(String endTag, boolean nl) throws IOException
endTag
- the complete end tag, e.g. </end>
nl
- decides whether there should be a newline after the tag
IOException
public void writeEndTag(String endTag) throws IOException
endTag
- the complete end tag, e.g. </end>
IOException
setNlAfterEndTag(boolean)
public void writeEmptyElement(String emptyTag, boolean nl) throws IOException
emptyTag
- the complete tag for an empty element, e.g. <empty/>
nl
- decides whether there should be a newline after the tag
IOException
public void writeEmptyElement(String emptyTag) throws IOException
emptyTag
- the complete tag for an empty element, e.g. <start/>
IOException
setNlAfterEmptyTag(boolean)
public void writeCData(String cData) throws IOException
cData
- the character data to write
IOException
public void writePCData(String pcData) throws IOException
pcData
- the parseable character data to write
IOException
public void writeElementWithCData(String startTag, String cData, String endTag) throws IOException
startTag
- the complete start tag, e.g. <element>
cData
- the character data to writeendTag
- the complete end tag, e.g. </element>
IOException
public void writeElementWithPCData(String startTag, String pcData, String endTag) throws IOException
startTag
- the complete start tag, e.g. <element>
pcData
- the parseable character data to writeendTag
- the complete end tag, e.g. </element>
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |