Add new links to core.
[arachne.git] / src / net / pterodactylus / arachne / core / URLFetcher.java
index 8f8465d..0b4cb47 100644 (file)
@@ -1,11 +1,15 @@
 package net.pterodactylus.arachne.core;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
 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 +18,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 +55,36 @@ 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) {
+               URL newLink = null;
+               try {
+                       newLink = new URL(url, linkTarget);
+                       core.addPage(newLink);
+               } catch (MalformedURLException mue1) {
+                       logger.log(Level.WARNING, "Could not create URL from “" + url + "” and “" + linkTarget + "”.", mue1);
+               } catch (IllegalArgumentException iae1) {
+                       logger.log(Level.WARNING, "Could not add “" + newLink + "” to core queue.", iae1);
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void parsedTitle(InputStream inputStream, String title) {
+       }
+
 }