001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: HTMLElementBase.java,v 1.8 2004/10/29 00:41:24 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 org.w3c.dom.Node;
024: import com.meterware.httpunit.scripting.ScriptableDelegate;
025:
026: import java.util.List;
027: import java.util.ArrayList;
028:
029: /**
030: *
031: * @since 1.5.2
032: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
033: **/
034: abstract class HTMLElementBase implements HTMLElement {
035:
036: private Node _node;
037: private ScriptableDelegate _scriptable;
038: private List _supportedAttributes = new ArrayList();
039:
040: public String getID() {
041: return getAttribute("id");
042: }
043:
044: public String getClassName() {
045: return getAttribute("class");
046: }
047:
048: public String getTitle() {
049: return getAttribute("title");
050: }
051:
052: public String getName() {
053: return getAttribute("name");
054: }
055:
056: /**
057: * Returns a scriptable object which can act as a proxy for this control.
058: */
059: public ScriptableDelegate getScriptableDelegate() {
060: if (_scriptable == null) {
061: _scriptable = newScriptable();
062: _scriptable.setScriptEngine(getParentDelegate()
063: .getScriptEngine(_scriptable));
064: }
065: return _scriptable;
066: }
067:
068: /**
069: * Returns the text value of this block.
070: **/
071: public String getText() {
072: if (_node.getNodeType() == Node.TEXT_NODE) {
073: return _node.getNodeValue().trim();
074: } else if (_node == null || !_node.hasChildNodes()) {
075: return "";
076: } else {
077: return NodeUtils.asText(_node.getChildNodes()).trim();
078: }
079: }
080:
081: public String getTagName() {
082: return _node.getNodeName();
083: }
084:
085: protected HTMLElementBase(Node node) {
086: _node = node;
087: supportAttribute("id");
088: supportAttribute("class");
089: supportAttribute("title");
090: supportAttribute("name");
091: }
092:
093: public String getAttribute(final String name) {
094: return NodeUtils.getNodeAttribute(getNode(), name);
095: }
096:
097: public boolean isSupportedAttribute(String name) {
098: return _supportedAttributes.contains(name);
099: }
100:
101: protected String getAttribute(final String name, String defaultValue) {
102: return NodeUtils
103: .getNodeAttribute(getNode(), name, defaultValue);
104: }
105:
106: protected Node getNode() {
107: return _node;
108: }
109:
110: protected void supportAttribute(String name) {
111: _supportedAttributes.add(name);
112: }
113:
114: /**
115: * Creates and returns a scriptable object for this control. Subclasses should override this if they use a different
116: * implementation of Scriptable.
117: */
118: protected ScriptableDelegate newScriptable() {
119: return new HTMLElementScriptable(this );
120: }
121:
122: /**
123: * Returns the scriptable delegate which can provide the scriptable delegate for this element.
124: */
125: abstract protected ScriptableDelegate getParentDelegate();
126:
127: }
|