KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > metrics > JspDescriptor


1 /*
2  * Created on May 29, 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package org.hammurapi.inspectors.metrics;
8 import java.util.Iterator JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 /**
14  * @author MUCBJ0
15  *
16  * TODO To change the template for this generated type comment go to
17  * Window - Preferences - Java - Code Style - Code Templates
18  */

19 public class JspDescriptor {
20     public CodeMetric codeMetric = null;
21     public int numberOfJavaScriptSnippets = 0;
22     public int numberOfJavaScriptFunctions = 0;
23     
24     public int numberOfPopUpCalls = 0;
25     public int numberOfOutWriteOps = 0;
26     public int numberOfOutPrintOps = 0;
27     public int numberOfJavaScriptSubmits = 0;
28     public Vector JavaDoc listOfJavaScriptCalledJsp = new Vector JavaDoc();
29     public Vector JavaDoc listOfInvokedJsp = new Vector JavaDoc();
30     public Vector JavaDoc listOfSqlStrings = new Vector JavaDoc();
31     // <form name=\"frmTresuryUpd\" action=\"eap_treasury_update_list.jsp\" >
32
public void analyseHtmlForm(String JavaDoc jsStream) {
33         try {
34             int actionIndex = jsStream.indexOf("action=");
35             int jspSuffixIndex = jsStream.indexOf(".jsp");
36             int closeIndex = jsStream.indexOf(">");
37             if (actionIndex > -1 && jspSuffixIndex > -1 && closeIndex > -1
38                     && closeIndex > jspSuffixIndex) {
39                 String JavaDoc referenceStr = jsStream.substring(actionIndex + 8,
40                         jspSuffixIndex + 3);
41                 JspXref jXref = new JspXref(this, "HTML form action",
42                         referenceStr);
43                 this.listOfInvokedJsp.add(jXref);
44             }
45         } catch (Exception JavaDoc e) {
46             System.out.println(jsStream);
47             e.printStackTrace();
48         }
49     }
50     public void analyseJavaScriptPortion(String JavaDoc jsStream) {
51         
52         String JavaDoc tmpJSStream = jsStream;
53         int subCount = tmpJSStream.toCharArray().length;
54
55         while ((subCount = tmpJSStream.lastIndexOf("function ", subCount-1)) != -1) {
56             numberOfJavaScriptFunctions++;
57             tmpJSStream = tmpJSStream.substring(0, subCount);
58         }
59         
60         tmpJSStream = jsStream;
61         subCount = tmpJSStream.toCharArray().length;
62         
63         while ((subCount = tmpJSStream.lastIndexOf(".submit()", subCount-1)) != -1) {
64             numberOfJavaScriptSubmits++;
65             tmpJSStream = tmpJSStream.substring(0, subCount);
66         }
67         
68 /* if (jsStream.indexOf("function ") > -1) {
69             System.out.println("amit-Script function - " + jsStream);
70             numberOfJavaScriptFunctions++;
71             System.out.println("amit-numberOfJavaScriptFunctions - " + numberOfJavaScriptFunctions);
72         }
73         
74         if (jsStream.indexOf(".submit()") > -1) {
75             numberOfJavaScriptSubmits++;
76         }
77 */

78         
79         int startWinOpenerIndex = jsStream.indexOf("window.opener");
80         int startWinOpenIndex = jsStream.indexOf("window.open");
81         int startJspIndex = jsStream.indexOf(".jsp");
82         int endWinOpenIndex = jsStream.indexOf(")");
83         if (startWinOpenIndex > -1 && startWinOpenerIndex == -1) {
84             numberOfPopUpCalls++;
85         }
86         if (startWinOpenIndex > -1 && startWinOpenerIndex == -1
87                 && startJspIndex > -1 && startJspIndex < endWinOpenIndex) {
88             findJspXref(startWinOpenIndex, jsStream);
89         }
90         int startActionIndex = jsStream.indexOf(".action");
91         if (startActionIndex > -1) {
92             findJspXref(startActionIndex, jsStream);
93         }
94     }
95     
96     private void findJspXref(int startIndex, String JavaDoc jsStream) {
97         try {
98             int startDoubleQuote = jsStream.indexOf("\"", startIndex);
99             int endDoubleQuote = jsStream.indexOf("\"", startDoubleQuote + 1);
100             String JavaDoc referenceStr = jsStream.substring(startDoubleQuote,
101                     endDoubleQuote);
102             // System.out.println("* " + startActionIndex+ ", " +
103
// startDoubleQuote+ ", "
104
// +endDoubleQuote+ ", " + referenceStr);
105
if (referenceStr.indexOf(".jsp") > -1) {
106                 listOfJavaScriptCalledJsp.add(new JspXref(this,
107                         "Java Script Action", referenceStr));
108                 System.out.println(referenceStr);
109             }
110         } catch (Exception JavaDoc e) {
111             System.out.println(jsStream);
112             e.printStackTrace();
113         }
114     }
115     public Element JavaDoc toDom(Document JavaDoc document) {
116         
117         Element JavaDoc ret = document.createElement("JspDescriptor");
118         ret.appendChild(this.codeMetric.toDom(document));
119         ret.setAttribute("numberOfJavaScriptSnippets", String
120                 .valueOf(numberOfJavaScriptSnippets));
121         ret.setAttribute("numberOfJavaScriptFunctions", String
122                 .valueOf(numberOfJavaScriptFunctions));
123         ret.setAttribute("numberOfPopUpCalls", String
124                 .valueOf(numberOfPopUpCalls));
125         ret.setAttribute("numberOfOutWriteOps", String
126                 .valueOf(numberOfOutWriteOps));
127         ret.setAttribute("numberOfOutPrintOps", String
128                 .valueOf(numberOfOutPrintOps));
129         ret.setAttribute("numberOfJavaScriptSubmits", String
130                 .valueOf(numberOfJavaScriptSubmits));
131         Iterator JavaDoc it = listOfInvokedJsp.iterator();
132         while (it.hasNext()) {
133             //!! use a reference Descriptor Obj
134
JspXref jspX = (JspXref) it.next();
135             ret.appendChild(jspX.toDom(document));
136         }
137         it = listOfJavaScriptCalledJsp.iterator();
138         while (it.hasNext()) {
139             //!! use a reference Descriptor Obj
140
JspXref jspX = (JspXref) it.next();
141             ret.appendChild(jspX.toDom(document));
142         }
143         it = listOfSqlStrings.iterator();
144         while (it.hasNext()) {
145             //!! use a reference Descriptor Obj
146
Element JavaDoc jspRef = document.createElement("SqlString");
147             jspRef.setAttribute("name", (String JavaDoc) it.next());
148             ret.appendChild(jspRef);
149         }
150         return ret;
151     }
152 }
153
Popular Tags