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:
024: package org.hammurapi.inspectors;
025:
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.util.Properties;
032:
033: import org.hammurapi.InspectorBase;
034: import org.hammurapi.HammurapiException;
035: import org.hammurapi.results.AnnotationContext;
036: import org.hammurapi.results.LinkedAnnotation;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039:
040: import com.pavelvlasov.config.ConfigurationException;
041: import com.pavelvlasov.config.Parameterizable;
042: import com.pavelvlasov.jsel.CompilationUnit;
043: import com.pavelvlasov.render.RenderRequest;
044: import com.pavelvlasov.render.RenderingException;
045: import com.pavelvlasov.render.dom.AbstractRenderer;
046:
047: /**
048: * This class demostrates how to:
049: * Generate annotations with multiple files
050: * Use renderers and embedded styles
051: * Read inspector parameters
052: * Read annotation config parameters
053: * getConfigInfo implementation
054: *
055: * @author Pavel Vlasov
056: * @version $Revision: 1.5 $
057: */
058: public class AnnotationTest extends InspectorBase implements
059: Parameterizable {
060: private static final String BEAN_IMAGE = "AnnotationTest.jpg";
061:
062: public void visit(final CompilationUnit compilationUnit) {
063: context.annotate(new LinkedAnnotation() {
064: private String path;
065:
066: public String getName() {
067: return getContext().getDescriptor().getName();
068: }
069:
070: public String getPath() {
071: return path;
072: }
073:
074: public void render(final AnnotationContext context)
075: throws HammurapiException {
076: final AnnotationContext.FileEntry imageEntry = context
077: .getNextFile(".jpg");
078:
079: try {
080: byte[] buf = new byte[4096];
081: InputStream in = getClass().getResourceAsStream(
082: BEAN_IMAGE);
083: if (in == null) {
084: throw new HammurapiException(
085: "Resource not found " + BEAN_IMAGE);
086: }
087:
088: try {
089: OutputStream out = new FileOutputStream(
090: imageEntry.getFile());
091: try {
092: // Very stupid, but there is a problem with Ant classloader
093: // It puts additonal bytes at the end.
094: in.read(buf);
095: out.write(buf, 0, 2342);
096: } finally {
097: out.close();
098: }
099: } finally {
100: in.close();
101: }
102: } catch (IOException e) {
103: throw new HammurapiException("Cannot save "
104: + imageEntry.getFile().getAbsolutePath(), e);
105: }
106:
107: class AnnotationTestRenderer extends AbstractRenderer {
108: AnnotationTestRenderer() {
109: super (new RenderRequest(AnnotationTest.this ));
110: }
111:
112: public Element render(Document document) {
113: Element annotationElement = document
114: .createElement("annotation-test");
115: String title = (String) context
116: .getParameter("title");
117: if (title != null) {
118: Element titleElement = document
119: .createElement("title");
120: annotationElement.appendChild(titleElement);
121: titleElement.appendChild(document
122: .createTextNode(title));
123: }
124: annotationElement.setAttribute("image",
125: imageEntry.getPath());
126:
127: if (pi != null) {
128: annotationElement.setAttribute("pi", pi
129: .toString());
130: }
131:
132: return annotationElement;
133: }
134:
135: }
136:
137: AnnotationContext.FileEntry fileEntry = context
138: .getNextFile(context.getExtension());
139: path = fileEntry.getPath(); // This file is the entry point to the annotation.
140: try {
141: AnnotationTestRenderer renderer = new AnnotationTestRenderer();
142: FileOutputStream out = new FileOutputStream(
143: fileEntry.getFile());
144: renderer
145: .setEmbeddedStyle(context.isEmbeddedStyle());
146: try {
147: renderer
148: .render(
149: context.getParameter("style") == null ? null
150: : new FileInputStream(
151: context
152: .getParameter(
153: "style")
154: .toString()),
155: out);
156: } finally {
157: out.close();
158: }
159: } catch (IOException e) {
160: throw new HammurapiException("Can't save "
161: + fileEntry.getFile().getAbsolutePath(), e);
162: } catch (RenderingException e) {
163: throw new HammurapiException("Can't render "
164: + fileEntry.getFile().getAbsolutePath(), e);
165: }
166: }
167:
168: public Properties getProperties() {
169: return null;
170: }
171: });
172: }
173:
174: private Double pi;
175:
176: public boolean setParameter(String name, Object parameter)
177: throws ConfigurationException {
178: if ("pi".equals(name)) {
179: pi = (Double) parameter;
180: return true;
181: }
182: throw new ConfigurationException("Parameter " + name
183: + " is not supported");
184: }
185:
186: public String getConfigInfo() {
187: return "PI=" + pi;
188: }
189:
190: }
|