📄 xjsdocument.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.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.SwingUtilities;
import org.mozilla.javascript.*;
import xbrowser.renderer.*;
import xbrowser.renderer.custom.*;
/** Represent <code>document</code> JavaScript object in browser. */
public class XJSDocument extends ScriptableObject implements XDOM {
public String getClassName() {
return "XJSDocument";
}
// Prototype constructor, needed by ScriptableObject
public XJSDocument() {
}
public XJSDocument( XHTMLDocument doc, XJSWindow window) {
this.doc = doc;
jsWindow = window;
images = new XHTMLCollection();
links = new XHTMLCollection();
forms = new XHTMLCollection();
anchors = new XHTMLCollection();
applets = new XHTMLCollection();
_writesKeys = new LinkedList();
_writes = new HashMap();
props = new HashMap(20);
pageProps = doc.getWindow().getPageProperties();
try {
defineSelf();
} catch( Exception e) {
XRendererSupport.getContext().getLogger().error( this, e);
}
}
public void endParsing() {
SwingUtilities.invokeLater( new Thread() {
public void run() {
try {
Thread.sleep( 1500); // Ugly hack
} catch( InterruptedException iexc) {}
XRendererSupport.getContext().getLogger().message(this, "Writing all writes to DOM");
Iterator it = _writesKeys.iterator();
setJSWritePhase(true);
while( it.hasNext()) {
Object key = it.next();
Element e = (Element) key;
try {
String text = (String)_writes.get( e);
XRendererSupport.insertAfterEnd(doc, e, text);
} catch( Exception ex) {
XRendererSupport.getContext().getLogger().error(this, ex);
}
}
XRendererSupport.getContext().getLogger().message(this, "Writing done!");
setJSWritePhase(false);
}
void setJSWritePhase( boolean is) {}
});
}
public void addObject( ScriptableObject object, String name, int destCollection) {
switch( destCollection) {
case XDOM.DOCUMENT :
defineProperty( name, object, 0);
break;
case XDOM.IMAGES :
images.add( object, name);
break;
case XDOM.FORMS :
forms.add( object, name);
break;
case XDOM.LINKS :
links.add( object, name);
break;
case XDOM.ANCHORS :
anchors.add( object, name);
break;
default:
throw new RuntimeException( "XJSDocument.addObject(): bad dest!");
}
}
public void setDocProperty( String name, String value) {
props.put( name.toUpperCase(), value);
}
public void defineSelf() throws Exception {
defineFunctionProperties( new String[]{ "write"}, getClass(), DONTENUM);
defineProperty( "window", getClass(), READONLY);
defineProperty( "location", getClass(), READONLY);
defineProperty( "referrer", getClass(), READONLY);
defineProperty( "images", images, READONLY);
defineProperty( "links", links, READONLY);
defineProperty( "forms", forms, READONLY);
defineProperty( "anchors", anchors, READONLY);
defineProperty( "applets", applets, READONLY);
defineProperty( "aLinkColor", getClass(), READONLY);
defineProperty( "vlinkColor", getClass(), READONLY);
defineProperty( "linkColor", getClass(), READONLY);
defineProperty( "fgColor", getClass(), READONLY);
defineProperty( "bgColor", getClass(), READONLY);
defineProperty( "defaultCharset", getClass(), READONLY);
defineProperty( "title", getClass(), READONLY);
defineProperty( "cookie", getClass(), DONTENUM);
defineProperty( "protocol", getClass(), DONTENUM);
defineProperty( "domain", getClass(), DONTENUM);
defineProperty( "location", getClass(), DONTENUM);
defineProperty( "URL", getClass(), DONTENUM);
defineProperty( "URLUnencoded", getClass(), READONLY);
defineProperty( "fileCreatedDate", getClass(), READONLY);
defineProperty( "fileModifiedDate", getClass(), READONLY);
defineProperty( "fileSize", getClass(), READONLY);
defineProperty( "lastModified", getClass(), READONLY);
}
public void setScript( Element el) {
// XRendererSupport.getContext().getLogger().message(this, "context: " + el + ", doc = " + this);
currentElement = el;
}
// Implementation of more JS function
public void write( String text) {
XRenderer r = jsWindow.htmlWindow;
if( r instanceof XCustomRenderer) {
XCustomRenderer cr = (XCustomRenderer)r;
if( _writes == null || currentElement == null || text == null)
XRendererSupport.getContext().getLogger().error( this, "Could not write! Add " +
currentElement + " to " + _writes + "/" + ", doc = " + this);
else {
// does not work during parsing!
// doc.insertBeforeStart( currentElement, text);
String prevText = (String)_writes.get( currentElement);
if( prevText == null) {
_writesKeys.add( currentElement);
_writes.put( currentElement, text);
} else {
text = prevText + text;
_writes.put( currentElement, text);
}
}
}
else
XRendererSupport.getContext().getLogger().error( this, "doc.write(): jsWindow is not CustomRenderer!");
}
public Object getDocument() {
return this;
}
public Object getWindow() {
return jsWindow;
}
public Object getLocation(){
return jsWindow.getLocation();
}
public String getReferrer() {
String referrer = "get from page properties";
return referrer;
}
// --------- Document properties ---------
private String getProp( String name) {
String result = (String) props.get( name);
return result == null ? "" : result;
}
public String getALinkColor() { return getProp("ALINKCOLOR"); }
public String getVlinkColor() { return getProp("VLINKCOLOR"); }
public String getLinkColor() { return getProp("LINKCOLOR"); }
public String getFgColor() { return getProp("FGCOLOR"); }
public String getBgColor() { return getProp("BGCOLOR"); }
public String getDefaultCharset() { return getProp("DEFAULTCHARSET"); }
// -------- Document dates -------
public String getFileModifiedDate() {
return pageProps.getModified();
}
public String getFileCreatedDate() {
return pageProps.getCreated();
}
public int getFileSize() {
return Integer.parseInt( pageProps.getSize());
}
public String getLastModified() throws ParseException {
String lastmod = pageProps.getModified();
Date lmdate = new SimpleDateFormat().parse( lastmod);
String reformatted = new SimpleDateFormat("MM/DD/YY hh:mm:ss").format(lmdate);
return reformatted;
}
public String getCookie() {
return XRendererSupport.getContext().getCookieManager().getApplicableCookies( doc.getBase()).toString();
}
public void setCookie( String newCookies) {
XRendererSupport.getContext().getCookieManager().setCookies( doc.getBase(), newCookies);
}
private URL getDocURL() {
return doc.getBase();
}
public String getProtocol() {
return getDocURL().getProtocol();
}
public String getDomain() {
return getDocURL().getHost();
}
public String getURL() {
return getDocURL().toString();
}
public String getURLUnencoded(){
try
{
return URLDecoder.decode( getURL());
}
catch( Exception e )
{
XRendererSupport.getContext().getLogger().error(this, e);
return getURL(); // is this correct ??!!
}
}
public String getTitle(){
return doc.getWindow().getPageTitle();
}
// Attributes:
public static Object UNDEFINED = new Integer(1);
protected XHTMLDocument doc;
protected XJSWindow jsWindow;
protected XHTMLCollection images;
protected XHTMLCollection anchors;
protected XHTMLCollection links;
protected XHTMLCollection forms;
protected XHTMLCollection applets;
protected HashMap props;
protected XPageProperties pageProps;
protected Element currentElement;
protected LinkedList _writesKeys;
protected HashMap _writes;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -