X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Farachne%2Fcore%2FPage.java;h=4d14b755b8ee0970a945be7491fc146caa153e6f;hb=8b58c6a6203d8fd6989d92bb5d9fedb9cce37d46;hp=8c4ae441cd813d2ac0ba6983986dada7a7736547;hpb=2afeec11e70577d9b92b1e73bdc23869c46a85ce;p=arachne.git diff --git a/src/net/pterodactylus/arachne/core/Page.java b/src/net/pterodactylus/arachne/core/Page.java index 8c4ae44..4d14b75 100644 --- a/src/net/pterodactylus/arachne/core/Page.java +++ b/src/net/pterodactylus/arachne/core/Page.java @@ -3,16 +3,21 @@ */ 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 a {@link Site} and a path within the - * site. + * Container for a page. A page consists of an {@link Edition} and a path within + * the edition. * * @author David ‘Bombe’ Roden */ public class Page { - /** The site of the page. */ - private final Site site; + /** The edition of the page. */ + private final Edition edition; /** The path of the page. */ private final String path; @@ -20,23 +25,23 @@ public class Page { /** * Creates a new page. * - * @param site - * The site of the page + * @param edition + * The edition of the page * @param path * The path of the page */ - public Page(Site site, String path) { - this.site = site; + public Page(Edition edition, String path) { + this.edition = edition; this.path = path; } /** - * Returns the site of the page. + * Returns the edition of the page. * - * @return The page’s site + * @return The page’s edition */ - public Site getSite() { - return site; + public Edition getEdition() { + return edition; } /** @@ -48,4 +53,112 @@ 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 + // + + /** + * {@inheritDoc} + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return getClass().getName() + "[edition=" + edition + ",path=" + path + "]"; + } + }