This repository has been 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
/
HyperLinkExtractor.java
164 lines (135 loc) · 4.28 KB
/
HyperLinkExtractor.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
/*
* HyperLinkExtractor.java
* Gary Read 662193
* Swansea University
*/
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
public class HyperLinkExtractor {
private static ArrayList<String> HyperLinks;
private static PrintWriter oLog;
//Main method, Initilizes varibles from console
public static void main(String[] args) {
String url = null;
boolean partial = false;
intPrintWriter();
//Checks for correct command line arguments
if (args.length < 1) {
usage();
return;
} else {
if (args[0].equalsIgnoreCase("-cp")) {
url = args[1];
partial = true;
getHyperLinks(url, partial);
}
else if (args[0].charAt(0) != '-') {
url = args[0];
partial = false;
getHyperLinks(url, partial);
} else {
usage();
return;
}
}
}
private static void getHyperLinks(String url, boolean partial) {
//Create new HyperLink ArrayList<String>
HyperLinks = new ArrayList<String>();
//Create Initial Node
Node newNode = new Node(url, partial);
System.out.println("BRANCHING:\t" + newNode.getNodeURL());
HyperLinks.add(newNode.getNodeURL());
for (String link : newNode.getNodeLeaves()) {
Node node = new Node(link, partial);
System.out.println();
String tab = "\t\t\t\t";
if (node.getNodeLeaves() != null) {
System.out.println("\tBRANCHING:\t" + node.getNodeURL());
HyperLinks.add(node.getNodeURL());
for (String address : node.getNodeLeaves()) {
System.out.println(tab + address);
HyperLinks.add(address);
}
} else {
System.out.println("Malformed URL.");
}
}
writeToFile();
}
//Method to write HyperLinks<String> to file called 'HyperLinks.txt'
private static void writeToFile() {
PrintWriter out = null;
String fileName = "HyperLinks.txt";
try {
out = new PrintWriter(fileName);
//Get and Format current time and date
Date d = new Date();
String time = new SimpleDateFormat("HH:mm:ss").format(d);
String date = new SimpleDateFormat("dd/MM/yyyy").format(d);
out.println("#=====================================================================");
out.println("#Generated by HyperLinkExtractor");
out.println("#");
out.println("#Created " + date + " @ " + time);
out.println("#=====================================================================");
out.println("#");
for (String link : HyperLinks) {
out.println(link);
}
}
catch (IOException e) {
System.out.println("Writing Failed");
}
finally {
if (out != null) {
out.close();
}
}
System.out.println();
System.out.println("Completed Writing - " + fileName);
}
//Initalize PrintWriter for logging
private static void intPrintWriter() {
oLog = null;
//Creating PrintWriter for logging
try {
//Over-write any previous logs
oLog = new PrintWriter("log.txt");
oLog.print("");
//Create FileWriter captured in a PrintWriter for append and automatic flushing
oLog = new PrintWriter(new FileWriter("log.txt", true), true);
//Get and Format current time and date
Date d = new Date();
String time = new SimpleDateFormat("HH:mm:ss").format(d);
String date = new SimpleDateFormat("dd/MM/yyyy").format(d);
oLog.println("=====================================================================");
oLog.println("Generated by HyperLinkExtractor");
oLog.println();
oLog.println("Created " + date + " @ " + time);
oLog.println("=====================================================================");
}
catch (IOException e) {
System.out.println();
System.out.println("IO Exception");
System.out.println("\t" + "Logs will not be captured.");
System.out.println();
}
}
//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();
}
}