-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (58 loc) · 2.44 KB
/
Main.java
File metadata and controls
81 lines (58 loc) · 2.44 KB
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
package com.company;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private static final Pattern urlPattern = Pattern.compile("(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)" + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*" + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
boolean rootValid = false;
String rootURL = null;
do {
try {
openPage(rootURL = sc.nextLine());
rootValid = true;
} catch (IOException e) {
System.out.println("Invalid URL " + e.getMessage());
}
} while (!rootValid);
HashSet<String> hs = new HashSet<String>();
Site currentSite;
Site rootSite = new Site(rootURL, hs);
currentSite = rootSite;
hs.add(rootSite.getUrl());
long compTest = 0;
while (true) {
while (currentSite.getParent().hasNext()) {
while (currentSite.hasNext()) {
Site currentChild = currentSite.nextSite();
if (currentChild.isValid()) {
hs.add(currentChild.getUrl());
currentSite = currentChild;
for (int i = currentChild.getDepth()%20; i>0; i--) {
System.out.print('.');
}
System.out.println(currentChild.getUrl());
}
}
currentSite = currentSite.getParent();
for (int i = currentSite.getDepth()%20; i>0; i--) {
System.out.print('.');
}
System.out.println(currentSite.getUrl());
}
currentSite = currentSite.getParent();
for (int i = currentSite.getDepth()%20; i>0; i--) {
System.out.print('.');
}
System.out.println(currentSite.getUrl());
}
}
private static InputStream openPage(String url) throws IOException {
return new URL(url).openStream();
}
}