1 7 package org.hammurapi.inspectors.metrics; 8 import java.util.Iterator ; 9 import java.util.Vector ; 10 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 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 listOfJavaScriptCalledJsp = new Vector (); 29 public Vector listOfInvokedJsp = new Vector (); 30 public Vector listOfSqlStrings = new Vector (); 31 public void analyseHtmlForm(String 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 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 e) { 46 System.out.println(jsStream); 47 e.printStackTrace(); 48 } 49 } 50 public void analyseJavaScriptPortion(String jsStream) { 51 52 String 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 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 jsStream) { 97 try { 98 int startDoubleQuote = jsStream.indexOf("\"", startIndex); 99 int endDoubleQuote = jsStream.indexOf("\"", startDoubleQuote + 1); 100 String referenceStr = jsStream.substring(startDoubleQuote, 101 endDoubleQuote); 102 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 e) { 111 System.out.println(jsStream); 112 e.printStackTrace(); 113 } 114 } 115 public Element toDom(Document document) { 116 117 Element 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 it = listOfInvokedJsp.iterator(); 132 while (it.hasNext()) { 133 JspXref jspX = (JspXref) it.next(); 135 ret.appendChild(jspX.toDom(document)); 136 } 137 it = listOfJavaScriptCalledJsp.iterator(); 138 while (it.hasNext()) { 139 JspXref jspX = (JspXref) it.next(); 141 ret.appendChild(jspX.toDom(document)); 142 } 143 it = listOfSqlStrings.iterator(); 144 while (it.hasNext()) { 145 Element jspRef = document.createElement("SqlString"); 147 jspRef.setAttribute("name", (String ) it.next()); 148 ret.appendChild(jspRef); 149 } 150 return ret; 151 } 152 } 153 | Popular Tags |