KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.hammurapi.InspectorBase;
34 import org.hammurapi.HammurapiException;
35 import org.hammurapi.results.AnnotationContext;
36 import org.hammurapi.results.LinkedAnnotation;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39
40 import com.pavelvlasov.config.ConfigurationException;
41 import com.pavelvlasov.config.Parameterizable;
42 import com.pavelvlasov.jsel.CompilationUnit;
43 import com.pavelvlasov.render.RenderRequest;
44 import com.pavelvlasov.render.RenderingException;
45 import com.pavelvlasov.render.dom.AbstractRenderer;
46
47 /**
48  * This class demostrates how to:
49  * Generate annotations with multiple files
50  * Use renderers and embedded styles
51  * Read inspector parameters
52  * Read annotation config parameters
53  * getConfigInfo implementation
54  *
55  * @author Pavel Vlasov
56  * @version $Revision: 1.5 $
57  */

58 public class AnnotationTest extends InspectorBase implements Parameterizable {
59     private static final String JavaDoc BEAN_IMAGE = "AnnotationTest.jpg";
60     
61     public void visit(final CompilationUnit compilationUnit) {
62         context.annotate(new LinkedAnnotation() {
63             private String JavaDoc path;
64             public String JavaDoc getName() {
65                 return getContext().getDescriptor().getName();
66             }
67
68             public String JavaDoc getPath() {
69                 return path;
70             }
71
72             public void render(final AnnotationContext context) throws HammurapiException {
73                 final AnnotationContext.FileEntry imageEntry=context.getNextFile(".jpg");
74                 
75                 try {
76                     byte[] buf=new byte[4096];
77                     InputStream JavaDoc in=getClass().getResourceAsStream(BEAN_IMAGE);
78                     if (in==null) {
79                         throw new HammurapiException("Resource not found "+BEAN_IMAGE);
80                     }
81                     
82                     try {
83                         OutputStream JavaDoc out=new FileOutputStream JavaDoc(imageEntry.getFile());
84                         try {
85                             // Very stupid, but there is a problem with Ant classloader
86
// It puts additonal bytes at the end.
87
in.read(buf);
88                             out.write(buf, 0, 2342);
89                         } finally {
90                             out.close();
91                         }
92                     } finally {
93                         in.close();
94                     }
95                 } catch (IOException JavaDoc e) {
96                     throw new HammurapiException("Cannot save "+imageEntry.getFile().getAbsolutePath(), e);
97                 }
98                 
99                 class AnnotationTestRenderer extends AbstractRenderer {
100                     AnnotationTestRenderer() {
101                         super(new RenderRequest(AnnotationTest.this));
102                     }
103                     
104                     public Element JavaDoc render(Document JavaDoc document) {
105                         Element JavaDoc annotationElement=document.createElement("annotation-test");
106                         String JavaDoc title=(String JavaDoc) context.getParameter("title");
107                         if (title!=null) {
108                             Element JavaDoc titleElement = document.createElement("title");
109                             annotationElement.appendChild(titleElement);
110                             titleElement.appendChild(document.createTextNode(title));
111                         }
112                         annotationElement.setAttribute("image", imageEntry.getPath());
113                         
114                         if (pi!=null) {
115                             annotationElement.setAttribute("pi", pi.toString());
116                         }
117                         
118                         return annotationElement;
119                     }
120                     
121                 }
122                                 
123                 AnnotationContext.FileEntry fileEntry=context.getNextFile(context.getExtension());
124                 path=fileEntry.getPath(); // This file is the entry point to the annotation.
125
try {
126                     AnnotationTestRenderer renderer=new AnnotationTestRenderer();
127                     FileOutputStream JavaDoc out=new FileOutputStream JavaDoc(fileEntry.getFile());
128                     renderer.setEmbeddedStyle(context.isEmbeddedStyle());
129                     try {
130                         renderer.render(context.getParameter("style")==null ? null : new FileInputStream JavaDoc(context.getParameter("style").toString()), out);
131                     } finally {
132                         out.close();
133                     }
134                 } catch (IOException JavaDoc e) {
135                     throw new HammurapiException("Can't save "+fileEntry.getFile().getAbsolutePath(), e);
136                 } catch (RenderingException e) {
137                     throw new HammurapiException("Can't render "+fileEntry.getFile().getAbsolutePath(), e);
138                 }
139             }
140
141             public Properties JavaDoc getProperties() {
142                 return null;
143             }
144         });
145     }
146
147     private Double JavaDoc pi;
148         
149     public boolean setParameter(String JavaDoc name, Object JavaDoc parameter) throws ConfigurationException {
150         if ("pi".equals(name)) {
151             pi=(Double JavaDoc) parameter;
152             return true;
153         }
154         throw new ConfigurationException("Parameter "+name+" is not supported");
155     }
156     
157     public String JavaDoc getConfigInfo() {
158         return "PI="+pi;
159     }
160
161 }
162
Popular Tags