Class ContentPrinter
- All Implemented Interfaces:
ContentHandler
,LexicalHandler
ContentHandler
and
LexicalHandler
.
Can be parametrized w.r.t
- target of output (normally a
PrintWriter
or sim. - encoding
- format of output (indenting, compressed, debug only)
- whether to include doctype and/or xmltype declaration
CData sections and disable output escaping are not supported.
The encoding name which appears in the XML output must have a form like "UTF-8",
and "SHOULD be understood in a case-insensitive way." ([XML1.0], setion 4.3.3).
Similarly, the API doc for Charset
says "Charset names are not
case sensitive".
In this module all instances of the Charset class are replaced by their "canonical name".
To detect cases of UnsupportedEncodingException
are left to
the java runtime classes.
CHECK NOETIG !?!?!? Korrekte übersetzung nicht automatisch bei PrintWriter !?!?
Surrogate pairs are translated to Numeric character references,
BUT ONLY IN CDATA/PCDATA, not in Element names, attribute names,
pi targets, etc.
FIXME: currently PrintWriter is only needed for "println()".
This COULD be replaced by a Writer and "append( System property "line.separator",
PLUS an additional method to alter this explicitly!
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
protected boolean
protected boolean
protected @Opt XMLDocumentIdentifier
protected int
protected int
static final String
static final int
protected boolean
Only for compact debugging output: endtags are omitted and reflected by indentation only.protected Set<NamespaceName>
protected boolean
protected PrintWriter
protected boolean
protected boolean
static final String
static final String
-
Constructor Summary
ConstructorDescriptionContentPrinter
(OutputStream out, @Opt String selectedEncoding) Deprecated.ContentPrinter
(OutputStream out, @Opt String selectedEncoding, boolean pretty) Deprecated.ContentPrinter
(OutputStream out, @Opt String selectedEncoding, boolean pretty, boolean noend) Creates a ContentPrinter which can omit all end tags.ContentPrinter
(OutputStream out, @Opt String selectedEncoding, Set<NamespaceName> noindent) Creates a ContentPrinter which inserts indentation ONLY inside of elements which do not have mixed contents.ContentPrinter
(OutputStream out, Charset selectedEncoding) ContentPrinter
(OutputStream out, Charset selectedEncoding, boolean pretty) ContentPrinter
(OutputStream out, Charset selectedEncoding, boolean pretty, boolean noend) ContentPrinter
(OutputStream out, Charset selectedEncoding, Set<NamespaceName> noindent) ContentPrinter
(PrintWriter out, boolean pretty) ContentPrinter
(PrintWriter out, boolean pretty, boolean noend) ContentPrinter
(PrintWriter out, Set<NamespaceName> noindent) Creates a ContentPrinter which inserts indentation ONLY inside of elements which do not have mixed contents. -
Method Summary
Modifier and TypeMethodDescriptionvoid
characters
(char[] ch, int start, int length) void
comment
(char[] ch, int start, int length) Print comment to output; assume no double dashes and no trailing dash are contained [XSLT 1.0:7.4]
FIXME indentation, newline, etc.void
void
protected void
doWrite
(char[] ch, int start, int length, boolean inAtt, boolean replaceDoubleQuote) Prints character data in text contents (ie.(package private) void
doWriteErronuous_1
(char c) (package private) void
doWriteSurrogate
(char c1, char c2) Write the two java characters which form a "unicode surrogate pair" as one single "XML character entity", in hexadecimal format.void
void
endElement
(String namespaceURI, String localName, String qName) void
endPrefixMapping
(String prefix) void
flush()
protected void
close the open tag of an element.void
ignorableWhitespace
(char[] ch, int start, int length) protected void
indent()
Start a new line and advance to the column given byindent
, the current identation depth, reflecting open and close element events.protected void
void
processingInstruction
(String target, String data) setDeclareXml
(boolean declareXml) Whether an XML type declaration shall be printed at the start of the generated output.setDtdDocId
(XMLDocumentIdentifier dtdDocId) The dtd adress of the doctype declaration which will be printed inserted in the generated output, iff !=nullsetLineWidth
(int maxwidth) void
skippedEntity
(String name) void
void
startElement
(String namespaceURI, String localName, String qName, Attributes atts) void
startPrefixMapping
(String prefix, String uri) Methods inherited from class eu.bandm.tools.util.xml.DefaultContentHandler
endCDATA, endDTD, endEntity, setDocumentLocator, startCDATA, startDTD, startEntity
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Field Details
-
out
-
pretty
protected boolean pretty -
noendtags
protected boolean noendtagsOnly for compact debugging output: endtags are omitted and reflected by indentation only. -
noindent
-
selectedEncoding
-
declareXml
protected transient boolean declareXml -
dtdDocId
-
maxwidth
protected transient int maxwidth -
xml_version
- See Also:
-
surrogateErrorIndication
- See Also:
-
newAttLineIndent
- See Also:
-
newAttLineIndentLen
public static final int newAttLineIndentLen -
beforeFirstElement
protected transient boolean beforeFirstElement -
reindent
protected transient boolean reindent -
indent
protected transient int indent -
attcol
protected transient int attcol -
openTagPending
protected transient boolean openTagPending
-
-
Constructor Details
-
ContentPrinter
@Deprecated public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding) throws UnsupportedEncodingException Deprecated.Insert an encoder with the given encoding name between the output and the selected drain. If an XML declaration is selected bysetDeclareXml(boolean)
, then the encoding name will be included. The XML type is currently fixed to "XML 1.0
"- Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, Charset selectedEncoding) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
ContentPrinter
@Deprecated public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding, boolean pretty) throws UnsupportedEncodingException Deprecated.- Parameters:
out
- where to print the output toselectedEncoding
-pretty
- whether to insert indentations- Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, Charset selectedEncoding, boolean pretty) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding, boolean pretty, boolean noend) throws UnsupportedEncodingException Creates a ContentPrinter which can omit all end tags. This is useful only withpretty==true
, as a more compact rendering for human readers, e.g. for debugging.- Parameters:
out
- where to print the output toselectedEncoding
-pretty
- whether to insert indentations (always or-ed with "noend")noend
- whether to leave out the end tag- Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, Charset selectedEncoding, boolean pretty, boolean noend) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding, Set<NamespaceName> noindent) throws UnsupportedEncodingException Creates a ContentPrinter which inserts indentation ONLY inside of elements which do not have mixed contents. (In these, whitespace may have some significance.) The set of mixed contents elements is given by a set of NamespaceNames.- Parameters:
out
- where to print the output toselectedEncoding
-noindent
- elements in which no identation may happen- Throws:
UnsupportedEncodingException
-
ContentPrinter
public ContentPrinter(OutputStream out, Charset selectedEncoding, Set<NamespaceName> noindent) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
ContentPrinter
-
ContentPrinter
-
ContentPrinter
-
ContentPrinter
Creates a ContentPrinter which inserts indentation ONLY inside of elements which do not have mixed contents. (In these, whitespace may have some significance.) The set of mixed contents elements is given by a set of NamespaceNames.- Parameters:
out
- where to print the output tonoindent
- elements in which no identation may happen
-
-
Method Details
-
setDeclareXml
Whether an XML type declaration shall be printed at the start of the generated output. -
setDtdDocId
The dtd adress of the doctype declaration which will be printed inserted in the generated output, iff !=null -
setLineWidth
-
doWriteErronuous_1
void doWriteErronuous_1(char c) -
doWriteSurrogate
void doWriteSurrogate(char c1, char c2) Write the two java characters which form a "unicode surrogate pair" as one single "XML character entity", in hexadecimal format. -
doWrite
protected void doWrite(char[] ch, int start, int length, boolean inAtt, boolean replaceDoubleQuote) Prints character data in text contents (ie. cdata sax events) and attribute nodes.
Prints some characters as named character references, according to [XML 1.0:4.6]. (Not all these rules are sensible, see our whitepaper.)
Prints surrogate pairs as XML numeric character reference "&#x...;
".In attribute values the newline character
\n
is replaced by the text of a character entity reference, namely "

", see the last paragraph in XSLT 1.0: 7.1.3ATTENTION assume surrogate pairs are completely (=both chars) contained in the character range to print. In cases of errors with surrogate pairs, an error indicating text
surrogateErrorIndication
is inserted in the output. -
comment
Print comment to output; assume no double dashes and no trailing dash are contained [XSLT 1.0:7.4]
FIXME indentation, newline, etc.- Specified by:
comment
in interfaceLexicalHandler
- Overrides:
comment
in classDefaultContentHandler
- Throws:
SAXException
-
comment
- Throws:
SAXException
-
comment
- Throws:
SAXException
-
ignorableWhitespace
Description copied from class:DefaultContentHandler
- Specified by:
ignorableWhitespace
in interfaceContentHandler
- Overrides:
ignorableWhitespace
in classDefaultContentHandler
- Throws:
SAXException
-
characters
public void characters(char[] ch, int start, int length) Description copied from class:DefaultContentHandler
- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classDefaultContentHandler
-
endElement
Description copied from class:DefaultContentHandler
- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classDefaultContentHandler
-
processingInstruction
Description copied from class:DefaultContentHandler
- Specified by:
processingInstruction
in interfaceContentHandler
- Overrides:
processingInstruction
in classDefaultContentHandler
-
skippedEntity
Description copied from class:DefaultContentHandler
- Specified by:
skippedEntity
in interfaceContentHandler
- Overrides:
skippedEntity
in classDefaultContentHandler
-
startDocument
public void startDocument()Description copied from class:DefaultContentHandler
- Specified by:
startDocument
in interfaceContentHandler
- Overrides:
startDocument
in classDefaultContentHandler
-
startElement
Description copied from class:DefaultContentHandler
- Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classDefaultContentHandler
-
printAtt
-
startPrefixMapping
Description copied from class:DefaultContentHandler
- Specified by:
startPrefixMapping
in interfaceContentHandler
- Overrides:
startPrefixMapping
in classDefaultContentHandler
-
endPrefixMapping
Description copied from class:DefaultContentHandler
- Specified by:
endPrefixMapping
in interfaceContentHandler
- Overrides:
endPrefixMapping
in classDefaultContentHandler
-
flushPending
protected void flushPending()close the open tag of an element. (is kept open for the case that the end tag arrives without intervening contents, and an empty tag can be created instead. -
flush
public void flush() -
endDocument
public void endDocument()Description copied from class:DefaultContentHandler
- Specified by:
endDocument
in interfaceContentHandler
- Overrides:
endDocument
in classDefaultContentHandler
-
indent
protected void indent()
-
ContentPrinter(java.io.OutputStream,java.nio.charset.Charset)
instead