1 56 package org.objectstyle.cayenne.tools; 57 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.StringTokenizer ; 62 63 import org.apache.oro.text.perl.Perl5Util; 64 import org.apache.tools.ant.Project; 65 import org.apache.tools.ant.Task; 66 import org.objectstyle.cayenne.util.CayenneMapEntry; 67 68 74 public class NamePatternMatcher { 75 76 protected Task parentTask; 77 private static final Perl5Util regexUtil = new Perl5Util(); 78 private static final String [] emptyArray = new String [0]; 79 80 protected String [] itemIncludeFilters; 81 protected String [] itemExcludeFilters; 82 83 public NamePatternMatcher(Task parentTask, String includePattern, 84 String excludePattern) { 85 this.parentTask = parentTask; 86 this.itemIncludeFilters = tokenizePattern(includePattern); 87 this.itemExcludeFilters = tokenizePattern(excludePattern); 88 } 89 90 100 public String [] tokenizePattern(String pattern) { 101 if (pattern != null && pattern.length() > 0) { 102 StringTokenizer toks = new StringTokenizer (pattern, ","); 103 104 int len = toks.countTokens(); 105 if (len == 0) { 106 return emptyArray; 107 } 108 109 List patterns = new ArrayList (len); 110 for (int i = 0; i < len; i++) { 111 String nextPattern = toks.nextToken(); 112 StringBuffer buffer = new StringBuffer (); 113 114 buffer.append("/^"); 118 for (int j = 0; j < nextPattern.length(); j++) { 119 char nextChar = nextPattern.charAt(j); 120 if (nextChar == '*' || nextChar == '?') { 121 buffer.append('.'); 122 } 123 buffer.append(nextChar); 124 } 125 buffer.append("$/"); 126 127 String finalPattern = buffer.toString(); 128 129 try { 131 regexUtil.match(finalPattern, "abc_123"); 132 } 133 catch (Exception e) { 134 parentTask.log("Ignoring invalid pattern [" 135 + nextPattern 136 + "], reason: " 137 + e.getMessage(), Project.MSG_WARN); 138 continue; 139 } 140 141 patterns.add(finalPattern); 142 } 143 144 return (String []) patterns.toArray(new String [patterns.size()]); 145 } 146 else { 147 return emptyArray; 148 } 149 } 150 151 155 protected List filter(List items) { 156 if (items == null || items.isEmpty()) { 157 return items; 158 } 159 160 if ((itemIncludeFilters.length == 0) && (itemExcludeFilters.length == 0)) { 161 return items; 162 } 163 164 Iterator it = items.iterator(); 165 while (it.hasNext()) { 166 CayenneMapEntry entity = (CayenneMapEntry) it.next(); 167 168 if (!passedIncludeFilter(entity)) { 169 it.remove(); 170 continue; 171 } 172 173 if (!passedExcludeFilter(entity)) { 174 it.remove(); 175 continue; 176 } 177 } 178 179 return items; 180 } 181 182 186 protected boolean passedIncludeFilter(CayenneMapEntry item) { 187 if (itemIncludeFilters.length == 0) { 188 return true; 189 } 190 191 String itemName = item.getName(); 192 for (int i = 0; i < itemIncludeFilters.length; i++) { 193 if (regexUtil.match(itemIncludeFilters[i], itemName)) { 194 return true; 195 } 196 } 197 198 return false; 199 } 200 201 205 protected boolean passedExcludeFilter(CayenneMapEntry item) { 206 if (itemExcludeFilters.length == 0) { 207 return true; 208 } 209 210 String itemName = item.getName(); 211 for (int i = 0; i < itemExcludeFilters.length; i++) { 212 if (regexUtil.match(itemExcludeFilters[i], itemName)) { 213 return false; 214 } 215 } 216 217 return true; 218 } 219 220 public static String replaceWildcardInStringWithString( 221 String wildcard, 222 String pattern, 223 String replacement) { 224 if (null == pattern || null == wildcard) 225 return pattern; 226 227 StringBuffer buffer = new StringBuffer (); 228 int lastPos = 0; 229 int wildCardPos = pattern.indexOf(wildcard); 230 while (-1 != wildCardPos) { 231 if (lastPos != wildCardPos) { 232 buffer.append(pattern.substring(lastPos, wildCardPos)); 233 } 234 buffer.append(replacement); 235 lastPos += wildCardPos + wildcard.length(); 236 wildCardPos = pattern.indexOf(wildcard, lastPos); 237 } 238 239 if (lastPos < pattern.length()) { 240 buffer.append(pattern.substring(lastPos)); 241 } 242 243 return buffer.toString(); 244 } 245 } | Popular Tags |