From 9160a2f5658ac7e58debfd04b4de81e550c9b02a Mon Sep 17 00:00:00 2001 From: dolby-oss Date: Fri, 3 Oct 2008 20:07:32 +0000 Subject: [PATCH] fixes to extraction of scripts from HTML git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3044 f5eafffb-2e1d-0410-98e4-8ec43c5233c4 --- .../ibm/wala/cast/js/util/HTMLCallback.java | 26 +++++++++++++++++-- .../com/ibm/wala/cast/js/util/WebUtil.java | 5 +++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/HTMLCallback.java b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/HTMLCallback.java index 63aa1fd98..2fbe92de4 100644 --- a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/HTMLCallback.java +++ b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/HTMLCallback.java @@ -67,7 +67,6 @@ public class HTMLCallback extends HTMLEditorKit.ParserCallback { System.out.println(new String(data)); try { out2.write(data); - out2.write("\n"); } catch (IOException e) { System.out.println("Error writing to second file"); } @@ -167,6 +166,9 @@ public class HTMLCallback extends HTMLEditorKit.ParserCallback { if(attr.substring(0,2).equals("on")) { out.write(varName + "." + attr + " = function " + attr + "_" + varName + "(event) {" + value + "};\n"); out2.write("\n\n" + varName2 + "." + attr + "(null);\n\n"); + } else if (value.startsWith("javascript:") || value.startsWith("javaScript:")) { + out.write("var " + varName + attr + " = " + value.substring(11) + "\n"); + out.write(varName + ".setAttribute('" + attr + "', " + varName + attr + ");\n"); } else { out.write(varName + ".setAttribute('" + attr + "', '" + value + "');\n"); } @@ -191,6 +193,11 @@ public class HTMLCallback extends HTMLEditorKit.ParserCallback { public void handleEndTag(HTML.Tag t, int pos) { if(t.toString().toUpperCase().equals("SCRIPT")) { System.out.println("Exiting Script"); + try { + out2.write("\n\n"); + } catch (IOException e) { + + } script = false; } System.out.println("End" + t); @@ -199,7 +206,22 @@ public class HTMLCallback extends HTMLEditorKit.ParserCallback { public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) { System.out.println("Simple" + t); - createElement(t,a); + if (script) { + try { + out2.write("<" + t); + Enumeration names = a.getAttributeNames(); + while (names.hasMoreElements()) { + Object name = names.nextElement(); + Object val = a.getAttribute(name); + out2.write(" " + name + "='" + val + "'"); + } + out2.write(">"); + } catch (IOException e) { + + } + } else { + createElement(t,a); + } } public void handleError(String errorMsg, int pos) { diff --git a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/WebUtil.java b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/WebUtil.java index d776e2fbc..8b223a8e6 100644 --- a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/WebUtil.java +++ b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/util/WebUtil.java @@ -50,7 +50,10 @@ public class WebUtil { public static SourceFileModule extractScriptFromHTML(URL url, Generator generator) { try { String urlFile = url.getFile(); - String urlName = urlFile.substring(urlFile.lastIndexOf('/')); + String urlName = + urlFile.lastIndexOf('/')>0? + urlFile.substring(urlFile.lastIndexOf('/')): + url.getHost() + ".html"; File F = new File(outputDir + urlName); Trace.println("making driver at " + F + " " + outputDir); if (F.exists()) F.delete();