@Test public void testHTML() throws Exception { Parser p = new MockParser(OVER_DEFAULT); BasicContentHandlerFactory.HANDLER_TYPE type = BasicContentHandlerFactory.HANDLER_TYPE.HTML; ContentHandler handler = new BasicContentHandlerFactory(type, -1).getNewContentHandler(); assertTrue(handler instanceof ToHTMLContentHandler); p.parse(null, handler, null, null); String extracted = handler.toString(); assertContains("<head><title>This is the title", extracted); assertContains("aaaaaaaaaa", extracted); assertTrue(extracted.length() > 110000); p = new MockParser(10); handler = new BasicContentHandlerFactory(type, 5).getNewContentHandler(); assertTrue(handler instanceof WriteOutContentHandler); assertWriteLimitReached(p, (WriteOutContentHandler) handler); extracted = handler.toString(); assertContains("This ", extracted); assertNotContains("aaaa", extracted); p = new MockParser(OVER_DEFAULT); ByteArrayOutputStream os = new ByteArrayOutputStream(); handler = new BasicContentHandlerFactory(type, -1).getNewContentHandler(os, ENCODING); assertTrue(handler instanceof ToHTMLContentHandler); p.parse(null, handler, null, null); assertContains("This is the title", os.toByteArray()); assertContains("aaaaaaaaaa", os.toByteArray()); assertContains("<body", os.toByteArray()); assertContains("<html", os.toByteArray()); assertTrue(os.toByteArray().length > 110000); p = new MockParser(10); os = new ByteArrayOutputStream(); handler = new BasicContentHandlerFactory(type, 5).getNewContentHandler(os, ENCODING); assertTrue(handler instanceof WriteOutContentHandler); assertWriteLimitReached(p, (WriteOutContentHandler) handler); assertEquals(0, os.toByteArray().length); }
@Override public ContentHandler getNewContentHandler() { if (type == HANDLER_TYPE.BODY) { return new BodyContentHandler(writeLimit); } else if (type == HANDLER_TYPE.IGNORE) { return new DefaultHandler(); } if (writeLimit > -1) { switch(type) { case TEXT: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); case HTML: return new WriteOutContentHandler(new ToHTMLContentHandler(), writeLimit); case XML: return new WriteOutContentHandler(new ToXMLContentHandler(), writeLimit); default: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); } } else { switch (type) { case TEXT: return new ToTextContentHandler(); case HTML: return new ToHTMLContentHandler(); case XML: return new ToXMLContentHandler(); default: return new ToTextContentHandler(); } } }
BasicContentHandlerFactory implements ContentHandlerFactory { @Override public ContentHandler getNewContentHandler() { if (type == HANDLER_TYPE.BODY) { return new BodyContentHandler(writeLimit); } else if (type == HANDLER_TYPE.IGNORE) { return new DefaultHandler(); } if (writeLimit > -1) { switch(type) { case TEXT: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); case HTML: return new WriteOutContentHandler(new ToHTMLContentHandler(), writeLimit); case XML: return new WriteOutContentHandler(new ToXMLContentHandler(), writeLimit); default: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); } } else { switch (type) { case TEXT: return new ToTextContentHandler(); case HTML: return new ToHTMLContentHandler(); case XML: return new ToXMLContentHandler(); default: return new ToTextContentHandler(); } } } }
BasicContentHandlerFactory implements ContentHandlerFactory { @Override public ContentHandler getNewContentHandler() { if (type == HANDLER_TYPE.BODY) { return new BodyContentHandler(writeLimit); } else if (type == HANDLER_TYPE.IGNORE) { return new DefaultHandler(); } if (writeLimit > -1) { switch(type) { case TEXT: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); case HTML: return new WriteOutContentHandler(new ToHTMLContentHandler(), writeLimit); case XML: return new WriteOutContentHandler(new ToXMLContentHandler(), writeLimit); default: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); } } else { switch (type) { case TEXT: return new ToTextContentHandler(); case HTML: return new ToHTMLContentHandler(); case XML: return new ToXMLContentHandler(); default: return new ToTextContentHandler(); } } } BasicContentHandlerFactory(HANDLER_TYPE type, int writeLimit); }
BasicContentHandlerFactory implements ContentHandlerFactory { @Override public ContentHandler getNewContentHandler() { if (type == HANDLER_TYPE.BODY) { return new BodyContentHandler(writeLimit); } else if (type == HANDLER_TYPE.IGNORE) { return new DefaultHandler(); } if (writeLimit > -1) { switch(type) { case TEXT: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); case HTML: return new WriteOutContentHandler(new ToHTMLContentHandler(), writeLimit); case XML: return new WriteOutContentHandler(new ToXMLContentHandler(), writeLimit); default: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); } } else { switch (type) { case TEXT: return new ToTextContentHandler(); case HTML: return new ToHTMLContentHandler(); case XML: return new ToXMLContentHandler(); default: return new ToTextContentHandler(); } } } BasicContentHandlerFactory(HANDLER_TYPE type, int writeLimit); static HANDLER_TYPE parseHandlerType(String handlerTypeName, HANDLER_TYPE defaultType); @Override ContentHandler getNewContentHandler(); @Override ContentHandler getNewContentHandler(OutputStream os, String encoding); @Override ContentHandler getNewContentHandler(OutputStream os, Charset charset); HANDLER_TYPE getType(); }
BasicContentHandlerFactory implements ContentHandlerFactory { @Override public ContentHandler getNewContentHandler() { if (type == HANDLER_TYPE.BODY) { return new BodyContentHandler(writeLimit); } else if (type == HANDLER_TYPE.IGNORE) { return new DefaultHandler(); } if (writeLimit > -1) { switch(type) { case TEXT: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); case HTML: return new WriteOutContentHandler(new ToHTMLContentHandler(), writeLimit); case XML: return new WriteOutContentHandler(new ToXMLContentHandler(), writeLimit); default: return new WriteOutContentHandler(new ToTextContentHandler(), writeLimit); } } else { switch (type) { case TEXT: return new ToTextContentHandler(); case HTML: return new ToHTMLContentHandler(); case XML: return new ToXMLContentHandler(); default: return new ToTextContentHandler(); } } } BasicContentHandlerFactory(HANDLER_TYPE type, int writeLimit); static HANDLER_TYPE parseHandlerType(String handlerTypeName, HANDLER_TYPE defaultType); @Override ContentHandler getNewContentHandler(); @Override ContentHandler getNewContentHandler(OutputStream os, String encoding); @Override ContentHandler getNewContentHandler(OutputStream os, Charset charset); HANDLER_TYPE getType();  }