-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.java
More file actions
executable file
·75 lines (57 loc) · 1.92 KB
/
Util.java
File metadata and controls
executable file
·75 lines (57 loc) · 1.92 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
package io;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class Util {
public static String extractURL(String tag) {
String host = null;
tag.trim();
if (tag.startsWith("<a") || tag.startsWith("<A")) {
for (int i = 0; i < tag.length();) {
String sub = tag.substring(i);
if (sub.startsWith("http://") || sub.startsWith("HTTP://")) {
int j = 7;
for (; j < sub.length() && sub.charAt(j) != '/'
&& sub.charAt(j) != '"'; j++)
;
host = sub.substring(0, j);
i += 7;
} else
i++;
}
}
return host;
}
public static ArrayList<String> extractTags(URL u) throws IOException {
ArrayList<String> tags = new ArrayList<String>();
Reader r = new BufferedReader(new InputStreamReader(u.openStream()));
StringBuffer tag = new StringBuffer();
int c = 0;
boolean inTag = false;
while ((c = r.read()) != -1) {
if (c == '<')
inTag = true;
if (inTag)
tag.append((char) c);
if (c == '>') {
tags.add(tag.toString());
tag = new StringBuffer();
inTag = false;
}
}
return tags;
}
public static void main(String args[]) {
try {
URL u = new URL("http://www.gold.ac.uk");
ArrayList<String> tags = extractTags(u);
for (int i = 0, n = tags.size(); i < n; ++i) {
String host = extractURL(tags.get(i));
if (host != null)
System.out.println(host);
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}