KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Attribute


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import com.gargoylesoftware.htmlunit.html.HtmlElement;
41 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
42
43 /**
44  * A JavaScript object for an Attribute.
45  *
46  * @see <a HREF="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-63764602">W3C DOM Level 2</a>
47  * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/attribute.asp">MSDN documentation</a>
48  * @version $Revision: 100 $
49  * @author Daniel Gredler
50  * @author Chris Erskine
51  */

52 public class Attribute extends SimpleScriptable {
53
54     private static final long serialVersionUID = 3256441425892750900L;
55
56     /**
57      * The name of the JavaScript object corresponding to this class as registered
58      * by the JavaScript context.
59      */

60     public static final String JavaDoc JS_OBJECT_NAME = "Attribute";
61
62     /**
63      * The name of this attribute.
64      */

65     private String JavaDoc name_;
66
67     /**
68      * The value of this attribute, used only when this attribute is detached from
69      * a parent HTML element (<tt>parent_</tt> is <tt>null</tt>).
70      */

71     private String JavaDoc value_;
72
73     /**
74      * The HTML element to which this attribute belongs. May be <tt>null</tt> if
75      * document.createAttribute() has been called but element.setAttributeNode()
76      * has not been called yet, or if document.setAttributeNode() has been called
77      * and this is the replaced attribute returned by said method.
78      */

79     private HtmlElement parent_;
80
81     /**
82      * Create an instance. Javascript objects must have a default constructor.
83      */

84     public Attribute() {}
85
86     /**
87      * Initializes this attribute.
88      * @param name the name of the attribute.
89      * @param parent the parent html element.
90      */

91     public void init(final String JavaDoc name, final HtmlElement parent) {
92         name_ = name;
93         parent_ = parent;
94         if(parent_ == null) {
95             value_ = "";
96         }
97     }
98
99     /**
100      * Detaches this attribute from the parent HTML element after caching the attribute value.
101      */

102     public void detachFromParent() {
103         if(parent_ != null) {
104             value_ = parent_.getAttributeValue(name_);
105         }
106         parent_ = null;
107     }
108
109     /**
110      * Returns <tt>true</tt> if arbitrary properties can be added to this attribute.
111      * @return <tt>true</tt> if arbitrary properties can be added to this attribute.
112      */

113     public boolean jsxGet_expando() {
114         return true;
115     }
116
117     /**
118      * Returns <code>null</code>
119      * @return <code>null</code>
120      */

121     public Object JavaDoc jsxGet_firstChild() {
122         return null;
123     }
124
125     /**
126      * Returns <code>null</code>
127      * @return <code>null</code>
128      */

129     public Object JavaDoc jsxGet_lastChild() {
130         return null;
131     }
132
133     /**
134      * Returns the name of the attribute.
135      * @return the name of the attribute.
136      */

137     public String JavaDoc jsxGet_name() {
138         return name_;
139     }
140
141     /**
142      * Returns <code>null</code>
143      * @return <code>null</code>
144      */

145     public Object JavaDoc jsxGet_nextSibling() {
146         return null;
147     }
148
149     /**
150      * Returns the name of this attribute.
151      * @return the name of this attribute.
152      */

153     public String JavaDoc jsxGet_nodeName() {
154         return jsxGet_name();
155     }
156
157     /**
158      * Returns the type of DOM node this attribute represents.
159      * @return the type of DOM node this attribute represents.
160      */

161     public int jsxGet_nodeType() {
162         return 2;
163     }
164
165     /**
166      * Returns the value of this attribute.
167      * @return the value of this attribute.
168      */

169     public String JavaDoc jsxGet_nodeValue() {
170         return jsxGet_value();
171     }
172
173     /**
174      * Returns the containing document.
175      * @return the containing document.
176      */

177     public Object JavaDoc jsxGet_ownerDocument() {
178         if(parent_ != null) {
179             final SimpleScriptable documentScriptable = getScriptableFor(parent_.getPage());
180             return documentScriptable;
181         }
182         else {
183             return null;
184         }
185     }
186
187     /**
188      * Returns <code>null</code>
189      * @return <code>null</code>
190      */

191     public Object JavaDoc jsxGet_parentNode() {
192         return null;
193     }
194
195     /**
196      * Returns <code>null</code>
197      * @return <code>null</code>
198      */

199     public Object JavaDoc jsxGet_previousSibling() {
200         return null;
201     }
202
203     /**
204      * Returns <tt>true</tt> if this attribute has been specified.
205      * @return <tt>true</tt> if this attribute has been specified.
206      */

207     public boolean jsxGet_specified() {
208         return true;
209     }
210
211     /**
212      * Returns the value of this attribute.
213      * @return the value of this attribute.
214      */

215     public String JavaDoc jsxGet_value() {
216         if(parent_ != null) {
217             return parent_.getAttributeValue(name_);
218         }
219         else {
220             return value_;
221         }
222     }
223
224     /**
225      * Sets the value of this attribute.
226      * @param value the new value of this attribute.
227      */

228     public void jsxSet_value(final String JavaDoc value) {
229         if(parent_ != null) {
230             parent_.setAttributeValue(name_, value);
231         }
232         else {
233             value_ = value;
234         }
235     }
236 }
237
Popular Tags