Class ToMarkdownContentHandler
- All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler
The handler builds a
commonmark-java document model from the SAX event stream and renders it
with commonmark's MarkdownRenderer. Document text is added to the
model as raw literals, so escaping of Markdown metacharacters — including
characters that would otherwise break out of a link, image, or table cell —
is performed in one place by the renderer rather than at each emit site.
The handler tolerates malformed input (unbalanced or misnested tags): block elements are always attached at a structurally valid point so a well-formed document model is rendered regardless of the event stream.
Content within <script> and <style> tags is ignored.
- Since:
- Apache Tika 3.2
-
Constructor Summary
ConstructorsConstructorDescriptionToMarkdownContentHandler(OutputStream stream, String encoding) ToMarkdownContentHandler(Writer writer) -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) voidvoidendElement(String uri, String localName, String qName) voidignorableWhitespace(char[] ch, int start, int length) voidstartElement(String uri, String localName, String qName, Attributes atts) toString()voidRenders the document model accumulated so far to the underlying writer and flushes it, if it has not already been written.Methods inherited from class DefaultHandler
endPrefixMapping, error, fatalError, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warningMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ContentHandler
declaration
-
Constructor Details
-
ToMarkdownContentHandler
-
ToMarkdownContentHandler
public ToMarkdownContentHandler(OutputStream stream, String encoding) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
ToMarkdownContentHandler
public ToMarkdownContentHandler()
-
-
Method Details
-
startElement
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException - Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXException
-
endElement
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Throws:
SAXException
-
characters
- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler- Throws:
SAXException
-
endDocument
- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classDefaultHandler- Throws:
SAXException
-
writePartialContentIfUnfinished
Renders the document model accumulated so far to the underlying writer and flushes it, if it has not already been written.This handler buffers the whole document and only writes it to the writer in
endDocument(), which a parser does not call when it aborts mid-document (a parse exception, or a write-limit abort withthrowOnWriteLimitReached=true). Unlike the streaming text/HTML/XML handlers, that would leave anOutputStream/Writerconsumer with an empty result. A parse driver that owns the writer can call this in afinallyso partial Markdown is still emitted on failure. Idempotent: a no-op once the content has been written (including after a successfulendDocument()).- Throws:
IOException- if writing to or flushing the underlying writer fails
-
toString
-