This repository was archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.java
213 lines (178 loc) · 5.32 KB
/
Node.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Node.java
* Gary Read 662193
* Swansea University
*/
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Scanner;
import java.util.ArrayList;
public class Node {
private String urlOfNode;
private boolean partial;
private ArrayList<String> nodeLeaves;
private PrintWriter oLog;
public static void main(String[] args) {
//Error case handleing
usage();
return;
}
//Console Usage Message
public static void usage() {
System.out.println();
System.out.println("HyperLinkExtractor");
System.out.println("\tUSAGE\t"+ "java HyperLinkExtractor [-OPTION] [URL_PATH]");
System.out.println("");
System.out.println("\tOPTIONS");
System.out.println("\t\t"+ "-cp" + "\tCapture Partial URLs");
System.out.println("\t\t"+ "" + "\tLeave blank to Omit Partial URLs");
System.out.println();
System.out.println("\tURL_PATH\t" + "http://example.com ");
System.out.println();
}
//Constructor
public Node(String urlOfNode, boolean partial) {
this.urlOfNode = urlOfNode;
this.partial = partial;
//Set up a PrintWriter for logging
setPrintWriter();
//Logging
if (oLog != null) {
oLog.println();
oLog.println("NODE CREATED @\t" + getNodeURL());
oLog.println("PARTIAL URLS CAPTURED? = " + partial);
oLog.println();
}
findNodeLeaves(urlOfNode);
}
//Return Node URL [String]
public String getNodeURL() {
return urlOfNode;
}
//Return Nodes Links [ArrayList<String>]
public ArrayList<String> getNodeLeaves() {
return nodeLeaves;
}
//Set capturing of partial URLS
public void capturePartialUrl(boolean partial) {
if (partial == true) {
this.partial = true;
} else {
this.partial = false;
}
}
//Method to crawl Node URL for its links
private void findNodeLeaves(String url) {
nodeLeaves = null;
Scanner in = getURLScanner(url);
if (in == null) {
nodeLeaves = null;
return;
} else {
//Logging
if (oLog != null) { oLog.println("\tRETRIEVING HYPERLINKS @\t" + getNodeURL()); }
nodeLeaves = new ArrayList<String>();
in.useDelimiter("<a href=\"");
if (in.hasNext()) { in.next(); }
}
while (in.hasNext()) {
Scanner end = new Scanner(" " + in.next());
end.useDelimiter("\""); //i.e www.csvision.com/index.php{"}<\a>
String link = null;
if (end.hasNext()) {
link = end.next();
if (link != null) {
//Remove leading and trailing whitespace
link = link.trim();
if (link.length() > 3) {
//Add all String begining with http://
if (link.substring(0,4).equalsIgnoreCase("http")) {
//Add link
nodeLeaves.add(link);
//Logging
if (oLog != null) { oLog.println("\t\t" + link); }
}
else if (link.substring(0,4).equalsIgnoreCase("www.")) {
//Add all String begining with www.
nodeLeaves.add(link);
//Logging
if (oLog != null) { oLog.println("\t\t" + link); }
}
else if (partial && link.length() > 0) {
/* Add all other urls begining with '/' appending Node URL to begining
* /uk/superman.html
* www.csvision.com/uk/superman.html */
if (link.charAt(0) == '/') {
//Corrects for appending back slashes "../part_1//part_2.html"
if (url.charAt(url.length() - 1) == '/') {
String newLink = url + link.substring(1,link.length());
//Add link
nodeLeaves.add(newLink);
//Logging
if (oLog != null) { oLog.println("\t\t" + newLink); }
} else {
String newLink = url + link;
//Add link
nodeLeaves.add(newLink);
//Logging
if (oLog != null) { oLog.println("\t\t" + newLink); }
}
}
}
}
}
}
end.close();
}
in.close();
//Logging
if (oLog != null) {
oLog.println();
oLog.println("COMPLETED CRAWL");
oLog.println("URLS CAPTURED = " + getNodeLeaves().size());
oLog.println();
oLog.println("=====================================================================");
oLog.close();
}
}
//Initalize PrintWriter for logging
private void setPrintWriter() {
oLog = null;
//Creating PrintWriter for logging
try {
//Create FileWriter captured in a PrintWriter for append and automatic flushing
oLog = new PrintWriter(new FileWriter("log.txt", true), true);
}
catch (IOException e) {
System.out.println();
System.out.println("IO Exception");
System.out.println("\t" + "Logs will not be captured.");
System.out.println();
}
}
//Return scanner, pre-initilised with URL stream [Scanner(URL.openStream())]
private Scanner getURLScanner(String url) {
Scanner in = null;
try {
URL locator = new URL(url);
in = new Scanner(locator.openStream());
}
catch (MalformedURLException e) {
if (oLog != null) { oLog.println("ERROR: Node.getURLScanner()\t" + "MALFORMED URL @\t" + getNodeURL()); }
return in;
}
catch (FileNotFoundException e) {
if (oLog != null) { oLog.println("ERROR: Node.getURLScanner()\t" + "INVALID URL @\t" + getNodeURL()); }
return in;
}
catch (IOException e) {
if (oLog != null) { oLog.println("ERROR: Node.getURLScanner()\t" + "IO EXCEPTION @\t" + getNodeURL()); }
return in;
}
return in;
}
}