001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: WebImage.java,v 1.13 2004/10/13 15:11:51 russgold Exp $
005: *
006: * Copyright (c) 2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import com.meterware.httpunit.scripting.ScriptableDelegate;
024: import com.meterware.httpunit.scripting.NamedDelegate;
025:
026: import java.net.URL;
027:
028: import org.w3c.dom.Node;
029:
030: /**
031: * Represents an image in an HTML document.
032: *
033: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
034: **/
035: public class WebImage extends FixedURLWebRequestSource {
036:
037: private Node _node;
038: private ParsedHTML _parsedHTML;
039: private Scriptable _scriptable;
040: private String _src;
041: private String _alt;
042:
043: WebImage(WebResponse response, ParsedHTML parsedHTML, URL baseURL,
044: Node node, FrameSelector sourceFrame, String defaultTarget,
045: String characterSet) {
046: super (response, node, baseURL, NodeUtils.getNodeAttribute(node,
047: "src"), sourceFrame, defaultTarget, characterSet);
048: _node = node;
049: _parsedHTML = parsedHTML;
050: _src = NodeUtils.getNodeAttribute(_node, "src");
051: _alt = NodeUtils.getNodeAttribute(_node, "alt");
052: }
053:
054: public String getName() {
055: return NodeUtils.getNodeAttribute(_node, "name");
056: }
057:
058: public String getSource() {
059: return _src;
060: }
061:
062: public String getAltText() {
063: return _alt;
064: }
065:
066: public WebLink getLink() {
067: return _parsedHTML.getFirstMatchingLink(
068: new HTMLElementPredicate() {
069:
070: public boolean matchesCriteria(Object link,
071: Object parentNode) {
072: for (Node parent = (Node) parentNode; parent != null; parent = parent
073: .getParentNode()) {
074: if (parent.equals(((WebLink) link)
075: .getNode()))
076: return true;
077: }
078: return false;
079: }
080: }, _node.getParentNode());
081: }
082:
083: /**
084: * Returns an object which provides scripting access to this link.
085: **/
086: public Scriptable getScriptableObject() {
087: if (_scriptable == null) {
088: _scriptable = new Scriptable();
089: _scriptable.setScriptEngine(getBaseResponse()
090: .getScriptableObject().getDocument()
091: .getScriptEngine(_scriptable));
092: }
093: return _scriptable;
094: }
095:
096: public class Scriptable extends HTMLElementScriptable implements
097: NamedDelegate {
098:
099: public Scriptable() {
100: super (WebImage.this );
101: }
102:
103: public String getName() {
104: return WebImage.this .getID().length() != 0 ? WebImage.this
105: .getID() : WebImage.this .getName();
106: }
107:
108: public Object get(String propertyName) {
109: if (propertyName.equalsIgnoreCase("src")) {
110: return getSource();
111: } else if (propertyName.equalsIgnoreCase("name")) {
112: return getName();
113: } else {
114: return super .get(propertyName);
115: }
116: }
117:
118: public void set(String propertyName, Object value) {
119: if (propertyName.equalsIgnoreCase("src")) {
120: if (value != null)
121: _src = value.toString();
122: } else {
123: super .set(propertyName, value);
124: }
125: }
126: }
127:
128: //---------------------------------- WebRequestSource methods ------------------------------------------
129:
130: /**
131: * Returns the scriptable delegate.
132: */
133:
134: public ScriptableDelegate getScriptableDelegate() {
135: return getScriptableObject();
136: }
137: }
|