1 package org.thema;
2
3 import java.util.regex.Pattern;
4
5 /***
6 * Classe com métodos utilitários para serem utilizados em qualquer projeto
7 *
8 * @author Eduardo M. Sasso
9 * @since Jan 12, 2004
10 */
11 public class Util {
12
13 /***
14 *
15 * @param str - String a ser substituída
16 * @param oldStr - String antiga
17 * @param newStr - String nova
18 * @param all - Todas as ocorrências
19 * @return Nova string substituindo oldStr por newStr
20 */
21 public static String replace(String str, String oldStr, String newStr, boolean all) {
22 Pattern pattern = Pattern.compile(oldStr,Pattern.CASE_INSENSITIVE);
23
24 String resultado = str;
25
26 if (pattern.matcher(str).find()) {
27 if (all) {
28 resultado = pattern.matcher(str).replaceAll(newStr);
29 } else {
30 resultado = pattern.matcher(str).replaceFirst(newStr);
31 }
32 }
33 return resultado;
34 /*
35 if (str == null || oldStr == null || oldStr.length() == 0 || newStr == null)
36 throw new IllegalArgumentException("String nula ou vazia!");
37 StringBuffer result = null;
38 int oldpos = 0;
39 do {
40 int pos = str.indexOf(oldStr, oldpos);
41 if (pos < 0)
42 break;
43 if (result == null)
44 result = new StringBuffer();
45 result.append(str.substring(oldpos, pos));
46 result.append(newStr);
47 pos += oldStr.length();
48 oldpos = pos;
49 } while (all);
50 if (oldpos == 0) {
51 return str;
52 } else {
53 result.append(str.substring(oldpos));
54 return new String(result);
55 }
56 */
57 }
58
59 /*
60 * TODO: Recuperar como um ArrayList para varias tabelas..
61 */
62 public static String getTableName(String sql){
63 int begin = sql.toLowerCase().indexOf("from")+4;
64 int end = sql.toLowerCase().indexOf("where");
65 end = end==-1?sql.length():end;
66 String tableName = sql.substring(begin, end).trim();
67 return tableName;
68 }
69
70
71 }