001: /*
002: * Created on May 29, 2004
003: *
004: *
005: * JSP invoke technique r Servlet response.redirect a HTML action L HTML HREF I
006: * JSP include f JSP forward X JavaScript s HTML Frame Source
007: *
008: */
009: package org.hammurapi.inspectors.metrics;
010:
011: import java.io.FileInputStream;
012: import java.io.FileOutputStream;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Properties;
019:
020: import org.hammurapi.InspectorBase;
021: import org.hammurapi.HammurapiException;
022: import org.hammurapi.results.AnnotationContext;
023: import org.hammurapi.results.LinkedAnnotation;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026:
027: import com.pavelvlasov.config.ConfigurationException;
028: import com.pavelvlasov.jsel.JselException;
029: import com.pavelvlasov.jsel.Repository;
030: import com.pavelvlasov.jsel.TypeDefinition;
031: import com.pavelvlasov.jsel.VariableDefinition;
032: import com.pavelvlasov.jsel.expressions.MethodCall;
033: import com.pavelvlasov.jsel.expressions.StringConstant;
034: import com.pavelvlasov.render.RenderRequest;
035: import com.pavelvlasov.render.RenderingException;
036: import com.pavelvlasov.render.dom.AbstractRenderer;
037: import com.pavelvlasov.review.SourceMarker;
038:
039: /**
040: * @author MUCBJ0
041: *
042: * TODO parameter for JspBase Type, Jsp File Extractor,
043: */
044: public class JspInspector extends InspectorBase implements
045: com.pavelvlasov.config.Parameterizable {
046:
047: private Map jspList = new HashMap();
048: private boolean isJavaScriptStream = false;
049: private JspDescriptor currentJspDescriptor = null;
050: private String jspBaseClass = "";
051:
052: public void visit(MethodCall methodCall) {
053: // Pavel Vlasov - to avoid NPE
054: if (currentJspDescriptor == null) {
055: return;
056: }
057: // System.out.println("*> " + methodCall.getName());
058: if ("response.sendRedirect".equals(methodCall.getName()
059: .toString())) {
060: String referenceStr = methodCall.getParameters().get(0)
061: .toString();
062: JspXref jXref = new JspXref(currentJspDescriptor,
063: "response.sendRedirect", referenceStr);
064: currentJspDescriptor.listOfInvokedJsp.add(jXref);
065: }
066: if ("pageContext.forward".equals(methodCall.getName()
067: .toString())) {
068: String referenceStr = methodCall.getParameters().get(0)
069: .toString();
070: JspXref jXref = new JspXref(currentJspDescriptor,
071: "JSP Forward", referenceStr);
072: currentJspDescriptor.listOfInvokedJsp.add(jXref);
073: }
074: if ("out.print".equals(methodCall.getName().toString())
075: || "out.println"
076: .equals(methodCall.getName().toString())) {
077: this .currentJspDescriptor.numberOfOutPrintOps++;
078: } else if ("out.write".equals(methodCall.getName().toString())) {
079: this .currentJspDescriptor.numberOfOutWriteOps++;
080: List parameterList = methodCall.getParameters();
081: for (int i = 0; i < parameterList.size(); i++) {
082: Object param = parameterList.get(i);
083: if (param instanceof StringConstant) {
084: StringConstant strCnst = (StringConstant) param;
085:
086: if (!isJavaScriptStream
087: && strCnst.toString().toLowerCase()
088: .indexOf("<script".toLowerCase()) > -1) {
089: isJavaScriptStream = true;
090: this .currentJspDescriptor.numberOfJavaScriptSnippets++;
091:
092: currentJspDescriptor
093: .analyseJavaScriptPortion(strCnst
094: .toString());
095: } else if (isJavaScriptStream
096: && strCnst.toString().toLowerCase()
097: .indexOf("</script>".toLowerCase()) > -1) {
098:
099: currentJspDescriptor
100: .analyseJavaScriptPortion(strCnst
101: .toString());
102:
103: isJavaScriptStream = false;
104: } else if (isJavaScriptStream) {
105:
106: currentJspDescriptor
107: .analyseJavaScriptPortion(strCnst
108: .toString());
109: }
110: if (!isJavaScriptStream
111: && strCnst.toString().toLowerCase()
112: .indexOf("<form".toLowerCase()) > -1) {
113: currentJspDescriptor.analyseHtmlForm(strCnst
114: .toString());
115: }
116: }
117: }
118: }
119: }
120:
121: public void extractJavaScriptPortion(String jsStream) {
122: // Pavel Vlasov: to avoid NPE
123: if (currentJspDescriptor != null) {
124: currentJspDescriptor.analyseJavaScriptPortion(jsStream);
125: }
126: }
127:
128: public void visit(VariableDefinition element) {
129: //!! SQL Extractor
130: }
131:
132: public void visit(TypeDefinition typeDefinition) {
133: try {
134: if (typeDefinition.isKindOf(jspBaseClass)) {
135: int start = typeDefinition.getAst().getFirstToken()
136: .getLine();
137: int end = typeDefinition.getAst().getLastToken()
138: .getLine();
139: JspDescriptor jspDescr = new JspDescriptor();
140: CodeMetric typeCodeMetric = new CodeMetric();
141: typeCodeMetric.setDescriptonEntity("Class");
142: typeCodeMetric.setName(typeDefinition.getFcn());
143: // packageName.replace('.','/')+compilationUnit.getName()
144: System.out.println("++ "
145: + typeDefinition.getCompilationUnit()
146: .getPackage().getName().toString());
147: typeCodeMetric.source_url = ((SourceMarker) typeDefinition)
148: .getSourceURL();
149: typeCodeMetric.source_line = ((SourceMarker) typeDefinition)
150: .getLine();
151: typeCodeMetric.source_col = ((SourceMarker) typeDefinition)
152: .getColumn();
153: typeCodeMetric.setNcss(end - start);
154: jspDescr.codeMetric = typeCodeMetric;
155: //!! potential thread issue !!
156: this .currentJspDescriptor = jspDescr;
157: jspList.put((String) typeDefinition.getFcn(), jspDescr);
158: }
159: } catch (JselException e) {
160: // TODO Auto-generated catch block
161: e.printStackTrace();
162: }
163: }
164:
165: public void leave(Repository repository) {
166: //aggregate();
167: context.annotate(new LinkedAnnotation() {
168: private String path;
169:
170: public String getName() {
171: return getContext().getDescriptor().getName();
172: }
173:
174: public String getPath() {
175: return path;
176: }
177:
178: public void render(AnnotationContext context)
179: throws HammurapiException {
180: String errorCausingFile = "";
181: // Output images here. See AnnotationTest for details.
182: class JspInspectorRenderer extends AbstractRenderer {
183: JspInspectorRenderer() {
184: super (new RenderRequest(JspInspector.this ));
185: }
186:
187: public Element render(Document document)
188: throws RenderingException {
189: Element entities = document
190: .createElement("Entities");
191: Element jspMetricElement = document
192: .createElement("JspMetric");
193: Collection values = jspList.values();
194: Iterator it = values.iterator();
195: System.out
196: .println("Number of JspDescriptors - "
197: + values.size());
198: while (it.hasNext()) {
199: JspDescriptor jspD = (JspDescriptor) it
200: .next();
201: jspMetricElement.appendChild(jspD
202: .toDom(document));
203: }
204: return jspMetricElement;
205: }
206: } //-- end class JspInspectorRenderer
207: /*
208: * locReport = new LocReporter( context, classMaxLoc,
209: * functionMaxLoc, ncssReport, chartDebugWindow );
210: * locReport.setNcssClassList( ncssClassList );
211: * locReport.setNcssFunctionList( ncssFunctionList );
212: *
213: * locReport.doIt( projectMetric) ;
214: */
215: AnnotationContext.FileEntry fileEntry = context
216: .getNextFile(context.getExtension());
217: path = fileEntry.getPath();
218: System.out.println(".> " + this .getPath().toString());
219: AnnotationContext.FileEntry fileEntryXml = context
220: .getNextFile(".xml");
221: try {
222: JspInspectorRenderer renderer = new JspInspectorRenderer();
223: FileOutputStream out = new FileOutputStream(
224: fileEntry.getFile());
225: renderer
226: .setEmbeddedStyle(context.isEmbeddedStyle());
227: try {
228: errorCausingFile = fileEntry.getFile()
229: .getAbsolutePath();
230: renderer
231: .render(
232: context.getParameter("style") == null ? null
233: : new FileInputStream(
234: context
235: .getParameter(
236: "style")
237: .toString()),
238: out);
239: } finally {
240: out.close();
241: }
242: //-- write a XML file for other XSL usage
243: FileOutputStream outXml = new FileOutputStream(
244: fileEntryXml.getFile());
245: try {
246: errorCausingFile = fileEntryXml.getFile()
247: .getAbsolutePath();
248: //-- write a XML file for other XSL usage
249: renderer.setEmbeddedStyle(false);
250: renderer.render(outXml);
251: // renderer.setPrettyPrint( true );
252: // InputStream
253: // inStream=getClass().getClassLoader().getResourceAsStream(xmlResourceName);
254: // renderer.render(inStream, outXml);
255: } finally {
256: outXml.close();
257: }
258: } catch (Exception e) {
259: throw new HammurapiException("Can't save "
260: + errorCausingFile + ". " + e.getMessage());
261: }
262: }
263:
264: public Properties getProperties() {
265: Properties ret = new Properties();
266: ret.setProperty("left-panel", "yes");
267: ret.setProperty("target", "JSP Metrics");
268: return ret;
269: }
270: });
271: }
272:
273: public boolean setParameter(String name, Object value)
274: throws ConfigurationException {
275: if ("jsp-base-class".equals(name)) {
276: jspBaseClass = value.toString();
277: return true;
278: } else {
279: throw new ConfigurationException("Parameter '" + name
280: + "' is not supported");
281: }
282: }
283: }
|