1 23 package org.hammurapi.inspectors.filters; 24 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.Stack ; 28 29 import org.apache.oro.text.GlobCompiler; 30 import org.apache.oro.text.regex.MalformedPatternException; 31 import org.apache.oro.text.regex.Pattern; 32 import org.apache.oro.text.regex.Perl5Matcher; 33 import org.hammurapi.InspectorBase; 34 import org.hammurapi.FilteringInspector; 35 36 import com.pavelvlasov.config.ConfigurationException; 37 import com.pavelvlasov.config.Parameterizable; 38 import com.pavelvlasov.jsel.CompilationUnit; 39 import com.pavelvlasov.jsel.JselException; 40 import com.pavelvlasov.jsel.Operation; 41 import com.pavelvlasov.jsel.Package; 42 import com.pavelvlasov.jsel.TypeBody; 43 import com.pavelvlasov.jsel.TypeDefinition; 44 45 50 public class LanguageElementFilter extends InspectorBase implements 51 FilteringInspector, Parameterizable { 52 53 private Stack approvals=new Stack (); 54 55 { 56 approvals.push(Boolean.TRUE); 58 } 59 60 private static class ElementEntry { 61 boolean exclude; 62 String name; 63 String operationSignature; 64 Pattern fileNamePattern; 65 } 66 67 private LinkedList elements=new LinkedList (); 68 69 public Boolean approve(Object element) { 70 return (Boolean ) approvals.peek(); 71 } 72 73 private Perl5Matcher matcher=new Perl5Matcher(); 74 75 public void visit(CompilationUnit compilationUnit) { 76 Package pkg=compilationUnit.getPackage(); 77 boolean decision=((Boolean ) approvals.peek()).booleanValue(); 79 80 Iterator it=elements.iterator(); 81 while (it.hasNext()) { 82 ElementEntry entry=(ElementEntry) it.next(); 83 if (entry.name!=null && entry.operationSignature==null && 84 ("*".equals(entry.name) 85 || pkg.getName().equals(entry.name) 86 || (entry.name.endsWith(".*") && pkg.getName().startsWith(entry.name.substring(0, entry.name.length()-2))))) { 87 decision = !entry.exclude; 88 break; 89 } 90 } 91 approvals.push(decision ? Boolean.TRUE : Boolean.FALSE); 92 93 94 decision=((Boolean ) approvals.peek()).booleanValue(); 96 97 it=elements.iterator(); 98 while (it.hasNext()) { 99 ElementEntry entry=(ElementEntry) it.next(); 100 if (entry.fileNamePattern!=null && matcher.matches(compilationUnit.getName(), entry.fileNamePattern)) { 101 decision = !entry.exclude; 102 break; 103 } 104 } 105 approvals.push(decision ? Boolean.TRUE : Boolean.FALSE); 106 } 107 108 public void leave(CompilationUnit compilaitonUnit) { 109 approvals.pop(); 110 approvals.pop(); 111 } 112 113 117 public void visit(TypeBody typeBody) { 118 boolean decision=((Boolean ) approvals.peek()).booleanValue(); 120 121 if (decision) { 123 Iterator it=elements.iterator(); 124 while (it.hasNext()) { 125 ElementEntry entry=(ElementEntry) it.next(); 126 try { 127 if (entry.name!=null && entry.operationSignature==null && typeBody.isKindOf(entry.name)) { 128 decision = !entry.exclude; 129 break; 130 } 131 } catch (JselException e) { 132 if (!(e.getCause() instanceof ClassNotFoundException )) { 133 context.warn(typeBody, e); 134 break; 135 } 136 } 137 } 138 } 139 140 approvals.push(decision ? Boolean.TRUE : Boolean.FALSE); 141 } 142 143 public void leave(TypeDefinition typeDefinition) { 144 approvals.pop(); 145 } 146 147 public void visit(Operation operation) { 148 boolean decision=((Boolean ) approvals.peek()).booleanValue(); 150 151 if (decision) { 153 Iterator it=elements.iterator(); 154 while (it.hasNext()) { 155 ElementEntry entry=(ElementEntry) it.next(); 156 try { 157 if (entry.name!=null && entry.operationSignature!=null && ("*".equals(entry.name) || operation.getEnclosingType().isKindOf(entry.name)) && operation.getInfo().isArgumentCompatible(entry.operationSignature)) { 158 decision = !entry.exclude; 159 break; 160 } 161 } catch (JselException e) { 162 context.warn(operation, e); 163 break; 164 } 165 } 166 } 167 168 approvals.push(decision ? Boolean.TRUE : Boolean.FALSE); 169 } 170 171 public void leave(Operation operation) { 172 approvals.pop(); 173 } 174 175 209 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 210 if ("include".equals(name)) { 211 ElementEntry entry=new ElementEntry(); 212 entry.exclude=false; 213 parseParameter(parameter, entry); 214 elements.addFirst(entry); 215 } else if ("exclude".equals(name)) { 216 ElementEntry entry=new ElementEntry(); 217 entry.exclude=true; 218 parseParameter(parameter, entry); 219 elements.addFirst(entry); 220 } else if ("include-file".equals(name)) { 221 GlobCompiler compiler=new GlobCompiler(); 222 ElementEntry entry=new ElementEntry(); 223 entry.exclude=false; 224 try { 225 entry.fileNamePattern=compiler.compile((String ) parameter); 226 } catch (MalformedPatternException e) { 227 throw new ConfigurationException(e); 228 } 229 elements.addFirst(entry); 230 } else if ("exclude-file".equals(name)) { 231 GlobCompiler compiler=new GlobCompiler(); 232 ElementEntry entry=new ElementEntry(); 233 entry.exclude=true; 234 try { 235 entry.fileNamePattern=compiler.compile((String ) parameter); 236 } catch (MalformedPatternException e) { 237 throw new ConfigurationException(e); 238 } 239 elements.addFirst(entry); 240 } else { 241 throw new ConfigurationException("Not supported parameter: "+name); 242 } 243 return true; 244 } 245 246 250 private static void parseParameter(Object parameter, ElementEntry entry) { 251 String value = (String ) parameter; 252 int pi = value.indexOf('('); 253 if (pi==-1) { 254 entry.name=value; 255 } else { 256 int lastDot=value.lastIndexOf('.', pi); 257 if (lastDot==-1) { 258 entry.name="*"; 259 entry.operationSignature=value; 260 } else { 261 entry.name=value.substring(0, lastDot); 262 entry.operationSignature=value.substring(lastDot+1); 263 } 264 } 265 } 266 } 267 | Popular Tags |