RichTextContentHandler extends WriteOutContentHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if ("img".equals(localName) && attributes.getValue("alt") != null) { String nfo = "[image: " + attributes.getValue("alt") + ']'; characters(nfo.toCharArray(), 0, nfo.length()); } if ("a".equals(localName) && attributes.getValue("name") != null) { String nfo = "[bookmark: " + attributes.getValue("name") + ']'; characters(nfo.toCharArray(), 0, nfo.length()); } } RichTextContentHandler(Writer writer); @Override void startElement(String uri, String localName, String qName, Attributes attributes);  }
@Test public void aTagTest() throws Exception { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XHTMLContentHandler xhtml = new XHTMLContentHandler( new RichTextContentHandler( new OutputStreamWriter(buffer, Charset.defaultCharset())), new Metadata()); xhtml.startDocument(); AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute("", "", "name", "", "value"); xhtml.startElement("a", attributes); xhtml.endDocument(); assertEquals("\n\n\n\n[bookmark: value]", buffer.toString(UTF_8.name())); }
@Test public void imgTagTest() throws Exception { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XHTMLContentHandler xhtml = new XHTMLContentHandler( new RichTextContentHandler( new OutputStreamWriter(buffer, Charset.defaultCharset())), new Metadata()); xhtml.startDocument(); AttributesImpl attributes = new AttributesImpl(); attributes.addAttribute("", "", "alt", "", "value"); xhtml.startElement("img", attributes); xhtml.endDocument(); assertEquals("\n\n\n\n[image: value]", buffer.toString(UTF_8.name())); }