-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedirects.java
More file actions
109 lines (85 loc) · 3.66 KB
/
Redirects.java
File metadata and controls
109 lines (85 loc) · 3.66 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
package javaxt.express.cms;
//******************************************************************************
//** Redirects Class
//******************************************************************************
/**
* Used to parse a plain text file with URL redirects.
*
******************************************************************************/
public class Redirects {
private java.util.HashMap<String, String> redirects = new java.util.HashMap<String, String>();
private java.util.List<String> sortedKeys = new java.util.ArrayList<String>();
private javaxt.io.File file;
private long lastUpdate;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class. */
public Redirects(javaxt.io.File file) {
this.file = file;
parseRedirects();
}
//**************************************************************************
//** parseRedirects
//**************************************************************************
/** Used to parse the "redirects.txt" file and updates the redirects hashmap.
* Also updates the lastUpdate timestamp used in the getRedirect() method.
*/
private void parseRedirects(){
redirects.clear();
sortedKeys.clear();
if (file.exists()){
lastUpdate = file.getDate().getTime();
for (String entry : file.getText().split("\r\n")){
entry = entry.trim();
if (entry.length()==0 || entry.startsWith("#") || entry.startsWith("//")){
//skip line
}
else{
while(entry.contains("\t\t")) entry = entry.replace("\t\t", "\t");
String[] arr = entry.split("\t");
if (arr.length>1){
redirects.put(arr[0], arr[1]);
}
}
}
sortedKeys = new java.util.ArrayList<String>(redirects.keySet());
java.util.Collections.sort(sortedKeys, new StringComparator());
}
}
public String getRedirect(java.net.URL url){
return getRedirect(url.toString());
}
//**************************************************************************
//** getRedirect
//**************************************************************************
/** Returns a redirected/updated url. Returns null if there is no redirect.
*/
private String getRedirect(String url){
//Check timestamp of the redirect file. Reparse as needed.
if (file.exists() && file.getDate().getTime()!=lastUpdate) parseRedirects();
//Loop through all the redirects and find a suitable match
java.util.Iterator<String> it = sortedKeys.iterator();
while (it.hasNext()){
String key = it.next();
if (url.toUpperCase().contains(key.toUpperCase())){
String replacement = redirects.get(key);
int x = url.toUpperCase().indexOf(key.toUpperCase());
int y = key.length();
String a = url.substring(0, x);
String b = url.substring(x+y);
return a + replacement + b;
}
}
//If not match is found, return a null
return null;
}
/** Used to compare to strings based on their length. Longer strings will
* appear first.
*/
private class StringComparator implements java.util.Comparator<String> {
public int compare(String t1, String t2) {
return new Integer(t2.length()).compareTo(new Integer(t1.length()));
}
}
}