1 package in.co.daffodil.db.jdbc; 2 3 public class EscapeParser { 4 5 static final char SPACE = (char) 32; 6 7 public static String parse(String str) { 8 String toRetutn = ""; 9 int length = str.length(); 10 int count = 0; 11 boolean ignore = false; 12 try { 13 for (int i = 0; i < length; i++) { 14 char ch = str.charAt(i); 15 if (ch == '{' && !ignore) { 16 count++; 17 String toSubstitute = ""; 18 while (str.charAt(++i) == SPACE) 19 ; 20 ch = str.charAt(i); 21 while (ch != SPACE && ch != '{' && ch != '}') { 22 toSubstitute += ch; 23 ch = str.charAt(++i); 24 } 25 toRetutn += getSubstitute(toSubstitute); 26 i--; 27 } else if (ch == '}' && !ignore) { 28 count--; 29 } else if (ch == '\'') { 30 ignore = !ignore; 31 toRetutn += ch; 32 } else 33 toRetutn += ch; 34 } 35 } catch (StringIndexOutOfBoundsException e) {} 36 finally { 37 if (count != 0){ 38 ; } 40 } 41 return toRetutn; 42 } 43 44 static String result = ""; 45 46 static String parse1(String toParse) { 47 int openingBracePosition = toParse.indexOf("{"); 48 int closingBracePosition = toParse.indexOf("}"); 49 50 if (openingBracePosition == -1 && closingBracePosition == -1) 51 return result; 52 53 String toAdd = ""; 54 int pointer = closingBracePosition; 55 int nextPointer = pointer + 1; 56 if (openingBracePosition != -1 && openingBracePosition < closingBracePosition) { 57 pointer = openingBracePosition; 58 59 while (toParse.charAt(++openingBracePosition) == ' ') 60 ; 61 62 nextPointer = toParse.indexOf(" ", openingBracePosition); 63 64 String toRemove = toParse.substring(openingBracePosition, nextPointer); 65 toAdd = getSubstitute(toRemove); 66 } 67 68 result += (toParse.substring(0, pointer) + toAdd); 69 return parse1(toParse.substring(nextPointer)); 70 } 71 72 static String getSubstitute(String str) { 73 String toReturn = ""; 74 if (str.equalsIgnoreCase("t")) 75 return "TIME"; 76 if (str.equalsIgnoreCase("ts")) 77 return "TIMESTAMP"; 78 if (str.equalsIgnoreCase("d")) 79 return "DATE"; 80 if (str.equalsIgnoreCase("fn") || str.equalsIgnoreCase("oj")) 81 return ""; 82 return str; 83 } 84 118 119 } 120 | Popular Tags |