public class HtmlSerializer
extends java.lang.Object
implements org.xml.sax.ContentHandler
OutputStream
as a UTF-8-encoded HTML 4.01 Strict
document. The SAX events must represent a valid XHTML 1.0 document, except
the namespace prefixes don't matter and there may be
startElement
and endElement
calls for elements
from other namespaces. The startElement
and
endElement
calls for non-XHTML elements are ignored. No
validity checking is performed. Hence, the emitter of the SAX events is
responsible for making sure the events represent a document that meets the
above requirements. The OutputStream
is closed when the end of
the document is seen.Modifier and Type | Field and Description |
---|---|
static int |
DOCTYPE_HTML401_STRICT |
static int |
DOCTYPE_HTML401_TRANSITIONAL |
static int |
DOCTYPE_HTML5 |
static int |
NO_DOCTYPE |
protected java.io.Writer |
writer
The writer used for output
|
Constructor and Description |
---|
HtmlSerializer(java.io.OutputStream out)
Creates a new instance of HtmlSerializer in the HTML 4.01 doctype mode
with the UTF-8 encoding and no charset meta.
|
HtmlSerializer(java.io.OutputStream out,
int doctype,
boolean emitMeta) |
HtmlSerializer(java.io.OutputStream out,
int doctype,
boolean emitMeta,
java.lang.String enc) |
Modifier and Type | Method and Description |
---|---|
void |
characters(char[] ch,
int start,
int length)
Writes out characters.
|
void |
endDocument()
Must be called in the end.
|
void |
endElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName)
Writes an end tag if the element is an XHTML element and is not an empty
element in HTML 4.01 Strict.
|
void |
endPrefixMapping(java.lang.String str)
Does nothing.
|
void |
ignorableWhitespace(char[] values,
int param,
int param2)
Does nothing.
|
static void |
main(java.lang.String[] args)
Used for testing.
|
void |
processingInstruction(java.lang.String str,
java.lang.String str1)
Does nothing.
|
void |
setDocumentLocator(org.xml.sax.Locator locator)
Does nothing.
|
void |
skippedEntity(java.lang.String str)
Does nothing.
|
void |
startDocument()
Must be called first.
|
void |
startElement(java.lang.String namespaceURI,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes atts)
Writes a start tag if the element is an XHTML element.
|
void |
startPrefixMapping(java.lang.String str,
java.lang.String str1)
Does nothing.
|
public static final int NO_DOCTYPE
public static final int DOCTYPE_HTML401_TRANSITIONAL
public static final int DOCTYPE_HTML401_STRICT
public static final int DOCTYPE_HTML5
protected java.io.Writer writer
public HtmlSerializer(java.io.OutputStream out)
out
- the stream to which the output is writtenpublic HtmlSerializer(java.io.OutputStream out, int doctype, boolean emitMeta)
public HtmlSerializer(java.io.OutputStream out, int doctype, boolean emitMeta, java.lang.String enc)
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
characters
in interface org.xml.sax.ContentHandler
ch
- the source arraystart
- the index of the first character to be writtenlength
- the number of characters to writeorg.xml.sax.SAXException
- if there are IO problemspublic void endDocument() throws org.xml.sax.SAXException
endDocument
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
- if there are IO problemspublic void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
endElement
in interface org.xml.sax.ContentHandler
namespaceURI
- the XML namespacelocalName
- the element name in the namespaceqName
- ignoredorg.xml.sax.SAXException
- if there are IO problemspublic void startDocument() throws org.xml.sax.SAXException
startDocument
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXException
startElement
in interface org.xml.sax.ContentHandler
namespaceURI
- the XML namespacelocalName
- the element name in the namespaceqName
- ignoredatts
- the attribute listorg.xml.sax.SAXException
- if there are IO problemspublic static void main(java.lang.String[] args)
public void endPrefixMapping(java.lang.String str) throws org.xml.sax.SAXException
endPrefixMapping
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void ignorableWhitespace(char[] values, int param, int param2) throws org.xml.sax.SAXException
ignorableWhitespace
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void processingInstruction(java.lang.String str, java.lang.String str1) throws org.xml.sax.SAXException
processingInstruction
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void setDocumentLocator(org.xml.sax.Locator locator)
setDocumentLocator
in interface org.xml.sax.ContentHandler
public void skippedEntity(java.lang.String str) throws org.xml.sax.SAXException
skippedEntity
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException
public void startPrefixMapping(java.lang.String str, java.lang.String str1) throws org.xml.sax.SAXException
startPrefixMapping
in interface org.xml.sax.ContentHandler
org.xml.sax.SAXException