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 |
// ############################################################# |
791 |
23 Oct 06 |
olle |
55 |
package se.lu.thep.coreftpd.common; |
741 |
10 Oct 06 |
olle |
56 |
|
741 |
10 Oct 06 |
olle |
57 |
/** |
1652 |
22 May 07 |
gregory |
58 |
* |
1652 |
22 May 07 |
gregory |
* <B>How to use:</B> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>MyHashTable MHT=new MyHashTable(String s);</CODE> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>MyHashTable MHT=new MyHashTable(String s, String separator1, String |
1652 |
22 May 07 |
gregory |
* separator2);</CODE> <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
64 |
* |
1652 |
22 May 07 |
gregory |
* <B>More info:</B> <BR> |
1652 |
22 May 07 |
gregory |
* Creates something that looks like a hash table. <BR> |
1652 |
22 May 07 |
gregory |
* You give an key (<CODE>String</CODE>) and receive an value (<CODE>String</CODE>) |
1652 |
22 May 07 |
gregory |
* (the key is case insensitive). <BR> |
1652 |
22 May 07 |
gregory |
* You can't add/remove Strings to your <CODE>MyHashTable</CODE>. <BR> |
1652 |
22 May 07 |
gregory |
* When you once have created it, it remains constant. <BR> |
741 |
10 Oct 06 |
olle |
* NOTE: The <I>key</I>s and the <I>value</I>s must not contain <I>separator1</I>. |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
* The <I>key</I>s must not contain <I>separator2</I>. <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
75 |
* |
1652 |
22 May 07 |
gregory |
* Example: <BR> |
1652 |
22 May 07 |
gregory |
* s = "key1=value1,key2=value2,key3=value3" <BR> |
1652 |
22 May 07 |
gregory |
* or <BR> |
1652 |
22 May 07 |
gregory |
* s = "key1=value1,key2=value=2,key3=v=a=l=u=e=3" <BR> |
1652 |
22 May 07 |
gregory |
80 |
* |
741 |
10 Oct 06 |
olle |
* <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
83 |
* |
1652 |
22 May 07 |
gregory |
* If you use the constructor with only one argument, <I>separator1</I> and |
1652 |
22 May 07 |
gregory |
* <I>separator2</I> are defined as: <BR> |
1652 |
22 May 07 |
gregory |
* separator1=","; <BR> |
741 |
10 Oct 06 |
olle |
* separator2="="; |
1652 |
22 May 07 |
gregory |
88 |
* |
741 |
10 Oct 06 |
olle |
* <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
91 |
* |
1652 |
22 May 07 |
gregory |
* Note: "<I>key1=value1,keyNoEqualSignValue,key3=value3</I>" will generate |
1652 |
22 May 07 |
gregory |
* these pairs: <BR> |
1652 |
22 May 07 |
gregory |
* key1="value1" <BR> |
1652 |
22 May 07 |
gregory |
* keyNoEqualSignValue="" <BR> |
741 |
10 Oct 06 |
olle |
* key3="value3" |
1652 |
22 May 07 |
gregory |
97 |
* |
1652 |
22 May 07 |
gregory |
98 |
* |
1652 |
22 May 07 |
gregory |
99 |
* |
1652 |
22 May 07 |
gregory |
100 |
* |
741 |
10 Oct 06 |
olle |
* <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
741 |
10 Oct 06 |
olle |
* For example: s="php=php,php3=php,php4=php,pl=perl,cgi=perl" |
1652 |
22 May 07 |
gregory |
104 |
* |
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 |
107 |
*/ |
741 |
10 Oct 06 |
olle |
108 |
|
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 |
111 |
final public class MyHashTable { |
1652 |
22 May 07 |
gregory |
112 |
private String[] sa_keys; |
741 |
10 Oct 06 |
olle |
113 |
|
1652 |
22 May 07 |
gregory |
114 |
private String[] sa_values; |
1652 |
22 May 07 |
gregory |
115 |
|
1652 |
22 May 07 |
gregory |
// private String s_wholeString; |
741 |
10 Oct 06 |
olle |
117 |
private int i_sizeOfArray; |
741 |
10 Oct 06 |
olle |
118 |
|
741 |
10 Oct 06 |
olle |
119 |
/** |
1652 |
22 May 07 |
gregory |
* Creates a hash table with <CODE>separator1=","</CODE> and <CODE>separator2="="</CODE> |
1652 |
22 May 07 |
gregory |
121 |
* |
1652 |
22 May 07 |
gregory |
* @param txtToMakeHashWith |
1652 |
22 May 07 |
gregory |
* "key1=value1,key2=value2,key3=value3" |
1652 |
22 May 07 |
gregory |
124 |
*/ |
1652 |
22 May 07 |
gregory |
125 |
public MyHashTable(String txtToMakeHashWith) { |
741 |
10 Oct 06 |
olle |
126 |
this(txtToMakeHashWith, ",", "="); |
741 |
10 Oct 06 |
olle |
127 |
} |
741 |
10 Oct 06 |
olle |
128 |
|
741 |
10 Oct 06 |
olle |
129 |
/** |
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 |
136 |
*/ |
1652 |
22 May 07 |
gregory |
137 |
public MyHashTable(String txtToMakeHashWith, String separator1, |
1652 |
22 May 07 |
gregory |
138 |
String separator2) { |
1652 |
22 May 07 |
gregory |
139 |
String s_wholeString = txtToMakeHashWith; // Previously this was a |
1652 |
22 May 07 |
gregory |
// global variable |
1652 |
22 May 07 |
gregory |
141 |
String[] sa_nyckelVardePar = MyString.makeArrayOfString( |
1652 |
22 May 07 |
gregory |
142 |
txtToMakeHashWith, separator1); |
1652 |
22 May 07 |
gregory |
143 |
i_sizeOfArray = sa_nyckelVardePar.length; |
1652 |
22 May 07 |
gregory |
144 |
sa_keys = new String[i_sizeOfArray]; |
1652 |
22 May 07 |
gregory |
145 |
sa_values = new String[i_sizeOfArray]; |
741 |
10 Oct 06 |
olle |
146 |
|
1652 |
22 May 07 |
gregory |
147 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
148 |
if (sa_nyckelVardePar[i].indexOf(separator2) != -1) { |
1652 |
22 May 07 |
gregory |
149 |
sa_keys[i] = sa_nyckelVardePar[i].substring(0, |
1652 |
22 May 07 |
gregory |
150 |
sa_nyckelVardePar[i].indexOf(separator2)); |
1652 |
22 May 07 |
gregory |
151 |
sa_values[i] = sa_nyckelVardePar[i] |
1652 |
22 May 07 |
gregory |
152 |
.substring(sa_nyckelVardePar[i].indexOf(separator2) |
1652 |
22 May 07 |
gregory |
153 |
+ separator2.length()); |
1652 |
22 May 07 |
gregory |
154 |
} else { |
1652 |
22 May 07 |
gregory |
155 |
sa_keys[i] = sa_nyckelVardePar[i]; |
1652 |
22 May 07 |
gregory |
156 |
sa_values[i] = ""; |
741 |
10 Oct 06 |
olle |
157 |
} |
741 |
10 Oct 06 |
olle |
158 |
} |
741 |
10 Oct 06 |
olle |
159 |
} |
741 |
10 Oct 06 |
olle |
160 |
|
741 |
10 Oct 06 |
olle |
161 |
/** |
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 |
164 |
*/ |
1652 |
22 May 07 |
gregory |
165 |
public void unescapeAllValues() { |
1652 |
22 May 07 |
gregory |
166 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
167 |
sa_values[i] = MyString.unescape(sa_values[i]); |
741 |
10 Oct 06 |
olle |
168 |
} |
741 |
10 Oct 06 |
olle |
169 |
} |
741 |
10 Oct 06 |
olle |
170 |
|
741 |
10 Oct 06 |
olle |
171 |
/** |
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 |
174 |
*/ |
1652 |
22 May 07 |
gregory |
175 |
public void unescapeMakePlusesIntoSpacesAllValues() { |
1652 |
22 May 07 |
gregory |
176 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
177 |
sa_values[i] = MyString.unescapeMakePlusesIntoSpaces(sa_values[i]); |
741 |
10 Oct 06 |
olle |
178 |
} |
741 |
10 Oct 06 |
olle |
179 |
} |
741 |
10 Oct 06 |
olle |
180 |
|
741 |
10 Oct 06 |
olle |
181 |
/** |
1652 |
22 May 07 |
gregory |
* Give the value at "index" <CODE>key</CODE>. |
1652 |
22 May 07 |
gregory |
183 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns null if key doesn't exist. |
1652 |
22 May 07 |
gregory |
185 |
*/ |
1652 |
22 May 07 |
gregory |
186 |
public String giveValueByIndex(String key) { |
1652 |
22 May 07 |
gregory |
187 |
/* |
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 |
190 |
*/ |
741 |
10 Oct 06 |
olle |
191 |
|
1652 |
22 May 07 |
gregory |
192 |
for (int i = 0; i < i_sizeOfArray; i++) |
741 |
10 Oct 06 |
olle |
193 |
if (key.equalsIgnoreCase(sa_keys[i])) |
741 |
10 Oct 06 |
olle |
194 |
return sa_values[i]; |
741 |
10 Oct 06 |
olle |
195 |
|
741 |
10 Oct 06 |
olle |
196 |
return null; |
741 |
10 Oct 06 |
olle |
197 |
} |
741 |
10 Oct 06 |
olle |
198 |
|
741 |
10 Oct 06 |
olle |
199 |
/** |
1652 |
22 May 07 |
gregory |
* Give the unescaped value at "index" <CODE>key</CODE>. |
1652 |
22 May 07 |
gregory |
201 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns null if key doesn't exist. |
1652 |
22 May 07 |
gregory |
203 |
*/ |
1652 |
22 May 07 |
gregory |
204 |
public String giveUnescapedValueByIndex(String key) { |
1652 |
22 May 07 |
gregory |
205 |
/* |
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 |
208 |
*/ |
741 |
10 Oct 06 |
olle |
209 |
|
1652 |
22 May 07 |
gregory |
210 |
for (int i = 0; i < i_sizeOfArray; i++) |
741 |
10 Oct 06 |
olle |
211 |
if (key.equalsIgnoreCase(sa_keys[i])) |
741 |
10 Oct 06 |
olle |
212 |
return MyString.unescape(sa_values[i]); |
741 |
10 Oct 06 |
olle |
213 |
|
741 |
10 Oct 06 |
olle |
214 |
return null; |
741 |
10 Oct 06 |
olle |
215 |
} |
741 |
10 Oct 06 |
olle |
216 |
|
741 |
10 Oct 06 |
olle |
217 |
/** |
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 |
221 |
* |
1652 |
22 May 07 |
gregory |
* @return Returns -1 if key doesn't exist |
1652 |
22 May 07 |
gregory |
223 |
*/ |
1652 |
22 May 07 |
gregory |
224 |
public int giveBiggestKeySize(String bigKey) { |
1652 |
22 May 07 |
gregory |
225 |
if (bigKey == null) |
741 |
10 Oct 06 |
olle |
226 |
return -1; |
741 |
10 Oct 06 |
olle |
227 |
|
1652 |
22 May 07 |
gregory |
228 |
int largestCurrentKey = -1; |
1652 |
22 May 07 |
gregory |
229 |
for (int i = 0; i < i_sizeOfArray; i++) { |
1652 |
22 May 07 |
gregory |
230 |
if (bigKey.length() >= sa_keys[i].length()) |
1652 |
22 May 07 |
gregory |
231 |
if (sa_keys[i].equalsIgnoreCase(bigKey.substring(0, sa_keys[i] |
1652 |
22 May 07 |
gregory |
232 |
.length()))) |
1652 |
22 May 07 |
gregory |
233 |
if (sa_keys[i].length() > largestCurrentKey) |
1652 |
22 May 07 |
gregory |
234 |
largestCurrentKey = sa_keys[i].length(); |
741 |
10 Oct 06 |
olle |
235 |
} |
741 |
10 Oct 06 |
olle |
236 |
|
741 |
10 Oct 06 |
olle |
237 |
return largestCurrentKey; |
741 |
10 Oct 06 |
olle |
238 |
} |
741 |
10 Oct 06 |
olle |
239 |
} |