2651de0d70075d0b724677a4d807825c114abda4
[arachne.git] / src / net / pterodactylus / arachne / core / Page.java
1 /*
2  * © 2009 David ‘Bombe’ Roden
3  */
4 package net.pterodactylus.arachne.core;
5
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import de.ina.util.validation.Validation;
10
11 /**
12  * Container for a page. A page consists of an {@link Edition} and a path within
13  * the edition.
14  *
15  * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
16  */
17 public class Page {
18
19         /** The edition of the page. */
20         private final Edition edition;
21
22         /** The path of the page. */
23         private final String path;
24
25         /**
26          * Creates a new page.
27          *
28          * @param edition
29          *            The edition of the page
30          * @param path
31          *            The path of the page
32          */
33         public Page(Edition edition, String path) {
34                 this.edition = edition;
35                 this.path = path;
36         }
37
38         /**
39          * Returns the edition of the page.
40          *
41          * @return The page’s edition
42          */
43         public Edition getEdition() {
44                 return edition;
45         }
46
47         /**
48          * Returns the path of the page within the site.
49          *
50          * @return The page’s path
51          */
52         public String getPath() {
53                 return path;
54         }
55
56         /**
57          * Creates a URL from the given page.
58          *
59          * @param host
60          *            The host of the URL
61          * @param port
62          *            The port number of the URL
63          * @return The created URL, or <code>null</code> if the URL could not be
64          *         created
65          */
66         public URL toURL(String host, int port) {
67                 try {
68                         return new URL("http://" + host + ":" + port + "/SSK@" + getEdition().getSite().getKey() + "/" + getEdition().getSite().getBasename() + "-" + getEdition().getNumber() + "/" + getPath());
69                 } catch (MalformedURLException mue1) {
70                         /* nearly impossible. */
71                 }
72                 return null;
73         }
74
75         //
76         // STATIC METHODS
77         //
78
79         /**
80          * Creates a page from the given URL.
81          *
82          * @param url
83          *            The URL to create a page from
84          * @return The created page, or <code>null</code> if the page could not be
85          *         created
86          */
87         public static Page fromURL(URL url) {
88                 String path = url.getPath();
89                 if (path.length() == 0) {
90                         path = "/";
91                 }
92                 String[] pathComponents = path.split("/");
93                 if (pathComponents.length < 2) {
94                         throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page.");
95                 }
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.");
100                 }
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.");
103                 }
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));
108                         return page;
109                 }
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));
117                         return page;
118                 }
119                 /* TODO: handle CHK */
120                 return null;
121         }
122
123         //
124         // PRIVATE METHODS
125         //
126
127         /**
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 (‘/’).
131          *
132          * @param pathComponents
133          *            The array of path components
134          * @param index
135          *            The index of the first path components
136          * @return The joined path
137          */
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) {
143                                 path.append('/');
144                         }
145                         path.append(pathComponents[pathComponentIndex]);
146                 }
147                 return path.toString();
148         }
149
150         //
151         // OBJECT METHODS
152         //
153
154         /**
155          * {@inheritdoc}
156          *
157          * @see java.lang.Object#toString()
158          */
159         @Override
160         public String toString() {
161                 return getClass().getName() + "[edition=" + edition + ",path=" + path + "]";
162         }
163
164 }