001: package com.meterware.httpunit.scripting;
002:
003: import com.meterware.httpunit.HTMLElement;
004:
005: /********************************************************************************************************************
006: * $Id: ScriptableDelegate.java,v 1.10 2004/03/09 21:49:18 russgold Exp $
007: *
008: * Copyright (c) 2002, Russell Gold
009: *
010: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
011: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
012: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
013: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
016: * of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
019: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
020: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
021: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
022: * DEALINGS IN THE SOFTWARE.
023: *
024: *******************************************************************************************************************/
025:
026: /**
027: * An interface for objects which will be accessible via scripting.
028: *
029: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
030: **/
031: abstract public class ScriptableDelegate {
032:
033: private ScriptingEngine _scriptEngine;
034:
035: private static final ScriptingEngine NULL_SCRIPT_ENGINE = new ScriptingEngine() {
036: public boolean supportsScriptLanguage(String language) {
037: return false;
038: }
039:
040: public String executeScript(String language, String script) {
041: return "";
042: }
043:
044: public boolean performEvent(String eventScript) {
045: return true;
046: }
047:
048: public String evaluateScriptExpression(String urlString) {
049: return null;
050: }
051:
052: public ScriptingEngine newScriptingEngine(
053: ScriptableDelegate child) {
054: return this ;
055: }
056:
057: public void clearCaches() {
058: }
059: };
060:
061: public boolean supportsScript(String language) {
062: return getScriptEngine().supportsScriptLanguage(language);
063: }
064:
065: /**
066: * Executes the specified scripted event.
067: **/
068: public boolean doEvent(String eventScript) {
069: if (eventScript.length() == 0)
070: return true;
071: return getScriptEngine().performEvent(eventScript);
072: }
073:
074: /**
075: * Executes the specified script, returning any intended replacement text.
076: * @return the replacement text, which may be empty.
077: **/
078: public String runScript(String language, String script) {
079: return (script.length() == 0) ? "" : getScriptEngine()
080: .executeScript(language, script);
081: }
082:
083: /**
084: * Evaluates the specified javascript expression, returning its value.
085: **/
086: public String evaluateExpression(String urlString) {
087: if (urlString.length() == 0)
088: return null;
089: return getScriptEngine().evaluateScriptExpression(urlString);
090: }
091:
092: /**
093: * Returns the value of the named property. Will return null if the property does not exist.
094: **/
095: public Object get(String propertyName) {
096: return null;
097: }
098:
099: /**
100: * Returns the value of the index property. Will return null if the property does not exist.
101: **/
102: public Object get(int index) {
103: return null;
104: }
105:
106: /**
107: * Sets the value of the named property. Will throw a runtime exception if the property does not exist or
108: * cannot accept the specified value.
109: **/
110: public void set(String propertyName, Object value) {
111: throw new RuntimeException("No such property: " + propertyName);
112: }
113:
114: /**
115: * Specifies the scripting engine to be used.
116: */
117: public void setScriptEngine(ScriptingEngine scriptEngine) {
118: _scriptEngine = scriptEngine;
119: }
120:
121: public ScriptingEngine getScriptEngine() {
122: return _scriptEngine != null ? _scriptEngine
123: : NULL_SCRIPT_ENGINE;
124: }
125:
126: public ScriptingEngine getScriptEngine(ScriptableDelegate child) {
127: return getScriptEngine().newScriptingEngine(child);
128: }
129:
130: protected ScriptableDelegate[] getDelegates(
131: final HTMLElement[] elements) {
132: ScriptableDelegate[] result = new ScriptableDelegate[elements.length];
133: for (int i = 0; i < elements.length; i++) {
134: result[i] = elements[i].getScriptableDelegate();
135: }
136: return result;
137: }
138:
139: }
|