741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
1652 |
22 May 07 |
gregory |
55 |
package se.lu.thep.coreftpd.ftp_server; |
741 |
10 Oct 06 |
olle |
56 |
|
791 |
23 Oct 06 |
olle |
57 |
import se.lu.thep.coreftpd.common.MyString; |
741 |
10 Oct 06 |
olle |
58 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
59 |
|
741 |
10 Oct 06 |
olle |
60 |
/** |
1652 |
22 May 07 |
gregory |
61 |
* |
1652 |
22 May 07 |
gregory |
* <B>About this class:</B> <BR> |
1652 |
22 May 07 |
gregory |
* This is similar to <CODE>MyHashTable</CODE> except this is comparing keys |
1652 |
22 May 07 |
gregory |
* cAsE sEnSiTiVe when using Windows and cAsE iNsEnSiTiVe when when using for |
1652 |
22 May 07 |
gregory |
* example UNIX, Linux, Mac etc. |
1652 |
22 May 07 |
gregory |
66 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
69 |
*/ |
741 |
10 Oct 06 |
olle |
70 |
|
1652 |
22 May 07 |
gregory |
// Note: To make MyHashTable CaSe SeNsItIvE, change all "equalsIgnoreCase" to |
1652 |
22 May 07 |
gregory |
// "equals" |
1652 |
22 May 07 |
gregory |
73 |
final public class MyHashTableWithPaths { |
1652 |
22 May 07 |
gregory |
74 |
private String[] sa_keys; |
741 |
10 Oct 06 |
olle |
75 |
|
1652 |
22 May 07 |
gregory |
76 |
private String[] sa_values; |
1652 |
22 May 07 |
gregory |
77 |
|
1652 |
22 May 07 |
gregory |
// private String s_wholeString; |
741 |
10 Oct 06 |
olle |
79 |
private int i_sizeOfArray; |
741 |
10 Oct 06 |
olle |
80 |
|
1652 |
22 May 07 |
gregory |
81 |
private static boolean isWindows = File.separatorChar == '\\'; |
741 |
10 Oct 06 |
olle |
82 |
|
741 |
10 Oct 06 |
olle |
83 |
/** |
1652 |
22 May 07 |
gregory |
* Creates a hash table with <CODE>separator1=","</CODE> and <CODE>separator2="="</CODE> |
1652 |
22 May 07 |
gregory |
85 |
* |
1652 |
22 May 07 |
gregory |
* @param txtToMakeHashWith |
1652 |
22 May 07 |
gregory |
* "key1=value1,key2=value2,key3=value3" |
1652 |
22 May 07 |
gregory |
88 |
*/ |
1652 |
22 May 07 |
gregory |
89 |
public MyHashTableWithPaths(String txtToMakeHashWith) { |
741 |
10 Oct 06 |
olle |
90 |
this(txtToMakeHashWith, ",", "="); |
741 |
10 Oct 06 |
olle |
91 |
} |
741 |
10 Oct 06 |
olle |
92 |
|
741 |
10 Oct 06 |
olle |
93 |
/** |
1652 |
22 May 07 |
gregory |
* @param txtToMakeHashWith = |
1652 |
22 May 07 |
gregory |
* "key1=value1,key2=value2,key3=value3" |
1652 |
22 May 07 |
gregory |
* @param separator1 |
1652 |
22 May 07 |
gregory |
* by default <CODE>==','</CODE>; |
1652 |
22 May 07 |
gregory |
* @param separator2 |
1652 |
22 May 07 |
gregory |
* by default <CODE>=='='</CODE>; |
1652 |
22 May 07 |
gregory |
100 |
*/ |
1652 |
22 May 07 |
gregory |
101 |
public MyHashTableWithPaths(String txtToMakeHashWith, String separator1, |
1652 |
22 May 07 |
gregory |
102 |
String separator2) { |
1652 |
22 May 07 |
gregory |
103 |
String s_wholeString = txtToMakeHashWith; // Previously this was a |
1652 |
22 May 07 |
gregory |
// global variable |
1652 |
22 May 07 |
gregory |
105 |
String[] sa_nyckelVardePar = MyString.makeArrayOfString( |
1652 |
22 May 07 |
gregory |
106 |
txtToMakeHashWith, separator1); |
1652 |
22 May 07 |
gregory |
107 |
i_sizeOfArray = sa_nyckelVardePar.length; |
1652 |
22 May 07 |
gregory |
108 |
sa_keys = new String[i_sizeOfArray]; |
1652 |
22 May 07 |
gregory |
109 |
sa_values = new String[i_sizeOfArray]; |
741 |
10 Oct 06 |
olle |
110 |
|
1652 |
22 May 07 |
gregory |
111 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
112 |
if (sa_nyckelVardePar[i].indexOf(separator2) != -1) { |
1652 |
22 May 07 |
gregory |
113 |
sa_keys[i] = sa_nyckelVardePar[i].substring(0, |
1652 |
22 May 07 |
gregory |
114 |
sa_nyckelVardePar[i].indexOf(separator2)); |
1652 |
22 May 07 |
gregory |
115 |
sa_values[i] = sa_nyckelVardePar[i] |
1652 |
22 May 07 |
gregory |
116 |
.substring(sa_nyckelVardePar[i].indexOf(separator2) |
1652 |
22 May 07 |
gregory |
117 |
+ separator2.length()); |
1652 |
22 May 07 |
gregory |
118 |
} else { |
1652 |
22 May 07 |
gregory |
119 |
sa_keys[i] = sa_nyckelVardePar[i]; |
1652 |
22 May 07 |
gregory |
120 |
sa_values[i] = ""; |
741 |
10 Oct 06 |
olle |
121 |
} |
741 |
10 Oct 06 |
olle |
122 |
} |
741 |
10 Oct 06 |
olle |
123 |
} |
741 |
10 Oct 06 |
olle |
124 |
|
741 |
10 Oct 06 |
olle |
125 |
/** |
1652 |
22 May 07 |
gregory |
* This will unescape (<CODE>Mystring.unescape(java.lang.String)</CODE>) |
1652 |
22 May 07 |
gregory |
* all values stored in this HashTable. |
1652 |
22 May 07 |
gregory |
128 |
*/ |
1652 |
22 May 07 |
gregory |
129 |
public void unescapeAllValues() { |
1652 |
22 May 07 |
gregory |
130 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
131 |
sa_values[i] = MyString.unescape(sa_values[i]); |
741 |
10 Oct 06 |
olle |
132 |
} |
741 |
10 Oct 06 |
olle |
133 |
} |
741 |
10 Oct 06 |
olle |
134 |
|
741 |
10 Oct 06 |
olle |
135 |
/** |
1652 |
22 May 07 |
gregory |
* All values in this HashTable will be unescaped and all plus signes (+) |
1652 |
22 May 07 |
gregory |
* will be converted into spaces ( ) (<CODE>Mystring.unescapeMakePlusesIntoSpaces(java.lang.String)</CODE>). |
1652 |
22 May 07 |
gregory |
138 |
*/ |
1652 |
22 May 07 |
gregory |
139 |
public void unescapeMakePlusesIntoSpacesAllValues() { |
1652 |
22 May 07 |
gregory |
140 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
141 |
sa_values[i] = MyString.unescapeMakePlusesIntoSpaces(sa_values[i]); |
741 |
10 Oct 06 |
olle |
142 |
} |
741 |
10 Oct 06 |
olle |
143 |
} |
741 |
10 Oct 06 |
olle |
144 |
|
741 |
10 Oct 06 |
olle |
145 |
/** |
1652 |
22 May 07 |
gregory |
* Give the value at "index" <CODE>key</CODE>. |
1652 |
22 May 07 |
gregory |
147 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns null if key doesn't exist. |
1652 |
22 May 07 |
gregory |
149 |
*/ |
1652 |
22 May 07 |
gregory |
150 |
public String giveValueByIndex(String key) { |
1652 |
22 May 07 |
gregory |
151 |
/* |
1652 |
22 May 07 |
gregory |
* //not necessery, but makes this method faster (is this really |
1652 |
22 May 07 |
gregory |
* faster?) if (s_wholeString.indexOf(key)==-1) return null; |
1652 |
22 May 07 |
gregory |
154 |
*/ |
741 |
10 Oct 06 |
olle |
155 |
|
1652 |
22 May 07 |
gregory |
156 |
for (int i = 0; i < i_sizeOfArray; i++) |
741 |
10 Oct 06 |
olle |
157 |
if (keyEqual(key, sa_keys[i])) |
741 |
10 Oct 06 |
olle |
158 |
return sa_values[i]; |
741 |
10 Oct 06 |
olle |
159 |
|
741 |
10 Oct 06 |
olle |
160 |
return null; |
741 |
10 Oct 06 |
olle |
161 |
} |
741 |
10 Oct 06 |
olle |
162 |
|
741 |
10 Oct 06 |
olle |
163 |
/** |
1652 |
22 May 07 |
gregory |
* Give the unescaped value at "index" <CODE>key</CODE>. |
1652 |
22 May 07 |
gregory |
165 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns null if key doesn't exist. |
1652 |
22 May 07 |
gregory |
167 |
*/ |
1652 |
22 May 07 |
gregory |
168 |
public String giveUnescapedValueByIndex(String key) { |
1652 |
22 May 07 |
gregory |
169 |
/* |
1652 |
22 May 07 |
gregory |
* //not necessery, but makes this method faster (is this really |
1652 |
22 May 07 |
gregory |
* faster?) if (s_wholeString.indexOf(key)==-1) return null; |
1652 |
22 May 07 |
gregory |
172 |
*/ |
741 |
10 Oct 06 |
olle |
173 |
|
1652 |
22 May 07 |
gregory |
174 |
for (int i = 0; i < i_sizeOfArray; i++) |
1652 |
22 May 07 |
gregory |
175 |
if (keyEqual(key, sa_keys[i])) |
741 |
10 Oct 06 |
olle |
176 |
return MyString.unescape(sa_values[i]); |
741 |
10 Oct 06 |
olle |
177 |
|
741 |
10 Oct 06 |
olle |
178 |
return null; |
741 |
10 Oct 06 |
olle |
179 |
} |
741 |
10 Oct 06 |
olle |
180 |
|
741 |
10 Oct 06 |
olle |
181 |
/** |
1652 |
22 May 07 |
gregory |
* If the <CODE>bigKey</CODE> is "aaabcd" and there is an index "aaa" and |
1652 |
22 May 07 |
gregory |
* an index "aa", then "aaa".lenth() (=3) is returned (<CODE>bigKey</CODE> |
1652 |
22 May 07 |
gregory |
* shall be longer than the real index). |
1652 |
22 May 07 |
gregory |
185 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns -1 if key doesn't exist |
1652 |
22 May 07 |
gregory |
187 |
*/ |
1652 |
22 May 07 |
gregory |
188 |
public int giveBiggestKeySize(String bigKey) { |
1652 |
22 May 07 |
gregory |
189 |
if (bigKey == null) |
741 |
10 Oct 06 |
olle |
190 |
return -1; |
741 |
10 Oct 06 |
olle |
191 |
|
1652 |
22 May 07 |
gregory |
192 |
int largestCurrentKey = -1; |
741 |
10 Oct 06 |
olle |
193 |
|
1652 |
22 May 07 |
gregory |
194 |
for (int i = 0, sharedPathsLength = sa_keys.length; i < sharedPathsLength; i++) // Optimization... |
741 |
10 Oct 06 |
olle |
195 |
{ |
1652 |
22 May 07 |
gregory |
196 |
String tmpDir = sa_keys[i];// .replace('\\','/'); |
741 |
10 Oct 06 |
olle |
197 |
|
1652 |
22 May 07 |
gregory |
198 |
if (tmpDir.endsWith("*")) { |
1652 |
22 May 07 |
gregory |
199 |
int pathLengthWithAsterix = tmpDir.length() - 1; |
1652 |
22 May 07 |
gregory |
200 |
if (largestCurrentKey < pathLengthWithAsterix) { |
1652 |
22 May 07 |
gregory |
201 |
if (tmpDir.regionMatches(isWindows, 0, bigKey, 0, |
1652 |
22 May 07 |
gregory |
202 |
pathLengthWithAsterix)) { |
1652 |
22 May 07 |
gregory |
203 |
if (largestCurrentKey < pathLengthWithAsterix) |
1652 |
22 May 07 |
gregory |
204 |
largestCurrentKey = pathLengthWithAsterix; |
741 |
10 Oct 06 |
olle |
205 |
} |
741 |
10 Oct 06 |
olle |
206 |
} |
1652 |
22 May 07 |
gregory |
207 |
} else if (keyEqual(tmpDir, bigKey)) { |
1652 |
22 May 07 |
gregory |
208 |
int lengthNow = tmpDir.length(); |
1652 |
22 May 07 |
gregory |
209 |
if (largestCurrentKey < lengthNow) |
1652 |
22 May 07 |
gregory |
210 |
largestCurrentKey = lengthNow; |
741 |
10 Oct 06 |
olle |
211 |
} |
741 |
10 Oct 06 |
olle |
212 |
} |
741 |
10 Oct 06 |
olle |
213 |
|
1652 |
22 May 07 |
gregory |
214 |
/* |
1652 |
22 May 07 |
gregory |
* for (int i=0; i<i_sizeOfArray; i++) { if |
1652 |
22 May 07 |
gregory |
* (bigKey.length()>=sa_keys[i].length()) if |
1652 |
22 May 07 |
gregory |
* (keyEqual(sa_keys[i],bigKey.substring(0,sa_keys[i].length()))) if |
1652 |
22 May 07 |
gregory |
* (sa_keys[i].length()>largestCurrentKey) |
1652 |
22 May 07 |
gregory |
* largestCurrentKey=sa_keys[i].length(); } |
1652 |
22 May 07 |
gregory |
220 |
*/ |
741 |
10 Oct 06 |
olle |
221 |
|
741 |
10 Oct 06 |
olle |
222 |
return largestCurrentKey; |
1652 |
22 May 07 |
gregory |
223 |
} |
741 |
10 Oct 06 |
olle |
224 |
|
741 |
10 Oct 06 |
olle |
225 |
/** |
1652 |
22 May 07 |
gregory |
* This is instead of an "equal()" or "equalIgnoreCase()" so we can choose |
1652 |
22 May 07 |
gregory |
* when we shall consider |
1652 |
22 May 07 |
gregory |
228 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns true if k1 and k2 equals each other |
1652 |
22 May 07 |
gregory |
230 |
*/ |
1652 |
22 May 07 |
gregory |
231 |
private boolean keyEqual(String k1, String k2) { |
1652 |
22 May 07 |
gregory |
232 |
if (isWindows) // If Windows... |
741 |
10 Oct 06 |
olle |
233 |
{ |
741 |
10 Oct 06 |
olle |
234 |
return k1.equalsIgnoreCase(k2); |
1652 |
22 May 07 |
gregory |
235 |
} else { |
741 |
10 Oct 06 |
olle |
236 |
return k1.equals(k2); |
741 |
10 Oct 06 |
olle |
237 |
} |
741 |
10 Oct 06 |
olle |
238 |
} |
741 |
10 Oct 06 |
olle |
239 |
} |