[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/javascript JavaScript.java,1.2,1.3
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-02 16:26:19
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/javascript In directory usw-pr-cvs1:/tmp/cvs-serv27406/src/com/meterware/httpunit/javascript Modified Files: JavaScript.java Log Message: add document.links and link.onMouseOver support Index: JavaScript.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/javascript/JavaScript.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JavaScript.java 1 Aug 2002 20:23:16 -0000 1.2 +++ JavaScript.java 2 Aug 2002 16:26:15 -0000 1.3 @@ -23,8 +23,10 @@ import com.meterware.httpunit.ScriptEngine; import com.meterware.httpunit.WebForm; import com.meterware.httpunit.WebResponse; +import com.meterware.httpunit.WebLink; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import org.mozilla.javascript.ClassDefinitionException; import org.mozilla.javascript.Context; @@ -51,11 +53,13 @@ Scriptable scope = context.initStandardObjects( null ); ScriptableObject.defineClass( scope, Window.class ); ScriptableObject.defineClass( scope, Document.class ); + ScriptableObject.defineClass( scope, Link.class ); ScriptableObject.defineClass( scope, Form.class ); ScriptableObject.defineClass( scope, Control.class ); + ScriptableObject.defineClass( scope, Link.class ); Window w = (Window) context.newObject( scope, "Window" ); - w.initialize( response.getScriptableObject() ); + w.initialize( null, response.getScriptableObject() ); } @@ -73,7 +77,7 @@ } - void initialize( com.meterware.httpunit.ScriptableObject scriptable ) + void initialize( JavaScriptEngine parent, com.meterware.httpunit.ScriptableObject scriptable ) throws JavaScriptException, NotAFunctionException, PropertyException, SAXException { _scriptable = scriptable; _scriptable.setScriptEngine( this ); @@ -120,9 +124,12 @@ /** * Converts a scriptable delegate obtained from a subobject into the appropriate Rhino-compatible Scriptable. + * This default implementation throws an exception. **/ - abstract Scriptable toScriptable( com.meterware.httpunit.ScriptableObject delegate ) - throws PropertyException, NotAFunctionException, JavaScriptException, SAXException; + Scriptable toScriptable( com.meterware.httpunit.ScriptableObject delegate ) + throws PropertyException, NotAFunctionException, JavaScriptException, SAXException { + throw new UnsupportedOperationException(); + } } @@ -152,11 +159,11 @@ } - void initialize( com.meterware.httpunit.ScriptableObject scriptable ) + void initialize( JavaScriptEngine parent, com.meterware.httpunit.ScriptableObject scriptable ) throws JavaScriptException, NotAFunctionException, PropertyException, SAXException { - super.initialize( scriptable ); + super.initialize( this, scriptable ); _document = (Document) Context.getCurrentContext().newObject( this, "Document" ); - _document.initialize( getDelegate().getDocument() ); + _document.initialize( this, getDelegate().getDocument() ); getDelegate().load(); } @@ -181,6 +188,7 @@ static public class Document extends JavaScriptEngine { private Scriptable _forms; + private Scriptable _links; public String getClassName() { @@ -188,9 +196,10 @@ } - void initialize( com.meterware.httpunit.ScriptableObject scriptable ) + void initialize( JavaScriptEngine parent, com.meterware.httpunit.ScriptableObject scriptable ) throws JavaScriptException, NotAFunctionException, PropertyException, SAXException { - super.initialize( scriptable ); + super.initialize( this, scriptable ); + initializeLinks(); initializeForms(); } @@ -200,11 +209,26 @@ } + public Scriptable jsGet_links() { + return _links; + } + + public Scriptable jsGet_forms() { return _forms; } + private void initializeLinks() throws PropertyException, NotAFunctionException, JavaScriptException, SAXException { + WebLink.Scriptable scriptables[] = getDelegate().getLinks(); + Link[] links = new Link[ scriptables.length ]; + for (int i = 0; i < links.length; i++) { + links[ i ] = (Link) toScriptable( scriptables[ i ] ); + } + _links = Context.getCurrentContext().newArray( this, links ); + } + + private void initializeForms() throws PropertyException, NotAFunctionException, JavaScriptException, SAXException { WebForm.Scriptable scriptables[] = getDelegate().getForms(); Form[] forms = new Form[ scriptables.length ]; @@ -217,9 +241,20 @@ Scriptable toScriptable( com.meterware.httpunit.ScriptableObject delegate ) throws PropertyException, JavaScriptException, NotAFunctionException, SAXException { - final Form form = (Form) Context.getCurrentContext().newObject( this, "Form" ); - form.initialize( delegate ); - return form; + JavaScriptEngine element = (JavaScriptEngine) Context.getCurrentContext().newObject( this, getScriptableClassName( delegate ) ); + element.initialize( this, delegate ); + return element; + } + + + private String getScriptableClassName( com.meterware.httpunit.ScriptableObject delegate ) { + if (delegate instanceof WebForm.Scriptable) { + return "Form"; + } else if (delegate instanceof WebLink.Scriptable) { + return "Link"; + } else { + throw new IllegalArgumentException( "Unknown ScriptableObject class: " + delegate.getClass() ); + } } @@ -230,7 +265,39 @@ } - static public class Form extends JavaScriptEngine { + abstract static public class HTMLElement extends JavaScriptEngine { + + private Document _document; + + + public Document jsGet_document() { + return _document; + } + + + void initialize( JavaScriptEngine parent, com.meterware.httpunit.ScriptableObject scriptable ) + throws JavaScriptException, NotAFunctionException, PropertyException, SAXException { + super.initialize( parent, scriptable ); + _document = (Document) parent; + } + + } + + + static public class Link extends HTMLElement { + + public Document jsGet_document() { + return super.jsGet_document(); + } + + + public String getClassName() { + return "Link"; + } + } + + + static public class Form extends HTMLElement { public String getClassName() { return "Form"; @@ -240,7 +307,7 @@ Scriptable toScriptable( com.meterware.httpunit.ScriptableObject delegate ) throws PropertyException, NotAFunctionException, JavaScriptException, SAXException { final Control control = (Control) Context.getCurrentContext().newObject( this, "Control" ); - control.initialize( delegate ); + control.initialize( this, delegate ); return control; } @@ -251,11 +318,6 @@ public String getClassName() { return "Control"; - } - - - Scriptable toScriptable( com.meterware.httpunit.ScriptableObject delegate ) { - return null; } } |