1 15 package org.apache.hivemind.methodmatch; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.util.StringUtils; 22 23 31 32 public class MethodPatternParser 33 { 34 private List _filters; 35 36 public MethodFilter parseMethodPattern(String pattern) 37 { 38 _filters = new ArrayList (); 39 40 int parenx = pattern.indexOf('('); 41 42 String namePattern = parenx < 0 ? pattern : pattern.substring(0, parenx); 43 44 parseNamePattern(pattern, namePattern); 45 46 if (parenx >= 0) 47 parseParametersPattern(pattern, pattern.substring(parenx)); 48 49 switch (_filters.size()) 50 { 51 case 0: 52 return new MatchAllFilter(); 53 54 case 1: 55 56 return (MethodFilter) _filters.get(0); 57 58 default: 59 return new CompositeFilter(_filters); 60 } 61 } 62 63 private void parseNamePattern(String methodPattern, String namePattern) 64 { 65 if (namePattern.equals("*")) 66 return; 67 68 if (namePattern.length() == 0) 69 throw new ApplicationRuntimeException(MethodMatchMessages 70 .missingNamePattern(methodPattern)); 71 72 if (namePattern.startsWith("*") && namePattern.endsWith("*")) 73 { 74 String substring = namePattern.substring(1, namePattern.length() - 1); 75 76 validateNamePattern(methodPattern, substring); 77 78 _filters.add(new InfixNameFilter(substring)); 79 return; 80 } 81 82 if (namePattern.startsWith("*")) 83 { 84 String suffix = namePattern.substring(1); 85 86 validateNamePattern(methodPattern, suffix); 87 88 _filters.add(new NameSuffixFilter(suffix)); 89 return; 90 } 91 92 if (namePattern.endsWith("*")) 93 { 94 String prefix = namePattern.substring(0, namePattern.length() - 1); 95 96 validateNamePattern(methodPattern, prefix); 97 98 _filters.add(new NamePrefixFilter(prefix)); 99 return; 100 } 101 102 validateNamePattern(methodPattern, namePattern); 103 104 _filters.add(new ExactNameFilter(namePattern)); 105 } 106 107 private void parseParametersPattern(String methodPattern, String pattern) 108 { 109 if (pattern.equals("()")) 110 { 111 addParameterCountFilter(0); 112 return; 113 } 114 115 if (!pattern.endsWith(")")) 116 throw new ApplicationRuntimeException(MethodMatchMessages 117 .invalidParametersPattern(methodPattern)); 118 119 121 pattern = pattern.substring(1, pattern.length() - 1); 122 123 char ch = pattern.charAt(0); 124 125 if (Character.isDigit(ch)) 126 { 127 addParameterCountFilter(methodPattern, pattern); 128 return; 129 } 130 131 String [] names = StringUtils.split(pattern); 132 133 137 addParameterCountFilter(names.length); 138 for (int i = 0; i < names.length; i++) 139 _filters.add(new ParameterFilter(i, names[i].trim())); 140 141 } 142 143 private void addParameterCountFilter(String methodPattern, String pattern) 144 { 145 try 146 { 147 int count = Integer.parseInt(pattern); 148 addParameterCountFilter(count); 149 } 150 catch (NumberFormatException ex) 151 { 152 throw new ApplicationRuntimeException(MethodMatchMessages 153 .invalidParametersPattern(methodPattern)); 154 } 155 } 156 157 private void addParameterCountFilter(int count) 158 { 159 _filters.add(0, new ParameterCountFilter(count)); 161 } 162 163 private void validateNamePattern(String methodPattern, String nameSubstring) 164 { 165 if (nameSubstring.indexOf('*') >= 0) 166 throw new ApplicationRuntimeException(MethodMatchMessages 167 .invalidNamePattern(methodPattern)); 168 } 169 } | Popular Tags |