-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPage.java
71 lines (62 loc) · 2.73 KB
/
Page.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
package javasearchengine.objects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public final class Page {
public String pageTitle = "None";
public String pageDescribe = "None";
public String pagePath = "None";
public Page(String pageTitle, String pageDescribe, String pagePath) {
this.pageTitle = pageTitle;
this.pageDescribe = pageDescribe;
this.pagePath = pagePath;
}
public Page clone() {
return new Page(pageTitle, pageDescribe, pagePath);
}
public void save(File files){
File file = new File(files, (int) (Math.random() * 100000000) + ".page");
while (file.exists()){
file = new File(files, (int) (Math.random() * 100000000) + ".page");
}
try {
try (FileOutputStream in = new FileOutputStream(file)) {
if (pageDescribe.length() > 0 && pagePath.length() > 0 && pageTitle.length() > 0
&& !pageDescribe.equalsIgnoreCase("NONE") && !pagePath.equalsIgnoreCase("NONE") && !pageTitle.equalsIgnoreCase("NONE")) {
in.write((pageDescribe + "\r\n").getBytes());
in.write((pagePath + "\r\n").getBytes());
in.write((pageTitle + "\r\n").getBytes());
} else {
file.delete();
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public Page(String ip) {
try {
this.pagePath = ip;
this.pageTitle = Jsoup.connect(ip).get().getElementsByTag("title").text();
this.pageDescribe = "";
Document document = Jsoup.connect(ip).get();
for (Element element : document.children()) {
this.pageDescribe += element.text();
}
} catch (MalformedURLException | IllegalArgumentException ex) {
} catch (IOException ex) {
}
}
@Override
public String toString() {
String send = "<div><a href=\"%PATH%\" target=\"_blank\"><font size=\"4\" color=\"blue\"><u>%TITLE%</u></font></a><br><font size=\"3\" color=\"gray\">%DESCRIBE%</font><br><a target=\"_blank\" href=\"%PATH%\"><font size=\"3\" color=\"green\">%PATH%</font></a></div><br>\r\n";
return send.replace("%TITLE%", pageTitle)
.replace("%DESCRIBE%", pageDescribe.substring(0, (pageDescribe.length() < 180 ? pageDescribe.length() : 180)))
.replace("%PATH%", pagePath);
}
}