KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > results > simple > ReparsingReviewResults


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.results.simple;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.lang.ref.Reference JavaDoc;
32 import java.lang.ref.SoftReference JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.hammurapi.HammurapiRuntimeException;
37 import org.hammurapi.WaiverSet;
38 import org.hammurapi.results.ReviewResults;
39
40 import com.pavelvlasov.jsel.CompilationUnit;
41 import com.pavelvlasov.jsel.JselException;
42 import com.pavelvlasov.jsel.impl.CompilationUnitImpl;
43 import com.pavelvlasov.logging.Logger;
44
45 /**
46  * @author Pavel Vlasov
47  * @version $Revision: 1.3 $
48  */

49 public class ReparsingReviewResults extends SimpleDetailedResults implements ReviewResults {
50     /**
51      * Comment for <code>serialVersionUID</code>
52      */

53     private static final long serialVersionUID = -349746437092882478L;
54     private String JavaDoc path;
55     
56     private static class Context {
57         private Reference JavaDoc ref;
58         private ClassLoader JavaDoc classLoader;
59         private Logger logger;
60     }
61     
62     private static ThreadLocal JavaDoc contextMap=new ThreadLocal JavaDoc() {
63         protected Object JavaDoc initialValue() {
64             return new HashMap JavaDoc();
65         }
66     };
67     private int tabSize;
68     private String JavaDoc encoding;
69     
70     ReparsingReviewResults(CompilationUnit cu, WaiverSet waiverSet, ClassLoader JavaDoc classLoader, int tabSize, Logger logger) {
71         super(cu.getName(), waiverSet);
72         path=cu.getFile().getAbsolutePath();
73         this.encoding=cu.getEncoding();
74         Context context=new Context();
75         context.ref=new SoftReference JavaDoc(cu);
76         context.classLoader=classLoader;
77         context.logger=logger;
78         ((Map JavaDoc) contextMap.get()).put(path, context);
79         this.tabSize=tabSize;
80     }
81     
82     public CompilationUnit getCompilationUnit() {
83         Context context=(Context) ((Map JavaDoc) contextMap.get()).get(path);
84         
85         CompilationUnit cu = (CompilationUnit) (context.ref==null ? null : context.ref.get());
86         if (cu==null) {
87             try {
88                 InputStream JavaDoc is=new FileInputStream JavaDoc(new File JavaDoc(path));
89                 Reader JavaDoc reader = encoding==null ? new InputStreamReader JavaDoc(is) : new InputStreamReader JavaDoc(is, encoding);
90                 try {
91                     cu=new CompilationUnitImpl(reader, encoding, null, path, tabSize, context.classLoader, context.logger);
92                 } finally {
93                     reader.close();
94                     is.close();
95                 }
96             } catch (JselException e) {
97                 throw new HammurapiRuntimeException(e);
98             } catch (IOException JavaDoc e) {
99                 throw new HammurapiRuntimeException(e);
100             }
101             context.ref=new SoftReference JavaDoc(cu);
102         }
103         return cu;
104     }
105 }
106
Popular Tags