Use HTMLEditorKit parser in URL fetcher.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 9 Mar 2009 20:05:47 +0000 (21:05 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 9 Mar 2009 20:05:47 +0000 (21:05 +0100)
src/net/pterodactylus/arachne/core/URLFetcher.java

index 8f8465d..df8180f 100644 (file)
@@ -1,11 +1,14 @@
 package net.pterodactylus.arachne.core;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.arachne.parser.HtmlEditorKitParser;
+import net.pterodactylus.arachne.parser.ParserListener;
 import de.ina.util.validation.Validation;
 
 /**
@@ -14,7 +17,7 @@ import de.ina.util.validation.Validation;
  *
  * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
  */
-class URLFetcher implements Runnable {
+class URLFetcher implements Runnable, ParserListener {
 
        /** The logger. */
        private static final Logger logger = Logger.getLogger(URLFetcher.class.getName());
@@ -51,9 +54,29 @@ class URLFetcher implements Runnable {
                        long contentLength = urlConnection.getContentLength();
                        String contentType = urlConnection.getContentType();
                        logger.log(Level.INFO, "Type is “" + contentType + "”, length is " + contentLength + ".");
+                       HtmlEditorKitParser htmlEditorKitParser = new HtmlEditorKitParser();
+                       htmlEditorKitParser.parse(this, urlConnection.getInputStream(), "UTF-8");
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, "Could not fetch “" + url + "”.", ioe1);
                }
        }
 
+       //
+       // INTERFACE ParserListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void parsedLink(InputStream inputStream, String linkTarget, String linkTitle, String linkText) {
+               System.out.println("Found link to “" + linkTarget + "” named “" + linkText + "” or “" + linkTitle + "”.");
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void parsedTitle(InputStream inputStream, String title) {
+               System.out.println("Found title “" + title + "”.");
+       }
+
 }