-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebBrowserExample.java
122 lines (104 loc) · 3.42 KB
/
WebBrowserExample.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
import java.io.IOException;
import java.util.Scanner;
import java.net.URLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class HttpDownloader {
/** Downloads a website at the given url.
*
* This simply uses the well knows Scanner class, but
* we use a URLConnection as input to Scanner instead of a
* file or System.in.
* */
String download(String url) throws IOException {
URLConnection connection = new URL(url).openConnection();
String entirePage = "";
Scanner urlReader = new Scanner(connection.getInputStream());
while(urlReader.hasNextLine()) {
String line = urlReader.nextLine();
entirePage += line + "\n";
}
return entirePage;
}
/** Get a LinkedList of strings containing all urls on
* the website at the given url.
*
* You will probably have some problems understanding all this code
* unless you are familiar with regular expressions. Do not
* worry about understanding it, and just see how it is used.
* */
LinkedList<String> getUrls(String url) throws IOException {
String entirePage = download(url);
LinkedList<String> urlList = new LinkedList<String>();
Pattern p = Pattern.compile("http://([a-zA-Z0-9\\./-_]+)");
Matcher m = p.matcher(entirePage);
while (m.find() == true) {
urlList.add(m.group());
}
return urlList;
}
}
class UserInput {
String command;
String argument;
UserInput(String command, String argument) {
this.command = command;
this.argument = argument;
}
}
class CommandFeed {
Scanner terminal = new Scanner(System.in);
boolean hasMoreCommands() {
return terminal.hasNextLine();
}
UserInput nextCommand() {
String[] line = terminal.nextLine().split(" ");
String command = line[0];
String argument = line[1];
return new UserInput(command, argument);
}
}
class ReallySimpleTextBasedWebBrowser {
HttpDownloader browser = new HttpDownloader();
CommandFeed cli = new CommandFeed();
ReallySimpleTextBasedWebBrowser() throws IOException {
printInputPrefix();
while(cli.hasMoreCommands()) {
handleUserInput();
printInputPrefix();
}
}
void printInputPrefix() {
System.out.println();
System.out.print("~$ ");
}
void downloadAndDisplay(String url) throws IOException {
String website = browser.download(url);
System.out.println(website);
}
void downloadAndListUrls(String url) throws IOException {
LinkedList<String> urlList = browser.getUrls(url);
for(String foundUrl : urlList) {
System.out.println("- " + foundUrl);
}
}
void handleUserInput() throws IOException {
UserInput userInput = cli.nextCommand();
if(userInput.command.equals("download")) {
downloadAndDisplay(userInput.argument);
}
else if(userInput.command.equals("urls")) {
downloadAndListUrls(userInput.argument);
}
else {
System.err.println("Invalid argument. Usage: <download|urls> <url>");
}
}
}
public class WebBrowserExample {
public static void main(String[] args) throws IOException {
new ReallySimpleTextBasedWebBrowser();
}
}