Class ContentPrinter
- All Implemented Interfaces:
ContentHandler,LexicalHandler
ContentHandler and
LexicalHandler.
Can be parametrized w.r.t
- target of output (normally a
PrintWriteror 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("\n")«,
PLUS an additional method to alter this explicitly!
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected booleanprotected booleanprotected @Opt XMLDocumentIdentifierprotected intprotected booleanprotected intstatic final Stringstatic final intprotected booleanOnly for compact debugging output: endtags are omitted and reflected by indentation only.protected Set<NamespaceName> protected booleanprotected PrintWriterprotected booleanprotected booleanstatic final Stringstatic final String -
Constructor Summary
ConstructorsConstructorDescriptionContentPrinter(OutputStream out, @Opt String selectedEncoding) Deprecated, for removal: This API element is subject to removal in a future version.ContentPrinter(OutputStream out, @Opt String selectedEncoding, boolean pretty) Deprecated, for removal: This API element is subject to removal in a future version.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 TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) voidcomment(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.voidvoidprotected voiddoWrite(char[] ch, int start, int length, boolean inAtt, boolean replaceDoubleQuote) Prints character data in text contents (ie.(package private) voiddoWriteErronuous_1(char c) (package private) voiddoWriteSurrogate(char c1, char c2) Write the two java characters which form a "unicode surrogate pair" as one single "XML character entity", in hexadecimal format.voidvoidendElement(String namespaceURI, String localName, String qName) voidendPrefixMapping(String prefix) voidflush()protected voidclose the open tag of an element.voidignorableWhitespace(char[] ch, int start, int length) protected voidindent()Start a new line and advance to the column given byindent, the current identation depth, reflecting open and close element events.protected voidvoidprocessingInstruction(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) voidsetPretty(boolean pretty) voidskippedEntity(String name) voidPrints the XML declaration.voidstartElement(String namespaceURI, String localName, String qName, Attributes atts) Prints an open tag of the given element, with all given attributes.voidstartPrefixMapping(String prefix, String uri) Methods inherited from class eu.bandm.tools.util.xml.DefaultContentHandler
endCDATA, endDTD, endEntity, setDocumentLocator, startCDATA, startDTD, startEntityMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:
-
SURROGATE_ERROR_INDICATION
- See Also:
-
NEW_ATTLINE_INDENT
- See Also:
-
NEW_ATTLINE_INDENT_LEN
public static final int NEW_ATTLINE_INDENT_LEN -
beforeFirstElement
protected transient boolean beforeFirstElement -
initialComment
protected transient boolean initialComment -
reindent
protected transient boolean reindent -
indent
protected transient int indent -
attcol
protected transient int attcol -
openTagPending
protected transient boolean openTagPending
-
-
Constructor Details
-
ContentPrinter
@Deprecated(forRemoval=true) public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding) throws UnsupportedEncodingException Deprecated, for removal: This API element is subject to removal in a future version.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(forRemoval=true) public ContentPrinter(OutputStream out, @Opt @Opt String selectedEncoding, boolean pretty) throws UnsupportedEncodingException Deprecated, for removal: This API element is subject to removal in a future version.- 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
-
setPretty
public void setPretty(boolean pretty) -
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
\nis 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
SURROGATE_ERROR_INDICATIONis 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:
commentin interfaceLexicalHandler- Overrides:
commentin classDefaultContentHandler- Throws:
SAXException
-
comment
- Throws:
SAXException
-
comment
- Throws:
SAXException
-
ignorableWhitespace
Description copied from class:DefaultContentHandler- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultContentHandler- Throws:
SAXException
-
characters
public void characters(char[] ch, int start, int length) Description copied from class:DefaultContentHandler- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultContentHandler
-
endElement
Description copied from class:DefaultContentHandler- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultContentHandler
-
processingInstruction
Description copied from class:DefaultContentHandler- Specified by:
processingInstructionin interfaceContentHandler- Overrides:
processingInstructionin classDefaultContentHandler
-
skippedEntity
Description copied from class:DefaultContentHandler- Specified by:
skippedEntityin interfaceContentHandler- Overrides:
skippedEntityin classDefaultContentHandler
-
startDocument
public void startDocument()Prints the XML declaration. This is "version='1.0'", and the encoding, if it is known here.- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultContentHandler
-
startElement
Prints an open tag of the given element, with all given attributes. If this is the very first call, then the document type definition is printed first. This cannot be done earlier and independently, because the topmost element name must be (redundantly) named therein.- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultContentHandler
-
printAtt
-
startPrefixMapping
Description copied from class:DefaultContentHandler- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classDefaultContentHandler
-
endPrefixMapping
Description copied from class:DefaultContentHandler- Specified by:
endPrefixMappingin interfaceContentHandler- Overrides:
endPrefixMappingin 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:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultContentHandler
-
indent
protected void indent()
-
ContentPrinter(java.io.OutputStream,java.nio.charset.Charset)instead