Expose temporary file created by source extractor. Fixes #19

This commit is contained in:
Manu Sridharan 2013-04-10 12:22:57 -07:00
parent 355bf4ad69
commit 4f7ec12edd
6 changed files with 36 additions and 6 deletions

View File

@ -137,7 +137,7 @@ public class JSCallGraphBuilderUtil extends com.ibm.wala.cast.js.ipa.callgraph.J
IRFactory irFactory = AstIRFactory.makeDefaultFactory();
JavaScriptLoaderFactory loaders = new WebPageLoaderFactory(translatorFactory, preprocessor);
try {
Set<MappedSourceModule> script = WebUtil.extractScriptFromHTML(url);
Set<MappedSourceModule> script = WebUtil.extractScriptFromHTML(url).fst;
scripts = script.toArray(new SourceModule[script.size()]);
} catch (Error e) {
SourceModule dummy = new SourceURLModule(url);

View File

@ -27,14 +27,14 @@ public class TestWebUtil extends WalaTestCase {
@Test public void testAjaxslt() throws Error {
URL url = getClass().getClassLoader().getResource("ajaxslt/test/xslt.html");
Assert.assertTrue(url != null);
Set<MappedSourceModule> mod = WebUtil.extractScriptFromHTML( url );
Set<MappedSourceModule> mod = WebUtil.extractScriptFromHTML( url ).fst;
Assert.assertTrue(mod != null);
}
@Test public void testAjaxpath() throws Error {
URL url = getClass().getClassLoader().getResource("ajaxslt/test/xpath.html");
Assert.assertTrue(url != null);
Set<MappedSourceModule> mod = WebUtil.extractScriptFromHTML( url );
Set<MappedSourceModule> mod = WebUtil.extractScriptFromHTML( url ).fst;
Assert.assertTrue(mod != null);
}
}

View File

@ -246,6 +246,11 @@ public class DomLessSourceExtractor extends JSSourceExtractor {
finalRegion.println("window.__MAIN__();");
}
}
/**
* for storing the name of the temp file created by extractSources()
*/
private File tempFile;
public Set<MappedSourceModule> extractSources(URL entrypointUrl, IHtmlParser htmlParser, IUrlResolver urlResolver)
throws IOException, Error {
@ -259,6 +264,7 @@ public class DomLessSourceExtractor extends JSSourceExtractor {
// writing the final region into one SourceFileModule.
File outputFile = createOutputFile(entrypointUrl, DELETE_UPON_EXIT, USE_TEMP_NAME);
tempFile = outputFile;
FileMapping fileMapping = finalRegion.writeToFile(new PrintStream(outputFile));
if (fileMapping == null) {
fileMapping = new EmptyFileMapping();
@ -301,5 +307,10 @@ public class DomLessSourceExtractor extends JSSourceExtractor {
entry.getMapping().dump(System.out);
}
@Override
public File getTempFile() {
return tempFile;
}
}

View File

@ -10,6 +10,7 @@
*****************************************************************************/
package com.ibm.wala.cast.js.html;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Set;
@ -31,4 +32,11 @@ public abstract class JSSourceExtractor {
public abstract Set<MappedSourceModule> extractSources(URL entrypointUrl, IHtmlParser htmlParser, IUrlResolver urlResolver) throws IOException, Error;
/**
* Returns the temporary file created by a call to
* {@link #extractSources(URL, IHtmlParser, IUrlResolver)} which holds all the
* discovered JS source. If no such file exists, returns <code>null</code>
*/
public abstract File getTempFile();
}

View File

@ -10,6 +10,7 @@
*****************************************************************************/
package com.ibm.wala.cast.js.html;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@ -19,6 +20,7 @@ import java.util.Set;
import com.ibm.wala.cast.ir.translator.TranslatorToCAst.Error;
import com.ibm.wala.cast.js.html.jericho.JerichoHtmlParser;
import com.ibm.wala.util.collections.Pair;
public class WebUtil {
@ -34,10 +36,19 @@ public class WebUtil {
WebUtil.factory = factory;
}
public static Set<MappedSourceModule> extractScriptFromHTML(URL url) throws Error {
/**
*
* @param url
* @return a pair (S,F), where S is a set of extracted sources, and F is the
* temp file holding the combined sources (or <code>null</code> if no
* such file exists)
* @throws Error
*/
public static Pair<Set<MappedSourceModule>,File> extractScriptFromHTML(URL url) throws Error {
try {
JSSourceExtractor extractor = new DefaultSourceExtractor();
return extractor.extractSources(url, factory.getParser(), new IdentityUrlResolver());
Set<MappedSourceModule> sources = extractor.extractSources(url, factory.getParser(), new IdentityUrlResolver());
return Pair.make(sources, extractor.getTempFile());
} catch (IOException e) {
throw new RuntimeException("trouble with " + url, e);
}

View File

@ -249,7 +249,7 @@ public class CorrelationFinder {
JavaScriptLoader.addBootstrapFile(WebUtil.preamble);
Set<? extends SourceModule> script = null;
try {
script = WebUtil.extractScriptFromHTML(url);
script = WebUtil.extractScriptFromHTML(url).fst;
} catch (Error e) {
assert false : e.warning;
}