Source code: com/meterware/httpunit/scripting/ScriptableDelegate.java
1 package com.meterware.httpunit.scripting;
2
3 import com.meterware.httpunit.HTMLElement;
4
5 /********************************************************************************************************************
6 * $Id: ScriptableDelegate.java,v 1.10 2004/03/09 21:49:18 russgold Exp $
7 *
8 * Copyright (c) 2002, Russell Gold
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
11 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
13 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
19 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 *******************************************************************************************************************/
25
26 /**
27 * An interface for objects which will be accessible via scripting.
28 *
29 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
30 **/
31 abstract public class ScriptableDelegate {
32
33 private ScriptingEngine _scriptEngine;
34
35
36 private static final ScriptingEngine NULL_SCRIPT_ENGINE = new ScriptingEngine() {
37 public boolean supportsScriptLanguage( String language ) { return false; }
38 public String executeScript( String language, String script ) { return ""; }
39 public boolean performEvent( String eventScript ) { return true; }
40 public String evaluateScriptExpression( String urlString ) { return null; }
41 public ScriptingEngine newScriptingEngine( ScriptableDelegate child ) { return this; }
42 public void clearCaches() {}
43 };
44
45
46 public boolean supportsScript( String language ) {
47 return getScriptEngine().supportsScriptLanguage( language );
48 }
49
50
51 /**
52 * Executes the specified scripted event.
53 **/
54 public boolean doEvent( String eventScript ) {
55 if (eventScript.length() == 0) return true;
56 return getScriptEngine().performEvent( eventScript );
57 }
58
59
60 /**
61 * Executes the specified script, returning any intended replacement text.
62 * @return the replacement text, which may be empty.
63 **/
64 public String runScript( String language, String script ) {
65 return (script.length() == 0) ? "" : getScriptEngine().executeScript( language, script );
66 }
67
68
69 /**
70 * Evaluates the specified javascript expression, returning its value.
71 **/
72 public String evaluateExpression( String urlString ) {
73 if (urlString.length() == 0) return null;
74 return getScriptEngine().evaluateScriptExpression( urlString );
75 }
76
77
78 /**
79 * Returns the value of the named property. Will return null if the property does not exist.
80 **/
81 public Object get( String propertyName ) {
82 return null;
83 }
84
85
86 /**
87 * Returns the value of the index property. Will return null if the property does not exist.
88 **/
89 public Object get( int index ) {
90 return null;
91 }
92
93
94 /**
95 * Sets the value of the named property. Will throw a runtime exception if the property does not exist or
96 * cannot accept the specified value.
97 **/
98 public void set( String propertyName, Object value ) {
99 throw new RuntimeException( "No such property: " + propertyName );
100 }
101
102
103 /**
104 * Specifies the scripting engine to be used.
105 */
106 public void setScriptEngine( ScriptingEngine scriptEngine ) {
107 _scriptEngine = scriptEngine;
108 }
109
110
111 public ScriptingEngine getScriptEngine() {
112 return _scriptEngine != null ? _scriptEngine : NULL_SCRIPT_ENGINE;
113 }
114
115
116 public ScriptingEngine getScriptEngine( ScriptableDelegate child ) {
117 return getScriptEngine().newScriptingEngine( child );
118 }
119
120
121 protected ScriptableDelegate[] getDelegates( final HTMLElement[] elements ) {
122 ScriptableDelegate[] result = new ScriptableDelegate[ elements.length ];
123 for (int i = 0; i < elements.length; i++) {
124 result[i] = elements[i].getScriptableDelegate();
125 }
126 return result;
127 }
128
129 }