📄 xjsthread.java
字号:
/****************************************************************
* XBrowser - eXtended web Browser *
* *
* Copyright (c) 2000-2001 Armond Avanes *
* Refer to ReadMe & License files for more information *
* *
* *
* By: Uladzimir V. Kavalchuk *
* Uladzimir_Kavalchuk@ep.minsk.by *
* http://xbrowser.sourceforge.net/ *
*****************************************************************/
package xbrowser.renderer.custom.js;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.text.Element;
import xbrowser.renderer.*;
import xbrowser.renderer.custom.*;
import org.mozilla.javascript.*;
public class XJSThread extends Thread implements XIScriptConsumer {
public XJSThread( URL _base, XHTMLDocument doc) {
base = _base;
window = new XJSWindow( doc, doc.getWindow());
document = new XJSDocument( doc, window);
window.setDocument( document);
scriptItems = new LinkedList();
_scripts = new LinkedList();
}
public XJSWindow getWindow() {
return window;
}
// XIScriptConsumer implementation {
public synchronized void addScript( String script, Element startTag) {
scriptItems.addLast( script);
_scripts.addLast( startTag);
notify();
if( JS_SYNCHRONIZE)
try {
wait();
} catch( InterruptedException e) {}
}
public synchronized void addScriptLink( String src) {
try {
scriptItems.addLast( new URL( base, src));
_scripts.addLast( null);
} catch( MalformedURLException exc) {
// We must also say user that this page has bad js links...
XRendererSupport.getContext().getLogger().error( this, exc);
}
notify();
if( JS_SYNCHRONIZE)
try {
wait();
} catch( InterruptedException e) {}
}
public XDOM getSink() {
return document;
}
/** document.write() requires inserting text after </script ;> tag;
* but JS portion is taken BEFORE end tag occurs. This function is a
* workaround; may be eliminated later. */
public synchronized void setEndScriptTag( Element startTag, Element endTag) {
int len = _scripts.size();
for( int i = 0; i < len; i++) {
if( _scripts.get( i) == startTag)
_scripts.set( i, endTag);
}
}
public void endParsing() {
document.endParsing(); // notify
}
// } XIScriptConsumer end, implementated
protected void prepare() {
ctx = Context.enter();
ctx.initStandardObjects( window);
XJSDocument.UNDEFINED = ctx.getUndefinedValue();
try {
window.defineSelf();
if( DEBUG_THREAD)
XRendererSupport.getContext().getLogger().message( this, "JS Thread prepared");
} catch( Exception pex) {
XRendererSupport.getContext().getLogger().message( this, "JS Thread failed during prepare:");
XRendererSupport.getContext().getLogger().error( this, pex);
}
}
protected void cleanup() {
Context.exit();
}
public void run() {
Object portion = null;
prepare();
while( ! isInterrupted()) {
synchronized( this) {
try {
if( scriptItems.isEmpty())
wait(); // wait notifications of next portion
} catch( InterruptedException ie) {
if( DEBUG_THREAD)
XRendererSupport.getContext().getLogger().error( this, "Interrupt worked!");
break;
}
if( ! scriptItems.isEmpty()) {
portion = scriptItems.removeFirst();
if( ! _scripts.isEmpty()) {
Element script = (Element)_scripts.removeFirst();
document.setScript( script);
}
else
XRendererSupport.getContext().getLogger().warning(this, "No script context.");
}
else {
XRendererSupport.getContext().getLogger().error(this, "This should not happen");
portion = null;
}
notifyAll();
} // synch
if( portion != null) {
if( DEBUG_EXCUTION)
XRendererSupport.getContext().getLogger().message( this, portion);
try {
exec( portion);
} catch( Exception e) { // Stupid Rhino throws MISC exception!
onErrorWindowHandle(e);
XRendererSupport.getContext().getLogger().error( this, "JavaScript error:");
XRendererSupport.getContext().getLogger().error( this, portion);
XRendererSupport.getContext().getLogger().error( this, e);
}
}
} // while()
cleanup();
if( DEBUG_THREAD)
XRendererSupport.getContext().getLogger().message( this, "JS Thread ended");
} // run()
public void setCurrentLine( int line) {
currentLine = line;
}
protected void onErrorWindowHandle( Exception e) {
/** @todo Try to run window.onerror, if was set. */
}
protected void exec( Object portion) {
try {
if( portion instanceof String) {
String js = (String)portion;
ctx.evaluateString( window, js, "<file>", currentLine, null);
} else
if( portion instanceof URL) {
URL js_src = (URL)portion;
InputStream is = js_src.openStream();
Reader js_reader = new InputStreamReader( is);
ctx.evaluateReader( window, js_reader, "<file>", currentLine, null);
}
} catch( JavaScriptException jex) {
if( IGNORE_JS_EXC) {
XRendererSupport.getContext().getLogger().message( this, "Problem in JS -- ignored.");
} else {
XRendererSupport.getContext().getLogger().message( this, portion);
XRendererSupport.getContext().getLogger().message( this, jex);
}
} catch( IOException iox) {
XRendererSupport.getContext().getLogger().message( this, "Exec error: " + iox + ": " + portion);
}
}
// Attributes:
final static boolean DEBUG_THREAD = false;
final static boolean DEBUG_EXCUTION = false;
final static boolean IGNORE_JS_EXC = false;
final static boolean JS_SYNCHRONIZE = false;
private LinkedList scriptItems;
private LinkedList /*<Element>*/ _scripts;
private XJSDocument document;
private XJSWindow window;
private URL base;
private Context ctx;
private int currentLine = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -