Wraps another
XMLEventWriter and does the indentation.
XMLEventWriter API doesn't provide any portable way of
doing pretty-printing. This
XMLEventWriter filter provides
a portable indentation support by wrapping another
XMLEventWriter and adding proper
Characters event for indentation.
Because whitespace handling in XML is tricky, this is not an
one-size-fit-all indentation engine. Instead, this class is
focused on handling so-called "data-oritented XML" like follows:
Kohsuke
Kawaguchi
We'll discuss more about the supported subset of XML later.
To use this engine, do as follows:
XMLEventWriter w = xmlOutputFactory.createXMLEventWriter(...);
w = new
IndentingXMLEventWriter (w);
// start writing
Use
IndentingXMLEventWriter.setIndent(String) and
IndentingXMLEventWriter.setNewLine(String) to
control the indentation if you want.
What Subset Does This Support?
This engine works when the content model of each element is either
element-only or #PCDATA (but not mixed content model.) IOW, it
assumes that the children of any element is either (a) only elements
and no #PCDATA or (b) #PCDATA only and no elements.
The engine also tries to handle comments, PIs, and a DOCTYPE decl,
but in general it works only when those things appear in the
element-only content model.
For Maintainers
Please don't try to make this class into an almighty indentation class.
I've seen it attempted in Xerces and it's not gonna be pretty.
If you come up with an idea of another pretty-printer
that supports another subset, please go ahead and write your own class.
author: Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |