2 * © 2009 David ‘Bombe’ Roden
4 package net.pterodactylus.arachne.core;
6 import java.net.MalformedURLException;
9 import de.ina.util.validation.Validation;
12 * Container for a page. A page consists of an {@link Edition} and a path within
15 * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
19 /** The edition of the page. */
20 private final Edition edition;
22 /** The path of the page. */
23 private final String path;
29 * The edition of the page
31 * The path of the page
33 public Page(Edition edition, String path) {
34 this.edition = edition;
39 * Returns the edition of the page.
41 * @return The page’s edition
43 public Edition getEdition() {
48 * Returns the path of the page within the site.
50 * @return The page’s path
52 public String getPath() {
57 * Creates a URL from the given page.
62 * The port number of the URL
63 * @return The created URL, or <code>null</code> if the URL could not be
66 public URL toURL(String host, int port) {
68 return new URL("http://" + host + ":" + port + "/SSK@" + getEdition().getSite().getKey() + "/" + getEdition().getSite().getBasename() + "-" + getEdition().getNumber() + "/" + getPath());
69 } catch (MalformedURLException mue1) {
70 /* nearly impossible. */
80 * Creates a page from the given URL.
83 * The URL to create a page from
84 * @return The created page, or <code>null</code> if the page could not be
87 public static Page fromURL(URL url) {
88 String path = url.getPath();
89 if (path.length() == 0) {
92 String[] pathComponents = path.split("/");
93 if (pathComponents.length < 2) {
94 throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page.");
96 String siteName = pathComponents[1];
97 String[] siteComponents = siteName.split("@");
98 if (siteComponents.length != 2) {
99 throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
101 if (!"USK".equals(siteComponents[0]) && !"SSK".equals(siteComponents[0]) && !"CHK".equals(siteComponents[0])) {
102 throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
104 if ("USK".equals(siteComponents[0])) {
105 Site site = new Site(siteComponents[1], pathComponents[2]);
106 Edition edition = new Edition(site, Integer.parseInt(pathComponents[3]));
107 Page page = new Page(edition, createPath(pathComponents, 4));
110 if ("SSK".equals(siteComponents[0])) {
111 int lastDash = pathComponents[2].lastIndexOf('-');
112 String basename = pathComponents[2].substring(0, lastDash);
113 int editionNumber = Integer.parseInt(pathComponents[2].substring(lastDash + 1));
114 Site site = new Site(siteComponents[1], basename);
115 Edition edition = new Edition(site, editionNumber);
116 Page page = new Page(edition, createPath(pathComponents, 3));
119 /* TODO: handle CHK */
128 * Creates a path from the given String array, starting at the given index.
129 * The path is created by joining all Strings from the array, separating
130 * them with a slash (‘/’).
132 * @param pathComponents
133 * The array of path components
135 * The index of the first path components
136 * @return The joined path
138 private static String createPath(String[] pathComponents, int index) {
139 Validation.begin().isNotNull("pathComponents", pathComponents).check().isLessOrEqual("index", index, pathComponents.length).check();
140 StringBuilder path = new StringBuilder();
141 for (int pathComponentIndex = index; pathComponentIndex < pathComponents.length; pathComponentIndex++) {
142 if (path.length() > 0) {
145 path.append(pathComponents[pathComponentIndex]);
147 return path.toString();
157 * @see java.lang.Object#toString()
160 public String toString() {
161 return getClass().getName() + "[edition=" + edition + ",path=" + path + "]";