1 23 24 package org.hammurapi.inspectors; 25 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.util.Properties ; 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 ; 38 import org.w3c.dom.Element ; 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 58 public class AnnotationTest extends InspectorBase implements Parameterizable { 59 private static final String BEAN_IMAGE = "AnnotationTest.jpg"; 60 61 public void visit(final CompilationUnit compilationUnit) { 62 context.annotate(new LinkedAnnotation() { 63 private String path; 64 public String getName() { 65 return getContext().getDescriptor().getName(); 66 } 67 68 public String 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 in=getClass().getResourceAsStream(BEAN_IMAGE); 78 if (in==null) { 79 throw new HammurapiException("Resource not found "+BEAN_IMAGE); 80 } 81 82 try { 83 OutputStream out=new FileOutputStream (imageEntry.getFile()); 84 try { 85 in.read(buf); 88 out.write(buf, 0, 2342); 89 } finally { 90 out.close(); 91 } 92 } finally { 93 in.close(); 94 } 95 } catch (IOException 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 render(Document document) { 105 Element annotationElement=document.createElement("annotation-test"); 106 String title=(String ) context.getParameter("title"); 107 if (title!=null) { 108 Element 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(); try { 126 AnnotationTestRenderer renderer=new AnnotationTestRenderer(); 127 FileOutputStream out=new FileOutputStream (fileEntry.getFile()); 128 renderer.setEmbeddedStyle(context.isEmbeddedStyle()); 129 try { 130 renderer.render(context.getParameter("style")==null ? null : new FileInputStream (context.getParameter("style").toString()), out); 131 } finally { 132 out.close(); 133 } 134 } catch (IOException 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 getProperties() { 142 return null; 143 } 144 }); 145 } 146 147 private Double pi; 148 149 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 150 if ("pi".equals(name)) { 151 pi=(Double ) parameter; 152 return true; 153 } 154 throw new ConfigurationException("Parameter "+name+" is not supported"); 155 } 156 157 public String getConfigInfo() { 158 return "PI="+pi; 159 } 160 161 } 162 | Popular Tags |