public void write(OutputStream outputStream) throws IOException, WebApplicationException { Writer writer = new OutputStreamWriter(outputStream, UTF_8); BodyContentHandler body = new BodyContentHandler(new RichTextContentHandler(writer)); parse(parser, LOG, info.getPath(), is, body, metadata, context); } };
@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()); } } }
ch = new BodyContentHandler(new RichTextContentHandler(new OutputStreamWriter(text, UTF_8))); } else { ch = new DefaultHandler();
@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()); } } }
/** * Test to check img tags are detected and rich text version used. */ @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())); }
@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()); } } }
/** * Test to check a tags are detected and rich text version used. */ @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())); }