KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > SimpleAnnotationSample


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
24 package org.hammurapi.inspectors;
25
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import org.hammurapi.InspectorBase;
32 import org.hammurapi.HammurapiException;
33 import org.hammurapi.results.AnnotationContext;
34 import org.hammurapi.results.InlineAnnotation;
35 import org.hammurapi.results.LinkedAnnotation;
36
37 import com.pavelvlasov.config.ConfigurationException;
38 import com.pavelvlasov.jsel.CompilationUnit;
39
40 /**
41  * This class demostrates how to:
42  * Generate annotations with multiple files
43  * Use renderers and embedded styles
44  * Read inspector parameters
45  * Read annotation config parameters
46  * getConfigInfo implementation
47  *
48  * @author Pavel Vlasov
49  * @version $Revision: 1.2 $
50  */

51 public class SimpleAnnotationSample extends InspectorBase {
52     
53     public void visit(final CompilationUnit compilationUnit) {
54         context.annotate(new LinkedAnnotation() {
55             private String JavaDoc path;
56             private String JavaDoc cuPath=compilationUnit.getRelativeName();
57
58             public String JavaDoc getName() {
59                 return getContext().getDescriptor().getName();
60             }
61
62             public String JavaDoc getPath() {
63                 return path;
64             }
65
66             public void render(final AnnotationContext context) throws HammurapiException {
67                 final AnnotationContext.FileEntry linkEntry=context.getNextFile(".txt");
68
69                 try {
70                     Writer JavaDoc out=new FileWriter JavaDoc(linkEntry.getFile());
71                     try {
72                         out.write("Hello, world!!!");
73                     } finally {
74                         out.close();
75                     }
76                 } catch (IOException JavaDoc e) {
77                     throw new HammurapiException("Cannot save "+linkEntry.getFile().getAbsolutePath(), e);
78                 }
79                                                 
80                 AnnotationContext.FileEntry fileEntry=context.getNextFile(context.getExtension());
81                 path=fileEntry.getPath(); // This file is the entry point to the annotation.
82
try {
83                     Writer JavaDoc out=new FileWriter JavaDoc(fileEntry.getFile());
84                     try {
85                         out.write("<HTML><BODY><H1>My simple annotation</H1>"+
86                                 "PI: "+pi+
87                                 "<P><a HREF=\""+linkEntry.getPath()+"\">Greeting</a>" +
88                                 "</BODY></HTML>");
89                     } finally {
90                         out.close();
91                     }
92                 } catch (IOException JavaDoc e) {
93                     throw new HammurapiException("Cannot save "+linkEntry.getFile().getAbsolutePath(), e);
94                 }
95             }
96
97             public Properties JavaDoc getProperties() {
98                 return null;
99             }
100         });
101         
102         context.annotate(new InlineAnnotation() {
103
104             public String JavaDoc getContent() {
105                 return "<DIV style=\"border:solid; background-color:yellow\">Simple annotation sample</DIV>";
106             }
107
108             public String JavaDoc getName() {
109                 return getContext().getDescriptor().getName();
110             }
111
112             public void render(AnnotationContext context) throws HammurapiException {
113                 // This annotation doesn't create any additional files, so this method is empty
114

115             }
116
117             public Properties JavaDoc getProperties() {
118                 return null;
119             }
120             
121         });
122     }
123
124     private Double JavaDoc pi;
125         
126     public void setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
127         if ("pi".equals(name)) {
128             pi=(Double JavaDoc) parameter;
129         } else {
130             throw new ConfigurationException("Parameter "+name+" is not supported");
131         }
132     }
133     
134     public String JavaDoc getConfigInfo() {
135         return "PI="+pi;
136     }
137
138 }
139
Popular Tags