KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > HtmlScript


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.html;
39
40 import java.util.Map JavaDoc;
41
42
43 /**
44  * Wrapper for the html element "script".<br>
45  * When a script tag references an externat script (with attribute src) it gets executed when the node
46  * is added to the DOM tree. When the script code is nested, it gets executed when the text node
47  * containing the script is added to the HtmlScript.<br>
48  * The ScriptFilter feature of NekoHtml can't be used because it doesn't allow immediate access to the DOM
49  * (ie <code>document.write("&lt;span id='mySpan'/>"); document.getElementById("mySpan").tagName;</code>
50  * can't work with a filter).
51  * @version $Revision: 100 $
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
54  * @author Marc Guillemot
55  * @see <a HREF="http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-81598695">
56  * DOM Level 1</a>
57  * @see <a HREF="http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-81598695">
58  * DOM Level 2</a>
59  */

60 public class HtmlScript extends HtmlElement {
61
62     /** the HTML tag represented by this element */
63     public static final String JavaDoc TAG_NAME = "script";
64     private static int EventHandlerId_;
65
66     /**
67      * Create an instance of HtmlScript
68      *
69      * @param page The HtmlPage that contains this element.
70      * @param attributes the initial attributes
71      */

72     public HtmlScript( final HtmlPage page, final Map JavaDoc attributes ) {
73         super(page, attributes);
74     }
75
76     /**
77      * @return the HTML tag name
78      */

79     public String JavaDoc getTagName() {
80         return TAG_NAME;
81     }
82
83     /**
84      * Return the value of the attribute "charset". Refer to the
85      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
86      * documentation for details on the use of this attribute.
87      *
88      * @return The value of the attribute "charset"
89      * or an empty string if that attribute isn't defined.
90      */

91     public final String JavaDoc getCharsetAttribute() {
92         return getAttributeValue("charset");
93     }
94
95
96     /**
97      * Return the value of the attribute "type". Refer to the
98      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
99      * documentation for details on the use of this attribute.
100      *
101      * @return The value of the attribute "type"
102      * or an empty string if that attribute isn't defined.
103      */

104     public final String JavaDoc getTypeAttribute() {
105         return getAttributeValue("type");
106     }
107
108
109     /**
110      * Return the value of the attribute "language". Refer to the
111      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
112      * documentation for details on the use of this attribute.
113      *
114      * @return The value of the attribute "language"
115      * or an empty string if that attribute isn't defined.
116      */

117     public final String JavaDoc getLanguageAttribute() {
118         return getAttributeValue("language");
119     }
120
121
122     /**
123      * Return the value of the attribute "src". Refer to the
124      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
125      * documentation for details on the use of this attribute.
126      *
127      * @return The value of the attribute "src"
128      * or an empty string if that attribute isn't defined.
129      */

130     public final String JavaDoc getSrcAttribute() {
131         return getAttributeValue("src");
132     }
133
134     /**
135      * Return the value of the attribute "event".
136      * @return The value of the attribute "event"
137      */

138     public final String JavaDoc getEventAttribute() {
139         return getAttributeValue("event");
140     }
141
142     /**
143      * Return the value of the attribute "for".
144      * @return The value of the attribute "for"
145      */

146     public final String JavaDoc getHtmlForAttribute() {
147         return getAttributeValue("for");
148     }
149
150     /**
151      * Return the value of the attribute "defer". Refer to the
152      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
153      * documentation for details on the use of this attribute.
154      *
155      * @return The value of the attribute "defer"
156      * or an empty string if that attribute isn't defined.
157      */

158     public final String JavaDoc getDeferAttribute() {
159         return getAttributeValue("defer");
160     }
161     
162     /**
163      * Executes the content as a script if it is a text node
164      * @see com.gargoylesoftware.htmlunit.html.DomNode#appendChild(com.gargoylesoftware.htmlunit.html.DomNode)
165      */

166     public DomNode appendChild(final DomNode node) {
167         final DomNode response = super.appendChild(node);
168         executeScriptIfNeeded();
169         return response;
170     }
171
172     /**
173      * For internal use only
174      */

175     void executeScriptIfNeeded() {
176         final HtmlPage page = getPage();
177         
178         if (!page.getWebClient().isJavaScriptEnabled()) {
179             return;
180         }
181         if (!HtmlPage.isJavaScript(getTypeAttribute(), getLanguageAttribute())) {
182             getLog().debug("Script is not javascript. Skipping execution.");
183             return;
184         }
185
186         if (getSrcAttribute() != HtmlElement.ATTRIBUTE_NOT_DEFINED) {
187             getLog().debug("Loading external javascript: " + getSrcAttribute());
188             page.loadExternalJavaScriptFile(getSrcAttribute(), getCharsetAttribute());
189         }
190         else if (getFirstChild() != null) {
191             final DomCharacterData textNode = (DomCharacterData) getFirstChild();
192             final String JavaDoc scriptCode;
193             if (getEventAttribute() != ATTRIBUTE_NOT_DEFINED
194                     && getHtmlForAttribute() != ATTRIBUTE_NOT_DEFINED) {
195                 // event name can be like "onload" or "onload()"
196
String JavaDoc eventName = getEventAttribute();
197                 if (eventName.endsWith("()")) {
198                     eventName = eventName.substring(0, eventName.length()-2);
199                 }
200                 final String JavaDoc scriptEventHandler = getHtmlForAttribute() + "." + eventName;
201                 final String JavaDoc evhName = "htmlunit_evh_JJLL" + EventHandlerId_;
202                 scriptCode = "function " + evhName + "()\n{"
203                     + textNode.getData() + "}\n"
204                     + scriptEventHandler + "=" + evhName + ";";
205             }
206             else {
207                 scriptCode = textNode.getData();
208             }
209             getPage().executeJavaScriptIfPossible(scriptCode, "Embedded script", false, null);
210         }
211     }
212 }
213
Popular Tags