1 23 package org.hammurapi; 24 25 import java.lang.reflect.InvocationHandler ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Proxy ; 28 import java.text.MessageFormat ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import com.pavelvlasov.jsel.CompilationUnit; 35 import com.pavelvlasov.jsel.Package; 36 import com.pavelvlasov.logging.Logger; 37 import com.pavelvlasov.review.SourceMarker; 38 import com.pavelvlasov.util.VisitorStack; 39 import com.pavelvlasov.util.VisitorStackSource; 40 import com.pavelvlasov.wrap.WrapperHandler; 41 42 46 public abstract class InspectorContextBase implements InspectorContext { 47 protected InspectorDescriptor descriptor; 48 public final static String GENERIC_MESSAGE="SimpleViolation. Message not found"; 49 protected Logger logger; 50 private VisitorStackSource visitorStackSource; 51 private SessionImpl session; 52 53 public InspectorDescriptor getDescriptor() { 54 return descriptor; 55 } 56 57 61 public void reportViolation(SourceMarker source) { 62 reportViolation(source, descriptor.getMessage()); 63 } 64 65 69 public void reportViolationEx(SourceMarker source, String messageKey) { 70 reportViolation(source, descriptor.getMessage(messageKey)); 71 } 72 73 76 public SourceMarker detach(final SourceMarker source) { 77 if (source==null) { 78 return null; 79 } 80 81 Class sourceClass=source.getClass(); 82 final String [] sourceUrl = {source.getSourceURL()}; 84 List stack = getVisitorStack().getStack(CompilationUnit.class); 85 if (!stack.isEmpty()) { 86 CompilationUnit cu=(CompilationUnit) stack.get(0); 87 Package pkg=cu.getPackage(); 88 if (pkg.getName().length()==0) { 89 sourceUrl[0]=cu.getName(); 90 } else { 91 sourceUrl[0]=pkg.getName().replace('.', '/')+'/'+cu.getName(); 92 } 93 } 94 95 return (SourceMarker) Proxy.newProxyInstance( 96 sourceClass.getClassLoader(), 97 WrapperHandler.getClassInterfaces(sourceClass), 98 new InvocationHandler () { 99 100 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 101 if (SourceMarker.class.getMethod("getSourceURL", null).equals(method)) { 102 return sourceUrl[0]; 103 } 104 105 return method.invoke(source, args); 106 } 107 108 }); 109 } 110 111 116 public void reportViolation(SourceMarker source, Object [] params) { 117 String message = descriptor.getMessage(); 118 if (message==null) { 119 warn(source, "Message not found for inspector "+getDescriptor().getName()); 120 message=GENERIC_MESSAGE; 121 } 122 reportViolation(source, MessageFormat.format(message, params)); 123 } 124 125 130 public void reportViolationEx(SourceMarker source, Object [] params, String messageKey) { 131 String message = descriptor.getMessage(messageKey); 132 if (message==null) { 133 warn(source, "Message with key '"+messageKey+"' not found for inspector "+getDescriptor().getName()); 134 message=GENERIC_MESSAGE; 135 } 136 reportViolation(source, MessageFormat.format(message+" for key '"+messageKey+"'", params)); 137 } 138 139 143 public InspectorContextBase( 144 InspectorDescriptor descriptor, 145 Logger logger, 146 VisitorStackSource visitorStackSource, SessionImpl session) { 147 super(); 148 this.descriptor = descriptor; 149 this.logger=logger; 150 this.visitorStackSource=visitorStackSource; 151 this.session=session; 152 if (session!=null) { 153 session.addContext(descriptor.getName(), this); 154 } 155 } 156 157 162 public void info(SourceMarker source, String message) { 163 if (logger!=null) { 164 logger.info(this, loggerMessage(source, message)); 165 } 166 } 167 168 173 public void debug(SourceMarker source, String message) { 174 if (logger!=null) { 175 logger.debug(this, loggerMessage(source, message)); 176 } 177 } 178 179 184 public void verbose(SourceMarker source, String message) { 185 if (logger!=null) { 186 logger.verbose(this, loggerMessage(source, message)); 187 } 188 } 189 190 195 private String loggerMessage(SourceMarker source, String message) { 196 return descriptor.getName()+" "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" - "+message; 197 } 198 199 protected static final Date date = new Date (); 200 201 public VisitorStack getVisitorStack() { 202 return visitorStackSource==null ? null : visitorStackSource.getVisitorStack(); 203 } 204 205 private Map attributes=new HashMap (); 206 207 public void setAttribute(Object key, Object value) { 208 attributes.put(key, value); 209 } 210 211 public Object getAttribute(Object key) { 212 return attributes.get(key); 213 } 214 215 public Object removeAttribute(Object key) { 216 return attributes.remove(key); 217 } 218 219 public Session getSession() { 220 return session; 221 } 222 } 223 | Popular Tags |