1 19 20 package org.apache.cayenne.tools; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.StringTokenizer ; 26 import java.util.regex.Pattern ; 27 import java.util.regex.PatternSyntaxException ; 28 29 import org.apache.cayenne.util.CayenneMapEntry; 30 31 37 public class NamePatternMatcher { 38 39 protected ILog logger; 40 41 protected Pattern [] itemIncludeFilters; 42 protected Pattern [] itemExcludeFilters; 43 44 public NamePatternMatcher(ILog parentTask, String includePattern, 45 String excludePattern) { 46 this.logger = parentTask; 47 this.itemIncludeFilters = createPatterns(includePattern); 48 this.itemExcludeFilters = createPatterns(excludePattern); 49 } 50 51 60 public Pattern [] createPatterns(String patternString) { 61 String [] patternStrings = tokenizePattern(patternString); 62 List patterns = new ArrayList (patternStrings.length); 63 64 for (int i = 0; i < patternStrings.length; i++) { 65 66 try { 68 patterns.add(Pattern.compile(patternStrings[i])); 69 } 70 catch (PatternSyntaxException e) { 71 72 if (logger != null) { 73 logger.log("Ignoring invalid pattern [" 74 + patternStrings[i] 75 + "], reason: " 76 + e.getMessage(), ILog.MSG_WARN); 77 } 78 continue; 79 } 80 } 81 82 return (Pattern []) patterns.toArray(new Pattern [patterns.size()]); 83 } 84 85 94 public String [] tokenizePattern(String pattern) { 95 if (pattern != null && pattern.length() > 0) { 96 StringTokenizer toks = new StringTokenizer (pattern, ","); 97 98 int len = toks.countTokens(); 99 if (len == 0) { 100 return new String [0]; 101 } 102 103 List patterns = new ArrayList (len); 104 for (int i = 0; i < len; i++) { 105 String nextPattern = toks.nextToken(); 106 StringBuffer buffer = new StringBuffer (); 107 108 buffer.append("^"); 112 for (int j = 0; j < nextPattern.length(); j++) { 113 char nextChar = nextPattern.charAt(j); 114 if (nextChar == '*' || nextChar == '?') { 115 buffer.append('.'); 116 } 117 buffer.append(nextChar); 118 } 119 buffer.append("$"); 120 patterns.add(buffer.toString()); 121 } 122 123 return (String []) patterns.toArray(new String [patterns.size()]); 124 } 125 else { 126 return new String [0]; 127 } 128 } 129 130 134 protected List filter(List items) { 135 if (items == null || items.isEmpty()) { 136 return items; 137 } 138 139 if ((itemIncludeFilters.length == 0) && (itemExcludeFilters.length == 0)) { 140 return items; 141 } 142 143 Iterator it = items.iterator(); 144 while (it.hasNext()) { 145 CayenneMapEntry entity = (CayenneMapEntry) it.next(); 146 147 if (!passedIncludeFilter(entity)) { 148 it.remove(); 149 continue; 150 } 151 152 if (!passedExcludeFilter(entity)) { 153 it.remove(); 154 continue; 155 } 156 } 157 158 return items; 159 } 160 161 165 protected boolean passedIncludeFilter(CayenneMapEntry item) { 166 if (itemIncludeFilters.length == 0) { 167 return true; 168 } 169 170 String itemName = item.getName(); 171 for (int i = 0; i < itemIncludeFilters.length; i++) { 172 if (itemIncludeFilters[i].matcher(itemName).find()) { 173 return true; 174 } 175 } 176 177 return false; 178 } 179 180 184 protected boolean passedExcludeFilter(CayenneMapEntry item) { 185 if (itemExcludeFilters.length == 0) { 186 return true; 187 } 188 189 String itemName = item.getName(); 190 for (int i = 0; i < itemExcludeFilters.length; i++) { 191 if (itemExcludeFilters[i].matcher(itemName).find()) { 192 return false; 193 } 194 } 195 196 return true; 197 } 198 199 public static String replaceWildcardInStringWithString( 200 String wildcard, 201 String pattern, 202 String replacement) { 203 if (null == pattern || null == wildcard) 204 return pattern; 205 206 StringBuffer buffer = new StringBuffer (); 207 int lastPos = 0; 208 int wildCardPos = pattern.indexOf(wildcard); 209 while (-1 != wildCardPos) { 210 if (lastPos != wildCardPos) { 211 buffer.append(pattern.substring(lastPos, wildCardPos)); 212 } 213 buffer.append(replacement); 214 lastPos += wildCardPos + wildcard.length(); 215 wildCardPos = pattern.indexOf(wildcard, lastPos); 216 } 217 218 if (lastPos < pattern.length()) { 219 buffer.append(pattern.substring(lastPos)); 220 } 221 222 return buffer.toString(); 223 } 224 } 225 | Popular Tags |