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();  }
@Test public void testIgnore() throws Exception { Parser p = new MockParser(OVER_DEFAULT); ContentHandler handler = new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.IGNORE, -1).getNewContentHandler(); assertTrue(handler instanceof DefaultHandler); p.parse(null, handler, null, null); assertContains("org.xml.sax.helpers.DefaultHandler", handler.toString()); p = new MockParser(100); handler = new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.IGNORE, 5).getNewContentHandler(); assertTrue(handler instanceof DefaultHandler); p.parse(null, handler, null, null); assertContains("org.xml.sax.helpers.DefaultHandler", handler.toString()); }
@Test public void testText() throws Exception { Parser p = new MockParser(OVER_DEFAULT); BasicContentHandlerFactory.HANDLER_TYPE type = BasicContentHandlerFactory.HANDLER_TYPE.TEXT; ContentHandler handler = new BasicContentHandlerFactory(type, -1).getNewContentHandler(); assertTrue(handler instanceof ToTextContentHandler); p.parse(null, handler, null, null); String extracted = handler.toString(); assertContains("This is the title", extracted); assertContains("aaaaaaaaaa", extracted); assertNotContains("<body", extracted); assertNotContains("<html", 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 ToTextContentHandler); p.parse(null, handler, null, null); assertContains("This is the title", os.toByteArray()); assertContains("aaaaaaaaaa", os.toByteArray()); assertTrue(os.toByteArray().length > 110000); assertNotContains("<body", os.toByteArray()); assertNotContains("<html", os.toByteArray()); 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); }
@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); }
@Test public void testXML() 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 ToXMLContentHandler); p.parse(null, handler, new Metadata(), null); String extracted = handler.toString(); assertContains("<head><title>This is the title", extracted); assertContains("aaaaaaaaaa", extracted); assertTrue(handler.toString().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 ToXMLContentHandler); 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); }
@Test public void testBody() throws Exception { Parser p = new MockParser(OVER_DEFAULT); BasicContentHandlerFactory.HANDLER_TYPE type = BasicContentHandlerFactory.HANDLER_TYPE.BODY; ContentHandler handler = new BasicContentHandlerFactory(type, -1).getNewContentHandler(); assertTrue(handler instanceof BodyContentHandler); p.parse(null, handler, null, null); String extracted = handler.toString(); assertNotContains("title", extracted); assertContains("aaaaaaaaaa", extracted); assertTrue(extracted.length() > 110000); p = new MockParser(10); handler = new BasicContentHandlerFactory(type, 5).getNewContentHandler(); assertTrue(handler instanceof BodyContentHandler); assertWriteLimitReached(p, (BodyContentHandler)handler); extracted = handler.toString(); assertNotContains("This ", extracted); assertContains("aaaa", extracted); p = new MockParser(OVER_DEFAULT); ByteArrayOutputStream os = new ByteArrayOutputStream(); handler = new BasicContentHandlerFactory(type, -1).getNewContentHandler(os, ENCODING); assertTrue(handler instanceof BodyContentHandler); p.parse(null, handler, null, null); assertNotContains("title", os.toByteArray()); assertContains("aaaaaaaaaa", os.toByteArray()); assertNotContains("<body", os.toByteArray()); assertNotContains("<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); }