-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrl.java
More file actions
187 lines (170 loc) · 3.44 KB
/
Copy pathUrl.java
File metadata and controls
187 lines (170 loc) · 3.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package WebCrawler;
/**
* The Class Url.
*/
public class Url
{
/** The address of URL */
private String address = null;
/** The host of URL. */
private String host = null;
/** The port of URL. */
private int port = 80;
/** The path of URL. */
private String path = null;
/** The query of URL. */
private String query = null;
/**
* Instantiates a new url.
* Create a new URL according to website address
* @param websiteAddress the website address
*/
public Url(String websiteAddress)
{
address = parse(websiteAddress);
host = getHostByAddress(address);
path = getPathByAddress(address);
query = getStringQueryByAddress(address);
}
/**
* Parses the website address
* Three rules of an website address:
* 1: Must start with "http://"
* 2: Must not consist "www."
* 3: Must end with "/"
* @param websiteAddress the website address
* @return the string
*/
private String parse(String websiteAddress)
{
websiteAddress = websiteAddress.replace("www.", "");
if (websiteAddress.indexOf("http://") == -1)
{
websiteAddress = "http://" + websiteAddress;
}
String lastChar = websiteAddress.substring(websiteAddress.length()-1);
if (!lastChar.equals("/"))
{
websiteAddress = websiteAddress + "/";
}
return websiteAddress;
}
/**
* Gets the path by address.
* @param websiteAddress the website address
* @return the path
*/
private String getPathByAddress(String websiteAddress)
{
websiteAddress = websiteAddress.replace("http://", "");
websiteAddress = websiteAddress.replace("www.", "");
int index1 = websiteAddress.indexOf("/");
if (index1 != -1)
{
int index2 = websiteAddress.indexOf("?");
if (index2 == -1)
{
websiteAddress = websiteAddress.substring(index1);
}
else
{
websiteAddress = websiteAddress.substring(index1,index2);
}
return websiteAddress;
}
else
{
return "/";
}
}
/**
* Gets the string query by address.
* @param websiteAddress the website address
* @return the string query
*/
private String getStringQueryByAddress(String websiteAddress)
{
int index = websiteAddress.indexOf("?");
if (index != -1)
{
websiteAddress = websiteAddress.substring(index+1);
return websiteAddress;
}
else
{
return "";
}
}
/**
* Gets the host by address.
*
* @param websiteAddress the website address
* @return the host name
*/
private String getHostByAddress(String websiteAddress)
{
websiteAddress = websiteAddress.replace("http://", "");
websiteAddress = websiteAddress.replace("www.", "");
int index1 = websiteAddress.indexOf("/");
if (index1 != -1)
{
websiteAddress = websiteAddress.substring(0, index1);
}
return websiteAddress;
}
/**
* Gets the host.
* Public function
* @return the host
*/
public String getHost()
{
return host;
}
/**
* Gets the port.
* Public function
* @return the port
*/
public int getPort()
{
return port;
}
/**
* Gets the path.
* Public function
* @return the path
*/
public String getPath()
{
return path;
}
/**
* Gets the query.
* Public function
* @return the query
*/
public String getQuery()
{
return query;
}
/**
* Gets the origin.
* Public function
* @return the origin
*/
public String getOrigin()
{
// TODO Auto-generated method stub
return "http://"+host;
}
/**
* Gets the address.
* Public function
* @return the address
*/
public String getAddress()
{
return address;
}
}