1 9 package org.hammurapi.inspectors.metrics; 10 11 import java.io.FileInputStream ; 12 import java.io.FileOutputStream ; 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 20 import org.hammurapi.InspectorBase; 21 import org.hammurapi.HammurapiException; 22 import org.hammurapi.results.AnnotationContext; 23 import org.hammurapi.results.LinkedAnnotation; 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 27 import com.pavelvlasov.config.ConfigurationException; 28 import com.pavelvlasov.jsel.JselException; 29 import com.pavelvlasov.jsel.Repository; 30 import com.pavelvlasov.jsel.TypeDefinition; 31 import com.pavelvlasov.jsel.VariableDefinition; 32 import com.pavelvlasov.jsel.expressions.MethodCall; 33 import com.pavelvlasov.jsel.expressions.StringConstant; 34 import com.pavelvlasov.render.RenderRequest; 35 import com.pavelvlasov.render.RenderingException; 36 import com.pavelvlasov.render.dom.AbstractRenderer; 37 import com.pavelvlasov.review.SourceMarker; 38 43 public class JspInspector extends InspectorBase 44 implements 45 com.pavelvlasov.config.Parameterizable { 46 47 private Map jspList = new HashMap (); 48 private boolean isJavaScriptStream = false; 49 private JspDescriptor currentJspDescriptor = null; 50 private String jspBaseClass = ""; 51 52 public void visit(MethodCall methodCall) { 53 if (currentJspDescriptor == null) { 55 return; 56 } 57 if ("response.sendRedirect".equals(methodCall.getName().toString())) { 59 String referenceStr = methodCall.getParameters().get(0).toString(); 60 JspXref jXref = new JspXref(currentJspDescriptor, 61 "response.sendRedirect", referenceStr); 62 currentJspDescriptor.listOfInvokedJsp.add(jXref); 63 } 64 if ("pageContext.forward".equals(methodCall.getName().toString())) { 65 String referenceStr = methodCall.getParameters().get(0).toString(); 66 JspXref jXref = new JspXref(currentJspDescriptor, "JSP Forward", 67 referenceStr); 68 currentJspDescriptor.listOfInvokedJsp.add(jXref); 69 } 70 if ("out.print".equals(methodCall.getName().toString()) 71 || "out.println".equals(methodCall.getName().toString())) { 72 this.currentJspDescriptor.numberOfOutPrintOps++; 73 } else if ("out.write".equals(methodCall.getName().toString())) { 74 this.currentJspDescriptor.numberOfOutWriteOps++; 75 List parameterList = methodCall.getParameters(); 76 for (int i = 0; i < parameterList.size(); i++) { 77 Object param = parameterList.get(i); 78 if (param instanceof StringConstant) { 79 StringConstant strCnst = (StringConstant) param; 80 81 if (!isJavaScriptStream 82 && strCnst.toString().toLowerCase().indexOf( 83 "<script".toLowerCase()) > -1) { 84 isJavaScriptStream = true; 85 this.currentJspDescriptor.numberOfJavaScriptSnippets++; 86 87 currentJspDescriptor.analyseJavaScriptPortion(strCnst 88 .toString()); 89 } else if (isJavaScriptStream 90 && strCnst.toString().toLowerCase().indexOf( 91 "</script>".toLowerCase()) > -1) { 92 93 currentJspDescriptor.analyseJavaScriptPortion(strCnst 94 .toString()); 95 96 isJavaScriptStream = false; 97 } else if (isJavaScriptStream) { 98 99 currentJspDescriptor.analyseJavaScriptPortion(strCnst 100 .toString()); 101 } 102 if (!isJavaScriptStream 103 && strCnst.toString().toLowerCase().indexOf( 104 "<form".toLowerCase()) > -1) { 105 currentJspDescriptor.analyseHtmlForm(strCnst.toString()); 106 } 107 } 108 } 109 } 110 } 111 public void extractJavaScriptPortion(String jsStream) { 112 if (currentJspDescriptor != null) { 114 currentJspDescriptor.analyseJavaScriptPortion(jsStream); 115 } 116 } 117 public void visit(VariableDefinition element) { 118 } 120 public void visit(TypeDefinition typeDefinition) { 121 try { 122 if (typeDefinition.isKindOf(jspBaseClass)) { 123 int start = typeDefinition.getAst().getFirstToken().getLine(); 124 int end = typeDefinition.getAst().getLastToken().getLine(); 125 JspDescriptor jspDescr = new JspDescriptor(); 126 CodeMetric typeCodeMetric = new CodeMetric(); 127 typeCodeMetric.setDescriptonEntity("Class"); 128 typeCodeMetric.setName(typeDefinition.getFcn()); 129 System.out.println("++ " 131 + typeDefinition.getCompilationUnit().getPackage() 132 .getName().toString()); 133 typeCodeMetric.source_url = ((SourceMarker) typeDefinition) 134 .getSourceURL(); 135 typeCodeMetric.source_line = ((SourceMarker) typeDefinition) 136 .getLine(); 137 typeCodeMetric.source_col = ((SourceMarker) typeDefinition) 138 .getColumn(); 139 typeCodeMetric.setNcss(end - start); 140 jspDescr.codeMetric = typeCodeMetric; 141 this.currentJspDescriptor = jspDescr; 143 jspList.put((String ) typeDefinition.getFcn(), jspDescr); 144 } 145 } catch (JselException e) { 146 e.printStackTrace(); 148 } 149 } 150 public void leave(Repository repository) { 151 context.annotate(new LinkedAnnotation() { 153 private String path; 154 public String getName() { 155 return getContext().getDescriptor().getName(); 156 } 157 public String getPath() { 158 return path; 159 } 160 public void render(AnnotationContext context) 161 throws HammurapiException { 162 String errorCausingFile = ""; 163 class JspInspectorRenderer extends AbstractRenderer { 165 JspInspectorRenderer() { 166 super(new RenderRequest(JspInspector.this)); 167 } 168 public Element render(Document document) 169 throws RenderingException { 170 Element entities = document.createElement("Entities"); 171 Element jspMetricElement = document 172 .createElement("JspMetric"); 173 Collection values = jspList.values(); 174 Iterator it = values.iterator(); 175 System.out.println("Number of JspDescriptors - "+values.size()); 176 while (it.hasNext()) { 177 JspDescriptor jspD = (JspDescriptor) it.next(); 178 jspMetricElement.appendChild(jspD.toDom(document)); 179 } 180 return jspMetricElement; 181 } 182 } 191 AnnotationContext.FileEntry fileEntry = context 192 .getNextFile(context.getExtension()); 193 path = fileEntry.getPath(); 194 System.out.println(".> " + this.getPath().toString()); 195 AnnotationContext.FileEntry fileEntryXml = context 196 .getNextFile(".xml"); 197 try { 198 JspInspectorRenderer renderer = new JspInspectorRenderer(); 199 FileOutputStream out = new FileOutputStream (fileEntry 200 .getFile()); 201 renderer.setEmbeddedStyle(context.isEmbeddedStyle()); 202 try { 203 errorCausingFile = fileEntry.getFile() 204 .getAbsolutePath(); 205 renderer.render(context.getParameter("style") == null 206 ? null 207 : new FileInputStream (context.getParameter( 208 "style").toString()), out); 209 } finally { 210 out.close(); 211 } 212 FileOutputStream outXml = new FileOutputStream (fileEntryXml 214 .getFile()); 215 try { 216 errorCausingFile = fileEntryXml.getFile() 217 .getAbsolutePath(); 218 renderer.setEmbeddedStyle(false); 220 renderer.render(outXml); 221 } finally { 226 outXml.close(); 227 } 228 } catch (Exception e) { 229 throw new HammurapiException("Can't save " 230 + errorCausingFile + ". " + e.getMessage()); 231 } 232 } 233 public Properties getProperties() { 234 Properties ret = new Properties (); 235 ret.setProperty("left-panel", "yes"); 236 ret.setProperty("target", "JSP Metrics"); 237 return ret; 238 } 239 }); 240 } 241 public boolean setParameter(String name, Object value) 242 throws ConfigurationException { 243 if ("jsp-base-class".equals(name)) { 244 jspBaseClass = value.toString(); 245 return true; 246 } else { 247 throw new ConfigurationException("Parameter '" + name 248 + "' is not supported"); 249 } 250 } 251 } 252 | Popular Tags |