|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object tribble.xml.XmlOutputStreamAdapter
Generic XML output stream.
This default implementation of the XmlOutputStreamI
interface provides
a very simple functional XML output stream.
Example
We want to generate the following XML document:
<?xml version='1.0' stand-alone='yes'?> <emp:employee type='perm' ssn='647-37-0999'> <emp:name>John Q. Smith</emp:name> <emp:address> <emp:street>3817 Newhaven Dr.</emp:street> <emp:city>Dallas</emp:city> <emp:state>TX</emp:state> <emp:zip>75022</emp:zip> </emp:address> <emp:hire when='2001-02-11'/> <emp:dept>Customer Support A</emp:dept> </emp:employee>
First we create an XML output stream and initialize it:
Writer out; XmlOutputStreamI xml; Properties props; out = ...; xml = new XmlOutputStreamAdapter(out); props = ...; xml.initialize(props);
Next we specify that we want an <?xml> directive tag in the output with a stand-alone='yes' attribute:
xml.startDirective("xml"); xml.addAttribute("stand-alone", "yes"); xml.endDirective();
Next we start the outermost <emp:employee> tag, and set its attributes:
xml.startTag("emp", "employee"); xml.addAttribute("type", "perm"); xml.addAttribute("ssn", "647-37-0999");
For convenience, we can set the namespace prefix for all of the remaining
tags, since they are all the same (emp:), and use the simpler form
of the startTag()
method:
xml.setNamespace("emp");
Next we add the <emp:name> tag and its text content:
xml.startTag("name"); xml.addText("John Q. Smith"); xml.endTag();
Likewise, we continue in a similar fashion for the remaining XML tags:
xml.startTag("address"); xml.startTag("street"); xml.addText("3817 Newhaven Dr."); xml.endTag(); xml.startTag("city"); xml.addText("Dallas"); xml.endTag(); xml.startTag("state"); xml.addText("TX"); xml.endTag(); xml.startTag("zip"); xml.addText("75022"); xml.endTag(); xml.endTag(); // Close the <emp:address> tag xml.startTag("hire"); xml.addAttribute("when", "2001-02-11"); xml.endTag(); xml.startTag("dept"); xml.addText("Customer Support A"); xml.endTag();
Next we terminate the outermost XML tag (emp:employee):
xml.endTag(); // Close the <emp:employee> tag
Finally we flush any pending output for the output stream and then close it:
xml.flush(); xml.close();
Field Summary | |
protected static java.lang.String |
ENT_AMP
Character entity: '&'. |
protected static java.lang.String |
ENT_APOS
Character entity: '''. |
protected static java.lang.String |
ENT_GT
Character entity: '>'. |
protected static java.lang.String |
ENT_LT
Character entity: '<'. |
protected static java.lang.String |
ENT_QUOT
Character entity: '"'. |
protected int |
m_colNo
Current output column position (starting at 1). |
protected java.lang.String |
m_dir
Current directive tag. |
protected java.lang.String |
m_elem
Current element tag. |
protected java.lang.String |
m_namesp
Default XML namespace prefix. |
protected int |
m_nest
XML element node nesting level. |
protected java.io.Writer |
m_out
Output stream. |
protected java.util.ArrayList |
m_prefixes
Nested element namespace prefixes stack. |
protected short |
m_state
Current XML output state. |
protected java.util.ArrayList |
m_tags
Nested element tags stack. |
static int |
SERIES
API series number. |
protected static short |
ST_DIR
Output state: Within a directive. |
protected static short |
ST_TAG
Output state: Within an element tag. |
protected static short |
ST_TEXT
Output state: Within an element text content. |
Constructor Summary | |
XmlOutputStreamAdapter(java.io.Writer out)
Constructor. |
Method Summary | |
void |
addAttribute(java.lang.String attr,
java.lang.String val)
Add an attribute value to the current XML tag. |
void |
addCharacter(int ch)
Append a character to the text content of the current XML tag. |
void |
addText(java.lang.String text)
Append text content to the current XML tag. |
void |
close()
Close this XML output stream. |
void |
endDirective()
Terminate the current directive tag for this XML output stream. |
void |
endTag()
Terminate the current element tag for this XML output stream. |
protected void |
finalize()
Finalization. |
void |
flush()
Flush any pending output to this XML output stream. |
void |
initialize(java.util.Hashtable parms)
Initialize this XML output stream. |
static void |
main(java.lang.String[] args)
Test driver for this class. |
void |
setNamespace(java.lang.String namesp)
Establish the namespace prefix for XML tags written to this output stream. |
void |
startDirective(java.lang.String name)
Write a directive tag to this XML output stream. |
void |
startTag(java.lang.String tag)
Write an element tag to this XML output stream. |
void |
startTag(java.lang.String namesp,
java.lang.String tag)
Write an element tag to this XML output stream. |
protected void |
writeName(java.lang.String name)
Write an identifier name to this XML output stream. |
protected void |
writeQuoted(java.lang.String val)
Write a quoted attribute string to this XML output stream. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int SERIES
protected static final java.lang.String ENT_AMP
protected static final java.lang.String ENT_LT
protected static final java.lang.String ENT_GT
protected static final java.lang.String ENT_APOS
protected static final java.lang.String ENT_QUOT
protected static final short ST_TEXT
protected static final short ST_DIR
protected static final short ST_TAG
protected java.io.Writer m_out
protected java.lang.String m_namesp
protected java.lang.String m_elem
protected java.lang.String m_dir
protected java.util.ArrayList m_tags
protected java.util.ArrayList m_prefixes
protected int m_colNo
protected int m_nest
protected short m_state
Constructor Detail |
public XmlOutputStreamAdapter(java.io.Writer out)
out
- The text output stream to which the XML document is to be written.Method Detail |
public static void main(java.lang.String[] args) throws java.lang.Exception
This writes a sample XML document to the standard output stream. See the Example section for details.
args
- Command line arguments.
java.lang.Exception
public void initialize(java.util.Hashtable parms)
This must be the first method to be called on this object, prior to adding any tags or attributes.
This implementation of this method does not do anything. Implementations that extend this class may override this method to have it do something useful.
initialize
in interface XmlOutputStreamI
parms
- A hash table containing name/value pairs with which to initialize this
XML output stream. This argument may be allowed to be null, depending on
the implementation of this interface.
java.io.IOException
- Thrown if the specified hash table entries are malformed, or if some other
error occurs.close()
public void flush() throws java.io.IOException
flush
in interface XmlOutputStreamI
java.io.IOException
- Thrown if an I/O (output) error occurs.close()
public void close() throws java.io.IOException
This must be the last method to be called on this XML output object.
Implementations of this method typically should call the
flush()
method.
close
in interface XmlOutputStreamI
java.io.IOException
- Thrown if an I/O (output) error occurs.flush()
public void setNamespace(java.lang.String namesp) throws java.io.IOException
This setting stays in force until a subsequent call to this method changes it.
setNamespace
in interface XmlOutputStreamI
namesp
- The default namespace prefix for subsequent XML tags. Note that the
':' separator should not be included. This argument can
be empty ("") or null, in which case there is no default
namespace prefix.
java.io.IOException
- Thrown if an I/O (write) error occurs.public void startDirective(java.lang.String name) throws java.io.IOException
Attributes can be added to a directive using the
addAttribute()
method.
The directive must be terminated by calling the
endDirective()
method.
startDirective
in interface XmlOutputStreamI
name
- A directive tag name.
For example, a name of "perms" causes an XML directive tag of
<?perms?> to be written.
java.io.IOException
- Thrown if an I/O (write) error occurs.endDirective()
,
addAttribute()
public void endDirective() throws java.io.IOException
endDirective
in interface XmlOutputStreamI
java.io.IOException
- Thrown if an I/O (write) error occurs.startDirective()
public void startTag(java.lang.String namesp, java.lang.String tag) throws java.io.IOException
The new tag will be nested within the previously unterminated tag, if there is one.
The XML tag must be terminated by calling the
endTag()
method.
startTag
in interface XmlOutputStreamI
namesp
- The namespace prefix of the tag, without the ':' separator.
This argument can be empty ("") or null, in which case the tag
has no namespace prefix.tag
- The name of the tag. For example, a name of "ssn" and a
namespace of "emp" will be written to the XML output stream as
<emp:ssn>.
java.io.IOException
- Thrown if an I/O (write) error occurs.endTag()
,
startTag()
public void startTag(java.lang.String tag) throws java.io.IOException
The new tag will be nested within the previously unterminated tag, if there is one. The new tag will use the default namespace prefix, if one has been established.
The XML tag must be terminated by calling the
endTag()
method.
startTag
in interface XmlOutputStreamI
tag
- The name of the tag. For example, a name of "ssn" and a default
namespace of "emp" will be written to the XML output stream as
<emp:ssn>.
java.io.IOException
- Thrown if an I/O (write) error occurs.setNamespace()
,
endTag()
,
startTag()
public void addAttribute(java.lang.String attr, java.lang.String val) throws java.io.IOException
addAttribute
in interface XmlOutputStreamI
attr
- An attribute name.val
- The text value of the attribute.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
public void addText(java.lang.String text) throws java.io.IOException
This method may be called multiple times, each call appending additional text characters to the content of the current XML tag.
Unprintable characters (e.g., control characters) and special characters
(e.g., angle brackets '<' and '>') are written to
the output stream as XML character entities (e.g.,  or
<).
Unprintable characters can also be written explicitly using the
addCharacter()
method.
Each newline sequence (CR \u000D, LF \u000A, or a CR LF pair) is written as a single canonical newline, according to the conventions of the underlying output stream.
Note that this method calls the addCharacter()
method
for each character in text.
addText
in interface XmlOutputStreamI
text
- Text characters to append to the content of the current XML tag.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
addCharacter()
public void addCharacter(int ch) throws java.io.IOException
This method may be called multiple times, each call appending additional text characters to the content of the current XML tag.
Printable characters are written as is to the XML output stream, according to the character encoding of the underlying output stream. Unprintable characters (e.g., control characters) are written as character entities (&#dd;).
A newline sequence (CR \u000D, LF \u000A, or a CR LF pair) is written as a single canonical newline, according to the conventions of the underlying output stream.
addCharacter
in interface XmlOutputStreamI
ch
- A text character code to append to the content of the current XML tag.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
addText()
public void endTag() throws java.io.IOException
endTag
in interface XmlOutputStreamI
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
startTag()
protected void finalize() throws java.lang.Throwable
java.lang.Throwable
protected void writeName(java.lang.String name) throws java.io.IOException
name
- An identifier, i.e., an element tag, directive tag, or attribute name.
java.io.IOException
- Thrown if an I/O (write) error occurs.protected void writeQuoted(java.lang.String val) throws java.io.IOException
val
- The text value of the attribute.
java.io.IOException
- Thrown if an I/O (write) error occurs.addAttribute()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |