First implementation of the Arachne core.
[arachne.git] / src / net / pterodactylus / arachne / core / Core.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 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.List;
11
12 import de.ina.util.service.AbstractService;
13 import de.ina.util.validation.Validation;
14
15 /**
16  * Arachne’s core.
17  *
18  * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
19  */
20 public class Core extends AbstractService {
21
22         //
23         // PROPERTIES
24         //
25
26         /** The host of the freenet node. */
27         private String nodeHost = "localhost";
28
29         /** The port of the freenet node. */
30         private int nodePort = 8888;
31
32         //
33         // INTERNAL MEMBERS
34         //
35
36         /** The current list of URLs to crawl. */
37         private final List<Page> pages = Collections.synchronizedList(new ArrayList<Page>());
38
39         //
40         // ACTIONS
41         //
42
43         /**
44          * Adds the given URL to the list of pages to crawl.
45          *
46          * @param url
47          *            The URL to add
48          */
49         public void addPage(URL url) {
50                 Validation.begin().isNotNull("url", url).check().isEqual("url.getHost()", url.getHost(), (Object) nodeHost).isEqual("url.getPort()", url.getPort(), nodePort).check();
51                 String path = url.getPath();
52                 if (path.length() == 0) {
53                         path = "/";
54                 }
55                 String[] pathComponents = path.split("/");
56                 if (pathComponents.length < 2) {
57                         throw new IllegalArgumentException("URL “" + url + "” is not a valid freenet page.");
58                 }
59                 String siteName = pathComponents[1];
60                 String[] siteComponents = siteName.split("@");
61                 if (siteComponents.length != 2) {
62                         throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
63                 }
64                 if (!"USK".equals(siteComponents[0]) && !"SSK".equals(siteComponents[0]) && !"CHK".equals(siteComponents[0])) {
65                         throw new IllegalArgumentException("siteName “" + siteName + "” is not a valid freenet page.");
66                 }
67                 if ("USK".equals(siteComponents[0])) {
68                         Site site = new Site(siteComponents[1], pathComponents[2]);
69                         Edition edition = new Edition(site, Integer.parseInt(pathComponents[3]));
70                         Page page = new Page(edition, createPath(pathComponents, 4));
71                         addPage(page);
72                 }
73                 if ("SSK".equals(siteComponents[0])) {
74                         int lastDash = pathComponents[2].lastIndexOf('-');
75                         String basename = pathComponents[2].substring(0, lastDash);
76                         int editionNumber = Integer.parseInt(pathComponents[2].substring(lastDash + 1));
77                         Site site = new Site(siteComponents[1], basename);
78                         Edition edition = new Edition(site, editionNumber);
79                         Page page = new Page(edition, createPath(pathComponents, 3));
80                         addPage(page);
81                 }
82                 /* TODO: handle CHK */
83         }
84
85         /**
86          * Adds the given URL to the list of pages to crawl.
87          *
88          * @param url
89          *            The URL of the page to crawl
90          * @throws MalformedURLException
91          *             if the URL is not a valid URL
92          */
93         public void addPage(String url) throws MalformedURLException {
94                 Validation.begin().isNotNull("url", (Object) url).check();
95                 addPage(new URL(url));
96         }
97
98         /**
99          * Adds the given page to the list of pages to crawl.
100          *
101          * @param page
102          *            The page to add
103          */
104         public void addPage(Page page) {
105                 Validation.begin().isNotNull("page", page).check();
106                 pages.add(page);
107                 notifySyncObject();
108         }
109
110         //
111         // PRIVATE METHODS
112         //
113
114         /**
115          * Creates a path from the given String array, starting at the given index.
116          * The path is created by joining all Strings from the array, separating
117          * them with a slash (‘/’).
118          *
119          * @param pathComponents
120          *            The array of path components
121          * @param index
122          *            The index of the first path components
123          * @return The joined path
124          */
125         private String createPath(String[] pathComponents, int index) {
126                 Validation.begin().isNotNull("pathComponents", pathComponents).check().isLess("index", pathComponents.length, index).check();
127                 StringBuilder path = new StringBuilder();
128                 for (int pathComponentIndex = index; pathComponentIndex < pathComponents.length; pathComponentIndex++) {
129                         if (path.length() > 0) {
130                                 path.append('/');
131                         }
132                         path.append(pathComponents[pathComponentIndex]);
133                 }
134                 return path.toString();
135         }
136
137 }