1 15 package org.josql.functions; 16 17 import java.io.File ; 18 import java.io.FileReader ; 19 import java.io.BufferedReader ; 20 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import org.josql.functions.regexp.RegExpFactory; 25 import org.josql.functions.regexp.RegExp; 26 27 import org.josql.Query; 28 import org.josql.QueryExecutionException; 29 30 37 public class StringFunctions extends AbstractFunctionHandler 38 { 39 40 public static final String HANDLER_ID = "_internal_string"; 41 42 51 public boolean regexp (Object o, 52 String re) 53 throws QueryExecutionException 54 { 55 56 RegExp regexp = RegExpFactory.getDefaultInstance (); 57 58 if (regexp == null) 59 { 60 61 throw new QueryExecutionException ("No default regular expression library available for: " + 62 RegExpFactory.getDefaultInstanceName ()); 63 64 } 65 66 if (o == null) 67 { 68 69 return false; 70 71 } 72 73 String v = o.toString (); 74 75 return regexp.match (re, 76 v); 77 78 } 79 80 91 public boolean regexp (Object o, 92 String re, 93 String instName) 94 throws QueryExecutionException 95 { 96 97 RegExp regexp = RegExpFactory.getInstance (instName); 98 99 if (regexp == null) 100 { 101 102 throw new QueryExecutionException ("No regular expression library available for: " + 103 instName); 104 105 } 106 107 if (o == null) 108 { 109 110 return false; 111 112 } 113 114 String v = o.toString (); 115 116 return regexp.match (re, 117 v); 118 119 } 120 121 132 public List grep (File f, 133 String s, 134 boolean ignoreCase) 135 throws QueryExecutionException 136 { 137 138 if ((f == null) 139 || 140 (!f.exists ()) 141 || 142 (f.isDirectory ()) 143 || 144 (!f.canRead ()) 145 ) 146 { 147 148 return null; 149 150 } 151 152 List retData = new ArrayList (); 153 154 try 155 { 156 157 BufferedReader br = new BufferedReader (new FileReader (f)); 158 159 String l = br.readLine (); 160 161 int lc = 1; 162 163 String ss = s; 164 165 if (ignoreCase) 166 { 167 168 ss = s.toLowerCase (); 169 170 } 171 172 while (l != null) 173 { 174 175 int ind = -1; 176 177 if (ignoreCase) 178 { 179 180 ind = l.toLowerCase ().indexOf (ss); 181 182 } else { 183 184 ind = l.indexOf (ss); 185 186 } 187 188 if (ind != -1) 189 { 190 191 retData.add (new FileMatch (f, 192 lc, 193 ind, 194 s, 195 l)); 196 197 } 198 199 l = br.readLine (); 200 201 lc++; 202 203 } 204 205 br.close (); 206 207 } catch (Exception e) { 208 209 throw new QueryExecutionException ("Unable to read from file: " + 210 f, 211 e); 212 213 } 214 215 return retData; 216 217 } 218 219 236 public List rgrep (File f, 237 String regexp, 238 String instName) 239 throws QueryExecutionException 240 { 241 242 RegExp reImpl = RegExpFactory.getInstance (instName); 243 244 if (reImpl == null) 245 { 246 247 throw new QueryExecutionException ("No default regular expression library available for: " + 248 instName); 249 250 } 251 252 return this.rgrep (f, 253 regexp, 254 reImpl); 255 256 } 257 258 273 public List rgrep (File f, 274 String regexp) 275 throws QueryExecutionException 276 { 277 278 RegExp reImpl = RegExpFactory.getDefaultInstance (); 279 280 if (reImpl == null) 281 { 282 283 throw new QueryExecutionException ("No default regular expression library available for: " + 284 RegExpFactory.getDefaultInstanceName ()); 285 286 } 287 288 return this.rgrep (f, 289 regexp, 290 reImpl); 291 292 } 293 294 private List rgrep (File f, 295 String regexp, 296 RegExp reImpl) 297 throws QueryExecutionException 298 { 299 300 if ((f == null) 301 || 302 (!f.exists ()) 303 || 304 (f.isDirectory ()) 305 || 306 (!f.canRead ()) 307 ) 308 { 309 310 return null; 311 312 } 313 314 List retData = new ArrayList (); 315 316 try 317 { 318 319 BufferedReader br = new BufferedReader (new FileReader (f)); 320 321 String l = br.readLine (); 322 323 int lc = 1; 324 325 while (l != null) 326 { 327 328 if (reImpl.match (regexp, 329 l)) 330 { 331 332 retData.add (new FileMatch (f, 333 lc, 334 -1, 335 regexp, 336 l)); 337 338 } 339 340 l = br.readLine (); 341 342 lc++; 343 344 } 345 346 br.close (); 347 348 } catch (Exception e) { 349 350 throw new QueryExecutionException ("Unable to read from file: " + 351 f, 352 e); 353 354 } 355 356 return retData; 357 358 } 359 360 369 public List grep (File f, 370 String s) 371 throws QueryExecutionException 372 { 373 374 return this.grep (f, 375 s, 376 false); 377 378 } 379 380 389 public String trim (Object o, 390 Object v) 391 { 392 393 if (o == null) 394 { 395 396 return null; 397 398 } 399 400 String os = o.toString (); 401 402 if (v == null) 403 { 404 405 return os.trim (); 406 407 } 408 409 String vs = v.toString (); 410 411 if (os.endsWith (vs)) 412 { 413 414 os = os.substring (0, 415 vs.length ()); 416 417 } 418 419 if (os.startsWith (vs)) 420 { 421 422 os = os.substring (vs.length ()); 423 424 } 425 426 return os; 427 428 } 429 430 440 public double lastIndexOf (Object o, 441 Object i) 442 { 443 444 if (o == null) 445 { 446 447 return -1; 448 449 } 450 451 if (i == null) 452 { 453 454 return -1; 455 456 } 457 458 String os = o.toString (); 459 String is = i.toString (); 460 461 return os.lastIndexOf (is); 462 463 } 464 465 474 public String subStr (Object o, 475 double f) 476 { 477 478 return this.subStr (o, 479 f, 480 -1); 481 482 } 483 484 496 public String subStr (Object o, 497 double f, 498 double t) 499 { 500 501 if (o == null) 502 { 503 504 return null; 505 506 } 507 508 int fi = (int) f; 509 int ti = (int) t; 510 511 String s = o.toString (); 512 513 if ((fi < 0) 514 || 515 (fi > s.length ()) 516 ) 517 { 518 519 return ""; 520 521 } 522 523 if ((ti < fi) 524 || 525 (ti > s.length ()) 526 ) 527 { 528 529 ti = -1; 530 531 } 532 533 if (ti == -1) 534 { 535 536 return s.substring (fi); 537 538 } 539 540 return s.substring (fi, 541 ti); 542 543 } 544 545 public double length (Object o) 546 { 547 548 if (o == null) 549 { 550 551 return 0; 552 553 } 554 555 if (o instanceof String ) 556 { 557 558 return ((String ) o).length (); 559 560 } 561 562 return o.toString ().length (); 563 564 } 565 566 } 567 | Popular Tags |