1 25 26 29 package net.killingar.forum.internal; 30 31 import it.unimi.dsi.fastutil.longs.LongArrayList; 32 33 import java.net.URLEncoder ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.util.StringTokenizer ; 37 38 public class Utils 39 { 40 43 public static String disableSQL(String s) throws NullPointerException 44 { 45 if (s == null)return ""; 47 char[] sa = s.toCharArray(); 48 49 StringBuffer sb = new StringBuffer (sa.length); 50 for (int i = 0; i < sa.length; i++) 51 { 52 switch(sa[i]) 53 { 54 case '\\': 55 sb.append("\\\\"); 56 break; 57 case '\'': 58 sb.append("\\'"); 59 break; 60 case '\"': 61 sb.append("\\\""); 62 break; 63 69 70 96 default: 97 sb.append(sa[i]); 98 break; 99 } 100 } 101 return sb.toString(); 102 } 103 104 105 108 public static String disableHTML(String s) 109 { 110 if (s == null)return ""; 111 112 char[] sa = s.toCharArray(); 113 114 StringBuffer sb = new StringBuffer (sa.length); 115 for (int i = 0; i < sa.length; i++) 116 { 117 switch(sa[i]) 118 { 119 case '<': 120 sb.append("<"); 121 break; 122 case '>': 123 sb.append(">"); 124 break; 125 case '\"': 126 sb.append("""); 127 break; 128 case '&': 129 sb.append("&"); 130 break; 131 default: 132 sb.append(sa[i]); 133 break; 134 } 135 } 136 return sb.toString(); 137 } 138 141 public static String disableQuote(String s) 142 { 143 if (s == null)return ""; 144 145 char[] sa = s.toCharArray(); 146 147 StringBuffer sb = new StringBuffer (sa.length); 148 for (int i = 0; i < sa.length; i++) 149 { 150 switch(sa[i]) 151 { 152 case '\"': 153 sb.append("""); 154 break; 155 default: 156 sb.append(sa[i]); 157 break; 158 } 159 } 160 return sb.toString(); 161 } 162 163 164 167 public static String unHTML(String s) 168 { 169 if (s == null)return ""; 170 171 char[] sa = s.toCharArray(); 172 173 StringBuffer sb = new StringBuffer (sa.length); 174 char c; 175 for (int i = 0; i < sa.length; i++) 176 { 177 c = sa[i]; 178 switch(c) 179 { 180 case '&': 181 { 182 int foo = 3; 183 String tmp = s.substring(i+1, i+foo+1); 184 if (tmp.equals("lt;")) 185 sb.append('<'); 186 else if (tmp.equals("gt;")) 187 sb.append('>'); 188 else 189 { 190 foo = 4; 191 tmp = s.substring(i+1, i+foo+1); 192 193 194 if (tmp.equals("amp;")) 195 sb.append('&'); 196 else 197 { 198 foo = 5; 199 tmp = s.substring(i+1, i+foo+1); 200 201 202 if (tmp.equals("quot;")) 203 sb.append('\"'); 204 else if (tmp.equals("auml;")) 205 sb.append('ä'); 206 else if (tmp.equals("Auml;")) 207 sb.append('Ä'); 208 else if (tmp.equals("Ouml;")) 209 sb.append('Ö'); 210 else if (tmp.equals("ouml;")) 211 sb.append('ö'); 212 else 213 { 214 foo = 6; 215 tmp = s.substring(i+1, i+foo+1); 216 217 218 if (tmp.equals("aring;")) 219 sb.append('å'); 220 else if (tmp.equals("Aring;")) 221 sb.append('Å'); 222 else 223 { 224 sb.append(c); 225 break; 226 } 227 } 228 } 229 } 230 i+=foo; 231 } 232 break; 233 default: 234 sb.append(c); 235 break; 236 } 237 } 238 return sb.toString(); 239 } 240 241 242 246 public static boolean isspace(char c) 247 { 248 switch(c) 249 { 250 case ' ': 251 case '\n': 252 case '\r': 253 case '\t': 254 case '.': 255 case '?': 256 case '!': 257 case ',': 258 case ':': 259 case ';': 260 case '\'': 261 case '\"': 262 case '(': 263 case ')': 264 return true; 265 default: 266 return false; 267 } 268 } 269 270 271 274 public static int nrWords(String s) 275 { 276 if (s == null)return 0; 277 int count = 0; 278 boolean run = false; 279 280 char[] sa = s.toCharArray(); 281 282 for (int i = 0; i < sa.length; i++) 283 { 284 if (isspace(sa[i])) 285 { 286 if (run)count++; 287 run = false; 288 } 289 else run = true; 290 } 291 292 if (run) 293 count++; 294 return count; 295 } 296 297 300 public static long[] getLongsFromResult(ResultSet result) throws SQLException 301 { 302 LongArrayList a = new LongArrayList(); 303 304 for(int i = 0; result.next(); i++) 305 a.add(result.getLong(1)); 306 307 if (a.size() == 0) 308 return new long[0]; 309 310 long r[] = new long[a.size()]; 311 a.toArray(r); 312 313 return r; 314 } 315 316 private static long[] noResultLongs = new long[0]; 317 public static long[] getLongsFromString(String inList) 318 { 319 if (inList == null) 320 return noResultLongs; 321 322 StringTokenizer t = new StringTokenizer (inList, ", \t"); 323 long[] r = new long[t.countTokens()]; 324 int i = 0; 325 while (t.hasMoreTokens()) 326 r[i++] = Long.parseLong(t.nextToken()); 327 328 return r; 329 } 330 331 public String URLEncodeParam(String name, Object value) 332 { 333 StringBuffer link = new StringBuffer (); 334 335 if (value != null) 336 { 337 if (value instanceof String ) 338 { 339 link.append(name); 340 link.append('='); 341 link.append(URLEncoder.encode((String ) value)); 342 } 343 else 344 { 345 String [] values = (String []) value; 346 link.append(name); 347 link.append('='); 348 link.append(URLEncoder.encode(values[0])); 349 } 350 } 351 352 return link.toString(); 353 } 354 355 public String URLEncodeParams(java.util.Map m) 356 { 357 StringBuffer link = new StringBuffer (); 358 359 if (link.toString().indexOf("?") == -1) 360 link.append('?'); 361 else 362 link.append("&"); 363 364 java.util.Iterator i = m.entrySet().iterator(); 366 while (i.hasNext()) 367 { 368 java.util.Map.Entry entry = (java.util.Map.Entry)i.next(); 369 link.append(URLEncodeParam((String )entry.getKey(), entry.getValue())); 370 371 if (i.hasNext()) 372 link.append("&"); 373 } 374 return link.toString(); 375 } 376 } | Popular Tags |