From c85307a32e0957c15233b4d9ead5f58d8d167244 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 10 Mar 2009 00:24:58 +0100 Subject: [PATCH] Move methods to convert to and from URL to Page. --- src/net/pterodactylus/arachne/core/Core.java | 81 +---------------------- src/net/pterodactylus/arachne/core/Page.java | 99 ++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 78 deletions(-) diff --git a/src/net/pterodactylus/arachne/core/Core.java b/src/net/pterodactylus/arachne/core/Core.java index f6b050d..9e3f1a5 100644 --- a/src/net/pterodactylus/arachne/core/Core.java +++ b/src/net/pterodactylus/arachne/core/Core.java @@ -73,38 +73,6 @@ public class Core extends AbstractService { */ public void addPage(URL url) { Validation.begin().isNotNull("url", url).check().isEqual("url.getHost()", url.getHost(), (Object) nodeHost).isEqual("url.getPort()", url.getPort(), nodePort).check(); - String path = url.getPath(); - if (path.length() == 0) { - path = "/"; - } - String[] pathComponents = path.split("/"); - if (pathComponents.length < 2) { - throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page."); - } - String siteName = pathComponents[1]; - String[] siteComponents = siteName.split("@"); - if (siteComponents.length != 2) { - throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page."); - } - if (!"USK".equals(siteComponents[0]) && !"SSK".equals(siteComponents[0]) && !"CHK".equals(siteComponents[0])) { - throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page."); - } - if ("USK".equals(siteComponents[0])) { - Site site = new Site(siteComponents[1], pathComponents[2]); - Edition edition = new Edition(site, Integer.parseInt(pathComponents[3])); - Page page = new Page(edition, createPath(pathComponents, 4)); - addPage(page); - } - if ("SSK".equals(siteComponents[0])) { - int lastDash = pathComponents[2].lastIndexOf('-'); - String basename = pathComponents[2].substring(0, lastDash); - int editionNumber = Integer.parseInt(pathComponents[2].substring(lastDash + 1)); - Site site = new Site(siteComponents[1], basename); - Edition edition = new Edition(site, editionNumber); - Page page = new Page(edition, createPath(pathComponents, 3)); - addPage(page); - } - /* TODO: handle CHK */ } /** @@ -142,6 +110,7 @@ public class Core extends AbstractService { * @see de.ina.util.service.AbstractService#serviceRun() */ @Override + @SuppressWarnings("null") protected void serviceRun() { while (!shouldStop()) { Page nextPage = null; @@ -160,7 +129,7 @@ public class Core extends AbstractService { if (shouldStop()) { break; } - URL nextURL = createURL(nextPage); + URL nextURL = nextPage.toURL(nodeHost, nodePort); if (nextURL == null) { logger.log(Level.INFO, "Skipping “" + nextPage + "”."); continue; @@ -168,7 +137,7 @@ public class Core extends AbstractService { URLFetcher urlFetcher; try { logger.log(Level.INFO, "Fetching “" + nextURL + "”..."); - urlFetcher = new URLFetcher(this, nextURL); + urlFetcher = new URLFetcher(nextURL); urlFetcherExecutor.execute(urlFetcher); } catch (NoSuchAlgorithmException nsae1) { logger.log(Level.SEVERE, "Could not get “SHA-256” message digest!", nsae1); @@ -176,48 +145,4 @@ public class Core extends AbstractService { } } - // - // PRIVATE METHODS - // - - /** - * Creates a path from the given String array, starting at the given index. - * The path is created by joining all Strings from the array, separating - * them with a slash (‘/’). - * - * @param pathComponents - * The array of path components - * @param index - * The index of the first path components - * @return The joined path - */ - private String createPath(String[] pathComponents, int index) { - Validation.begin().isNotNull("pathComponents", pathComponents).check().isLessOrEqual("index", index, pathComponents.length).check(); - StringBuilder path = new StringBuilder(); - for (int pathComponentIndex = index; pathComponentIndex < pathComponents.length; pathComponentIndex++) { - if (path.length() > 0) { - path.append('/'); - } - path.append(pathComponents[pathComponentIndex]); - } - return path.toString(); - } - - /** - * Creates a URL from the given page. - * - * @param page - * The page to create a URL from - * @return The created URL, or null if the URL could not be - * created - */ - private URL createURL(Page page) { - try { - return new URL("http://" + nodeHost + ":" + nodePort + "/SSK@" + page.getEdition().getSite().getKey() + "/" + page.getEdition().getSite().getBasename() + "-" + page.getEdition().getNumber() + "/" + page.getPath()); - } catch (MalformedURLException mue1) { - /* nearly impossible. */ - } - return null; - } - } diff --git a/src/net/pterodactylus/arachne/core/Page.java b/src/net/pterodactylus/arachne/core/Page.java index 0a94be7..2651de0 100644 --- a/src/net/pterodactylus/arachne/core/Page.java +++ b/src/net/pterodactylus/arachne/core/Page.java @@ -3,6 +3,11 @@ */ package net.pterodactylus.arachne.core; +import java.net.MalformedURLException; +import java.net.URL; + +import de.ina.util.validation.Validation; + /** * Container for a page. A page consists of an {@link Edition} and a path within * the edition. @@ -48,6 +53,100 @@ public class Page { return path; } + /** + * Creates a URL from the given page. + * + * @param host + * The host of the URL + * @param port + * The port number of the URL + * @return The created URL, or null if the URL could not be + * created + */ + public URL toURL(String host, int port) { + try { + return new URL("http://" + host + ":" + port + "/SSK@" + getEdition().getSite().getKey() + "/" + getEdition().getSite().getBasename() + "-" + getEdition().getNumber() + "/" + getPath()); + } catch (MalformedURLException mue1) { + /* nearly impossible. */ + } + return null; + } + + // + // STATIC METHODS + // + + /** + * Creates a page from the given URL. + * + * @param url + * The URL to create a page from + * @return The created page, or null if the page could not be + * created + */ + public static Page fromURL(URL url) { + String path = url.getPath(); + if (path.length() == 0) { + path = "/"; + } + String[] pathComponents = path.split("/"); + if (pathComponents.length < 2) { + throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page."); + } + String siteName = pathComponents[1]; + String[] siteComponents = siteName.split("@"); + if (siteComponents.length != 2) { + throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page."); + } + if (!"USK".equals(siteComponents[0]) && !"SSK".equals(siteComponents[0]) && !"CHK".equals(siteComponents[0])) { + throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page."); + } + if ("USK".equals(siteComponents[0])) { + Site site = new Site(siteComponents[1], pathComponents[2]); + Edition edition = new Edition(site, Integer.parseInt(pathComponents[3])); + Page page = new Page(edition, createPath(pathComponents, 4)); + return page; + } + if ("SSK".equals(siteComponents[0])) { + int lastDash = pathComponents[2].lastIndexOf('-'); + String basename = pathComponents[2].substring(0, lastDash); + int editionNumber = Integer.parseInt(pathComponents[2].substring(lastDash + 1)); + Site site = new Site(siteComponents[1], basename); + Edition edition = new Edition(site, editionNumber); + Page page = new Page(edition, createPath(pathComponents, 3)); + return page; + } + /* TODO: handle CHK */ + return null; + } + + // + // PRIVATE METHODS + // + + /** + * Creates a path from the given String array, starting at the given index. + * The path is created by joining all Strings from the array, separating + * them with a slash (‘/’). + * + * @param pathComponents + * The array of path components + * @param index + * The index of the first path components + * @return The joined path + */ + private static String createPath(String[] pathComponents, int index) { + Validation.begin().isNotNull("pathComponents", pathComponents).check().isLessOrEqual("index", index, pathComponents.length).check(); + StringBuilder path = new StringBuilder(); + for (int pathComponentIndex = index; pathComponentIndex < pathComponents.length; pathComponentIndex++) { + if (path.length() > 0) { + path.append('/'); + } + path.append(pathComponents[pathComponentIndex]); + } + return path.toString(); + } + // // OBJECT METHODS // -- 2.7.4