Package org.htmlunit.cyberneko.filters
Class HTMLWriterFilter
- java.lang.Object
-
- org.htmlunit.cyberneko.filters.DefaultFilter
-
- org.htmlunit.cyberneko.filters.HTMLWriterFilter
-
- All Implemented Interfaces:
HTMLComponent
,XMLComponent
,XMLDocumentFilter
,XMLDocumentSource
,XMLDocumentHandler
public class HTMLWriterFilter extends DefaultFilter
An HTML writer written as a filter. Besides serializing the HTML event stream, the writer also passes the document events to the next stage in the pipeline. This allows applications to insert writer filters between other custom filters for debugging purposes.Since an HTML document may have specified its encoding using the <META> tag and http-equiv/content attributes, the writer will automatically change any character set specified in this tag to match the encoding of the output stream. Therefore, the character encoding name used to construct the writer should be an official IANA encoding name and not a Java encoding name.
Note: The modified character set in the <META> tag is not propagated to the next stage in the pipeline. The changed value is only output to the stream; the original value is sent to the next stage in the pipeline.
-
-
Field Summary
Fields Modifier and Type Field Description private int
elementDepth_
Element depth.private java.lang.String
encoding_
The encoding.private HTMLElements
htmlElements_
private boolean
normalize_
Normalize character content.private java.io.PrintWriter
printer_
The print writer used for serializing the document with the appropriate character encoding.private boolean
seenRootElement_
Seen root element.
-
Constructor Summary
Constructors Constructor Description HTMLWriterFilter()
Constructs a writer filter that prints to standard out.HTMLWriterFilter(java.io.OutputStream outputStream, java.lang.String encoding)
Constructs a writer filter using the specified output stream and encoding.HTMLWriterFilter(java.io.Writer writer, java.lang.String encoding, HTMLElements htmlElements)
Constructs a writer filter using the specified Java writer and encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
characters(XMLString text, Augmentations augs)
Characters.void
comment(XMLString text, Augmentations augs)
Comment.void
emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
Empty element.void
endElement(QName element, Augmentations augs)
End element.protected int
getElementDepth()
protected boolean
getNormalize()
protected boolean
hasSeenRootElement()
protected void
print(java.lang.String content)
protected void
printAttributeValue(java.lang.String text)
Print attribute value.protected void
printCharacters(XMLString text, boolean normalize)
Print characters.protected void
printEndElement(QName element)
Print end element.protected void
printStartElement(QName element, XMLAttributes attributes)
Print start element.void
startDocument(XMLLocator locator, java.lang.String encoding, NamespaceContext nscontext, Augmentations augs)
Start document.void
startElement(QName element, XMLAttributes attributes, Augmentations augs)
Start element.-
Methods inherited from class org.htmlunit.cyberneko.filters.DefaultFilter
doctypeDecl, endCDATA, endDocument, getDocumentHandler, getDocumentSource, getFeatureDefault, getPropertyDefault, getRecognizedFeatures, getRecognizedProperties, merge, processingInstruction, reset, setDocumentHandler, setDocumentSource, setFeature, setProperty, startCDATA, xmlDecl
-
-
-
-
Field Detail
-
encoding_
private java.lang.String encoding_
The encoding.
-
printer_
private java.io.PrintWriter printer_
The print writer used for serializing the document with the appropriate character encoding.
-
seenRootElement_
private boolean seenRootElement_
Seen root element.
-
elementDepth_
private int elementDepth_
Element depth.
-
normalize_
private boolean normalize_
Normalize character content.
-
htmlElements_
private final HTMLElements htmlElements_
-
-
Constructor Detail
-
HTMLWriterFilter
public HTMLWriterFilter()
Constructs a writer filter that prints to standard out.
-
HTMLWriterFilter
public HTMLWriterFilter(java.io.OutputStream outputStream, java.lang.String encoding) throws java.io.UnsupportedEncodingException
Constructs a writer filter using the specified output stream and encoding.- Parameters:
outputStream
- The output stream to write to.encoding
- The encoding to be used for the output. The encoding name should be an official IANA encoding name.- Throws:
java.io.UnsupportedEncodingException
-
HTMLWriterFilter
public HTMLWriterFilter(java.io.Writer writer, java.lang.String encoding, HTMLElements htmlElements)
Constructs a writer filter using the specified Java writer and encoding.- Parameters:
writer
- The Java writer to write to.encoding
- The encoding to be used for the output. The encoding name should be an official IANA encoding name.
-
-
Method Detail
-
startDocument
public void startDocument(XMLLocator locator, java.lang.String encoding, NamespaceContext nscontext, Augmentations augs) throws XNIException
Start document.- Specified by:
startDocument
in interfaceXMLDocumentHandler
- Overrides:
startDocument
in classDefaultFilter
- Parameters:
locator
- The document locator, or null if the document location cannot be reported during the parsing of this document. However, it is strongly recommended that a locator be supplied that can at least report the system identifier of the document.encoding
- The auto-detected IANA encoding name of the entity stream. This value will be null in those situations where the entity encoding is not auto-detected (e.g. internal entities or a document entity that is parsed from a java.io.Reader).nscontext
- The namespace context in effect at the start of this document. This object represents the current context. Implementors of this class are responsible for copying the namespace bindings from the the current context (and its parent contexts) if that information is important.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
comment
public void comment(XMLString text, Augmentations augs) throws XNIException
Comment.- Specified by:
comment
in interfaceXMLDocumentHandler
- Overrides:
comment
in classDefaultFilter
- Parameters:
text
- The text in the comment.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by application to signal an error.
-
startElement
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException
Start element.- Specified by:
startElement
in interfaceXMLDocumentHandler
- Overrides:
startElement
in classDefaultFilter
- Parameters:
element
- The name of the element.attributes
- The element attributes.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
emptyElement
public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException
Empty element.- Specified by:
emptyElement
in interfaceXMLDocumentHandler
- Overrides:
emptyElement
in classDefaultFilter
- Parameters:
element
- The name of the element.attributes
- The element attributes.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
characters
public void characters(XMLString text, Augmentations augs) throws XNIException
Characters.- Specified by:
characters
in interfaceXMLDocumentHandler
- Overrides:
characters
in classDefaultFilter
- Parameters:
text
- The content.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
endElement
public void endElement(QName element, Augmentations augs) throws XNIException
End element.- Specified by:
endElement
in interfaceXMLDocumentHandler
- Overrides:
endElement
in classDefaultFilter
- Parameters:
element
- The name of the element.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
printAttributeValue
protected void printAttributeValue(java.lang.String text)
Print attribute value.
-
printCharacters
protected void printCharacters(XMLString text, boolean normalize)
Print characters.
-
printStartElement
protected void printStartElement(QName element, XMLAttributes attributes)
Print start element.
-
printEndElement
protected void printEndElement(QName element)
Print end element.
-
print
protected void print(java.lang.String content)
-
hasSeenRootElement
protected boolean hasSeenRootElement()
-
getElementDepth
protected int getElementDepth()
-
getNormalize
protected boolean getNormalize()
-
-