1 19 20 package org.apache.cayenne.util; 21 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 25 31 class RegexUtil { 32 33 static final Pattern BACKSLASH = Pattern.compile("\\\\"); 34 static final Pattern DOT = Pattern.compile("\\."); 35 36 40 static String substBackslashes(String string) { 41 if (string == null) { 42 return null; 43 } 44 45 Matcher matcher = BACKSLASH.matcher(string); 46 return matcher.find() ? matcher.replaceAll("\\/") : string; 47 } 48 49 55 static String getPackagePath(String className) { 56 if (className == null) { 57 return ""; 58 } 59 60 Matcher matcher = DOT.matcher(className); 61 if (matcher.find()) { 62 String path = matcher.replaceAll("\\/"); 63 return path.substring(0, path.lastIndexOf("/")); 64 } 65 else { 66 return ""; 67 } 68 } 69 70 77 static String sqlPatternToRegex(String pattern) { 78 if (pattern == null) { 79 throw new NullPointerException ("Null pattern."); 80 } 81 82 if (pattern.length() == 0) { 83 throw new IllegalArgumentException ("Empty pattern."); 84 } 85 86 StringBuffer buffer = new StringBuffer (); 87 88 buffer.append("^"); 92 for (int j = 0; j < pattern.length(); j++) { 93 char nextChar = pattern.charAt(j); 94 if (nextChar == '%') { 95 nextChar = '*'; 96 } 97 98 if (nextChar == '*' || nextChar == '?') { 99 buffer.append('.'); 100 } 101 else if (nextChar == '.' 103 || nextChar == '/' 104 || nextChar == '$' 105 || nextChar == '^') { 106 buffer.append('\\'); 107 } 108 109 buffer.append(nextChar); 110 } 111 112 buffer.append("$"); 113 return buffer.toString(); 114 } 115 116 private RegexUtil() { 117 super(); 118 } 119 } 120 | Popular Tags |