001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.lang.reflect.InvocationHandler;
026: import java.lang.reflect.Method;
027: import java.lang.reflect.Proxy;
028: import java.text.MessageFormat;
029: import java.util.Date;
030: import java.util.HashMap;
031: import java.util.List;
032: import java.util.Map;
033:
034: import com.pavelvlasov.jsel.CompilationUnit;
035: import com.pavelvlasov.jsel.Package;
036: import com.pavelvlasov.logging.Logger;
037: import com.pavelvlasov.review.SourceMarker;
038: import com.pavelvlasov.util.VisitorStack;
039: import com.pavelvlasov.util.VisitorStackSource;
040: import com.pavelvlasov.wrap.WrapperHandler;
041:
042: /**
043: * @author Pavel Vlasov
044: * @version $Revision: 1.7 $
045: */
046: public abstract class InspectorContextBase implements InspectorContext {
047: protected InspectorDescriptor descriptor;
048: public final static String GENERIC_MESSAGE = "SimpleViolation. Message not found";
049: protected Logger logger;
050: private VisitorStackSource visitorStackSource;
051: private SessionImpl session;
052:
053: public InspectorDescriptor getDescriptor() {
054: return descriptor;
055: }
056:
057: /**
058: * Reports violation with a message from descriptor
059: * @param source
060: */
061: public void reportViolation(SourceMarker source) {
062: reportViolation(source, descriptor.getMessage());
063: }
064:
065: /**
066: * Reports violation with a message from descriptor
067: * @param source
068: */
069: public void reportViolationEx(SourceMarker source, String messageKey) {
070: reportViolation(source, descriptor.getMessage(messageKey));
071: }
072:
073: /**
074: * @param source
075: */
076: public SourceMarker detach(final SourceMarker source) {
077: if (source == null) {
078: return null;
079: }
080:
081: Class sourceClass = source.getClass();
082: // TODO - real detach through findBySignature
083: final String[] sourceUrl = { source.getSourceURL() };
084: List stack = getVisitorStack().getStack(CompilationUnit.class);
085: if (!stack.isEmpty()) {
086: CompilationUnit cu = (CompilationUnit) stack.get(0);
087: Package pkg = cu.getPackage();
088: if (pkg.getName().length() == 0) {
089: sourceUrl[0] = cu.getName();
090: } else {
091: sourceUrl[0] = pkg.getName().replace('.', '/') + '/'
092: + cu.getName();
093: }
094: }
095:
096: return (SourceMarker) Proxy.newProxyInstance(sourceClass
097: .getClassLoader(), WrapperHandler
098: .getClassInterfaces(sourceClass),
099: new InvocationHandler() {
100:
101: public Object invoke(Object proxy, Method method,
102: Object[] args) throws Throwable {
103: if (SourceMarker.class.getMethod(
104: "getSourceURL", null).equals(method)) {
105: return sourceUrl[0];
106: }
107:
108: return method.invoke(source, args);
109: }
110:
111: });
112: }
113:
114: /**
115: * Formats message taken from InspectorDescriptor with parameters.
116: * @param source
117: * @param params
118: */
119: public void reportViolation(SourceMarker source, Object[] params) {
120: String message = descriptor.getMessage();
121: if (message == null) {
122: warn(source, "Message not found for inspector "
123: + getDescriptor().getName());
124: message = GENERIC_MESSAGE;
125: }
126: reportViolation(source, MessageFormat.format(message, params));
127: }
128:
129: /**
130: * Formats message taken from InspectorDescriptor with parameters.
131: * @param source
132: * @param params
133: */
134: public void reportViolationEx(SourceMarker source, Object[] params,
135: String messageKey) {
136: String message = descriptor.getMessage(messageKey);
137: if (message == null) {
138: warn(source, "Message with key '" + messageKey
139: + "' not found for inspector "
140: + getDescriptor().getName());
141: message = GENERIC_MESSAGE;
142: }
143: reportViolation(source, MessageFormat.format(message
144: + " for key '" + messageKey + "'", params));
145: }
146:
147: /**
148: * @param descriptor
149: * @param session
150: */
151: public InspectorContextBase(InspectorDescriptor descriptor,
152: Logger logger, VisitorStackSource visitorStackSource,
153: SessionImpl session) {
154: super ();
155: this .descriptor = descriptor;
156: this .logger = logger;
157: this .visitorStackSource = visitorStackSource;
158: this .session = session;
159: if (session != null) {
160: session.addContext(descriptor.getName(), this );
161: }
162: }
163:
164: /**
165: * Outputs a message to the log
166: * @param source
167: * @param message
168: */
169: public void info(SourceMarker source, String message) {
170: if (logger != null) {
171: logger.info(this , loggerMessage(source, message));
172: }
173: }
174:
175: /**
176: * Outputs a message to the log
177: * @param source
178: * @param message
179: */
180: public void debug(SourceMarker source, String message) {
181: if (logger != null) {
182: logger.debug(this , loggerMessage(source, message));
183: }
184: }
185:
186: /**
187: * Outputs a message to the log
188: * @param source
189: * @param message
190: */
191: public void verbose(SourceMarker source, String message) {
192: if (logger != null) {
193: logger.verbose(this , loggerMessage(source, message));
194: }
195: }
196:
197: /**
198: * @param source
199: * @param message
200: * @return formatted message
201: */
202: private String loggerMessage(SourceMarker source, String message) {
203: return descriptor.getName() + " " + source.getSourceURL() + " "
204: + source.getLine() + ":" + source.getColumn() + " - "
205: + message;
206: }
207:
208: protected static final Date date = new Date();
209:
210: public VisitorStack getVisitorStack() {
211: return visitorStackSource == null ? null : visitorStackSource
212: .getVisitorStack();
213: }
214:
215: private Map attributes = new HashMap();
216:
217: public void setAttribute(Object key, Object value) {
218: attributes.put(key, value);
219: }
220:
221: public Object getAttribute(Object key) {
222: return attributes.get(key);
223: }
224:
225: public Object removeAttribute(Object key) {
226: return attributes.remove(key);
227: }
228:
229: public Session getSession() {
230: return session;
231: }
232: }
|