001: /*
002: * Created on May 29, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.hammurapi.inspectors.metrics;
008:
009: import java.util.Iterator;
010: import java.util.Vector;
011:
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014:
015: /**
016: * @author MUCBJ0
017: *
018: * TODO To change the template for this generated type comment go to
019: * Window - Preferences - Java - Code Style - Code Templates
020: */
021: public class JspDescriptor {
022: public CodeMetric codeMetric = null;
023: public int numberOfJavaScriptSnippets = 0;
024: public int numberOfJavaScriptFunctions = 0;
025:
026: public int numberOfPopUpCalls = 0;
027: public int numberOfOutWriteOps = 0;
028: public int numberOfOutPrintOps = 0;
029: public int numberOfJavaScriptSubmits = 0;
030: public Vector listOfJavaScriptCalledJsp = new Vector();
031: public Vector listOfInvokedJsp = new Vector();
032: public Vector listOfSqlStrings = new Vector();
033:
034: // <form name=\"frmTresuryUpd\" action=\"eap_treasury_update_list.jsp\" >
035: public void analyseHtmlForm(String jsStream) {
036: try {
037: int actionIndex = jsStream.indexOf("action=");
038: int jspSuffixIndex = jsStream.indexOf(".jsp");
039: int closeIndex = jsStream.indexOf(">");
040: if (actionIndex > -1 && jspSuffixIndex > -1
041: && closeIndex > -1 && closeIndex > jspSuffixIndex) {
042: String referenceStr = jsStream.substring(
043: actionIndex + 8, jspSuffixIndex + 3);
044: JspXref jXref = new JspXref(this , "HTML form action",
045: referenceStr);
046: this .listOfInvokedJsp.add(jXref);
047: }
048: } catch (Exception e) {
049: System.out.println(jsStream);
050: e.printStackTrace();
051: }
052: }
053:
054: public void analyseJavaScriptPortion(String jsStream) {
055:
056: String tmpJSStream = jsStream;
057: int subCount = tmpJSStream.toCharArray().length;
058:
059: while ((subCount = tmpJSStream.lastIndexOf("function ",
060: subCount - 1)) != -1) {
061: numberOfJavaScriptFunctions++;
062: tmpJSStream = tmpJSStream.substring(0, subCount);
063: }
064:
065: tmpJSStream = jsStream;
066: subCount = tmpJSStream.toCharArray().length;
067:
068: while ((subCount = tmpJSStream.lastIndexOf(".submit()",
069: subCount - 1)) != -1) {
070: numberOfJavaScriptSubmits++;
071: tmpJSStream = tmpJSStream.substring(0, subCount);
072: }
073:
074: /* if (jsStream.indexOf("function ") > -1) {
075: System.out.println("amit-Script function - " + jsStream);
076: numberOfJavaScriptFunctions++;
077: System.out.println("amit-numberOfJavaScriptFunctions - " + numberOfJavaScriptFunctions);
078: }
079:
080: if (jsStream.indexOf(".submit()") > -1) {
081: numberOfJavaScriptSubmits++;
082: }
083: */
084:
085: int startWinOpenerIndex = jsStream.indexOf("window.opener");
086: int startWinOpenIndex = jsStream.indexOf("window.open");
087: int startJspIndex = jsStream.indexOf(".jsp");
088: int endWinOpenIndex = jsStream.indexOf(")");
089: if (startWinOpenIndex > -1 && startWinOpenerIndex == -1) {
090: numberOfPopUpCalls++;
091: }
092: if (startWinOpenIndex > -1 && startWinOpenerIndex == -1
093: && startJspIndex > -1
094: && startJspIndex < endWinOpenIndex) {
095: findJspXref(startWinOpenIndex, jsStream);
096: }
097: int startActionIndex = jsStream.indexOf(".action");
098: if (startActionIndex > -1) {
099: findJspXref(startActionIndex, jsStream);
100: }
101: }
102:
103: private void findJspXref(int startIndex, String jsStream) {
104: try {
105: int startDoubleQuote = jsStream.indexOf("\"", startIndex);
106: int endDoubleQuote = jsStream.indexOf("\"",
107: startDoubleQuote + 1);
108: String referenceStr = jsStream.substring(startDoubleQuote,
109: endDoubleQuote);
110: // System.out.println("* " + startActionIndex+ ", " +
111: // startDoubleQuote+ ", "
112: // +endDoubleQuote+ ", " + referenceStr);
113: if (referenceStr.indexOf(".jsp") > -1) {
114: listOfJavaScriptCalledJsp.add(new JspXref(this ,
115: "Java Script Action", referenceStr));
116: System.out.println(referenceStr);
117: }
118: } catch (Exception e) {
119: System.out.println(jsStream);
120: e.printStackTrace();
121: }
122: }
123:
124: public Element toDom(Document document) {
125:
126: Element ret = document.createElement("JspDescriptor");
127: ret.appendChild(this .codeMetric.toDom(document));
128: ret.setAttribute("numberOfJavaScriptSnippets", String
129: .valueOf(numberOfJavaScriptSnippets));
130: ret.setAttribute("numberOfJavaScriptFunctions", String
131: .valueOf(numberOfJavaScriptFunctions));
132: ret.setAttribute("numberOfPopUpCalls", String
133: .valueOf(numberOfPopUpCalls));
134: ret.setAttribute("numberOfOutWriteOps", String
135: .valueOf(numberOfOutWriteOps));
136: ret.setAttribute("numberOfOutPrintOps", String
137: .valueOf(numberOfOutPrintOps));
138: ret.setAttribute("numberOfJavaScriptSubmits", String
139: .valueOf(numberOfJavaScriptSubmits));
140: Iterator it = listOfInvokedJsp.iterator();
141: while (it.hasNext()) {
142: //!! use a reference Descriptor Obj
143: JspXref jspX = (JspXref) it.next();
144: ret.appendChild(jspX.toDom(document));
145: }
146: it = listOfJavaScriptCalledJsp.iterator();
147: while (it.hasNext()) {
148: //!! use a reference Descriptor Obj
149: JspXref jspX = (JspXref) it.next();
150: ret.appendChild(jspX.toDom(document));
151: }
152: it = listOfSqlStrings.iterator();
153: while (it.hasNext()) {
154: //!! use a reference Descriptor Obj
155: Element jspRef = document.createElement("SqlString");
156: jspRef.setAttribute("name", (String) it.next());
157: ret.appendChild(jspRef);
158: }
159: return ret;
160: }
161: }
|