use of com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine in project htmlunit by HtmlUnit.
the class HtmlPage method loadExternalJavaScriptFile.
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* @param srcAttribute the source attribute from the script tag
* @param scriptCharset the charset from the script tag
* @return the result of loading the specified external JavaScript file
* @throws FailingHttpStatusCodeException if the request's status code indicates a request
* failure and the {@link WebClient} was configured to throw exceptions on failing
* HTTP status codes
*/
JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final Charset scriptCharset) throws FailingHttpStatusCodeException {
final WebClient client = getWebClient();
if (StringUtils.isBlank(srcAttribute) || !client.isJavaScriptEnabled()) {
return JavaScriptLoadResult.NOOP;
}
final URL scriptURL;
try {
scriptURL = getFullyQualifiedUrl(srcAttribute);
final String protocol = scriptURL.getProtocol();
if ("javascript".equals(protocol)) {
if (LOG.isInfoEnabled()) {
LOG.info("Ignoring script src [" + srcAttribute + "]");
}
return JavaScriptLoadResult.NOOP;
}
if (!"http".equals(protocol) && !"https".equals(protocol) && !"data".equals(protocol) && !"file".equals(protocol)) {
client.getJavaScriptErrorListener().malformedScriptURL(this, srcAttribute, new MalformedURLException("unknown protocol: '" + protocol + "'"));
return JavaScriptLoadResult.NOOP;
}
} catch (final MalformedURLException e) {
client.getJavaScriptErrorListener().malformedScriptURL(this, srcAttribute, e);
return JavaScriptLoadResult.NOOP;
}
final Object script;
try {
script = loadJavaScriptFromUrl(scriptURL, scriptCharset);
} catch (final IOException e) {
client.getJavaScriptErrorListener().loadScriptError(this, scriptURL, e);
return JavaScriptLoadResult.DOWNLOAD_ERROR;
} catch (final FailingHttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
return JavaScriptLoadResult.NO_CONTENT;
}
client.getJavaScriptErrorListener().loadScriptError(this, scriptURL, e);
throw e;
}
if (script == null) {
return JavaScriptLoadResult.COMPILATION_ERROR;
}
@SuppressWarnings("unchecked") final AbstractJavaScriptEngine<Object> engine = (AbstractJavaScriptEngine<Object>) client.getJavaScriptEngine();
engine.execute(this, script);
return JavaScriptLoadResult.SUCCESS;
}
Aggregations