KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
41 import java.net.MalformedURLException JavaDoc;
42 import java.net.URL JavaDoc;
43 import java.util.Map JavaDoc;
44
45 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
46 import com.gargoylesoftware.htmlunit.Page;
47 import com.gargoylesoftware.htmlunit.WebClient;
48 import com.gargoylesoftware.htmlunit.WebRequestSettings;
49 import com.gargoylesoftware.htmlunit.WebWindow;
50
51 /**
52  * Base class for frame and iframe.
53  *
54  * @version $Revision: 100 $
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  * @author David K. Taylor
57  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
58  * @author Marc Guillemot
59  * @author David D. Kilzer
60  */

61 public abstract class BaseFrame extends StyledElement {
62
63     /**
64      * The web window for a frame or iframe.
65      */

66     public final class FrameWindow implements WebWindow {
67         private Page enclosedPage_;
68         private Object JavaDoc scriptObject_;
69         
70         /**
71          * Private constructor as instanciation is not allowed directly.
72          */

73         private FrameWindow() {
74             // nothing to do
75
}
76
77         /**
78          * Return the name of this window.
79          *
80          * @return The name of this window.
81          */

82         public String JavaDoc getName() {
83             return getNameAttribute();
84         }
85
86         /**
87          * Set the name of this window.
88          *
89          * @param name The new name of this window.
90          */

91         public void setName(final String JavaDoc name) {
92             setNameAttribute(name);
93         }
94
95         /**
96          * Return the currently loaded page or null if no page has been loaded.
97          *
98          * @return The currently loaded page or null if no page has been loaded.
99          */

100         public Page getEnclosedPage() {
101             return enclosedPage_;
102         }
103
104         /**
105          * Set the currently loaded page.
106          *
107          * @param page The new page or null if there is no page (ie empty window)
108          */

109         public void setEnclosedPage( final Page page ) {
110             enclosedPage_ = page;
111         }
112
113         /**
114          * Return the window that contains this window.
115          *
116          * @return The parent window.
117          */

118         public WebWindow getParentWindow() {
119             return getPage().getEnclosingWindow();
120         }
121
122         /**
123          * Return the top level window that contains this window.
124          *
125          * @return The top level window that contains this window.
126          */

127         public WebWindow getTopWindow() {
128             return getParentWindow().getTopWindow();
129         }
130
131         /**
132          * Return the web client that "owns" this window.
133          *
134          * @return The web client or null if this window has been closed.
135          */

136         public WebClient getWebClient() {
137             return getPage().getWebClient();
138         }
139
140         /**
141          * Internal use only - subject to change without notice.<p>
142          * Return the javascript object that corresponds to this window.
143          * @return The javascript object that corresponds to this window.
144          */

145         public Object JavaDoc getScriptObject() {
146             return scriptObject_;
147         }
148
149         /**
150          * Internal use only - subject to change without notice.<p>
151          * Set the javascript object that corresponds to this node. This is not
152          * guarenteed to be set.
153          * @param scriptObject The javascript object.
154          */

155         public void setScriptObject( final Object JavaDoc scriptObject ) {
156             scriptObject_ = scriptObject;
157         }
158         
159         /**
160          * Return the html page in wich the &lt;frame&gt; or &lt;iframe&gt; tag is contained
161          * for this frame window.
162          * This is a facility method for <code>(HtmlPage) (getParentWindow().getEnclosedPage())</code>.
163          * @return the page in the parent window.
164          */

165         public HtmlPage getEnclosingPage() {
166             return getPage();
167         }
168     }
169
170     private final WebWindow enclosedWindow_ = new FrameWindow();
171
172     /**
173      * Create an instance of BaseFrame
174      *
175      * @param page The HtmlPage that contains this element.
176      * @param attributes the initial attributes
177      */

178     protected BaseFrame( final HtmlPage page, final Map JavaDoc attributes) {
179
180         super(page, attributes);
181
182         final WebClient webClient = page.getWebClient();
183         webClient.registerWebWindow(enclosedWindow_);
184     }
185
186
187     /**
188      * Called after the node for <frame...> or <iframe> has been added to the containing page.
189      * The node needs to be added first to allow js in the frame to see the frame in the parent
190      */

191     void loadInnerPage() {
192         String JavaDoc source = getSrcAttribute();
193         if( source.length() == 0 ) {
194             // Nothing to load
195
source = "about:blank";
196         }
197
198         loadInnerPageIfPossible(source);
199     }
200
201     private void loadInnerPageIfPossible(final String JavaDoc srcAttribute) {
202
203         if( srcAttribute.length() != 0 ) {
204             URL JavaDoc url = null;
205             try {
206                 url = getPage().getFullyQualifiedUrl(srcAttribute);
207             }
208             catch( final MalformedURLException JavaDoc e ) {
209                 getLog().error("Bad url in src attribute of " + getTagName() + ": url=["+srcAttribute+"]", e);
210             }
211             try {
212                 getPage().getWebClient().getPage(enclosedWindow_, new WebRequestSettings(url));
213             }
214             catch (final FailingHttpStatusCodeException e){
215                 // do nothing
216
}
217             catch( final IOException JavaDoc e ) {
218                 getLog().error("IOException when getting content for " + getTagName()
219                         + ": url=["+url.toExternalForm()+"]", e);
220             }
221         }
222     }
223
224     /**
225      * @return the HTML tag name
226      */

227     public abstract String JavaDoc getTagName();
228
229     /**
230      * Return the value of the attribute "longdesc". Refer to the
231      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
232      * documentation for details on the use of this attribute.
233      *
234      * @return The value of the attribute "longdesc"
235      * or an empty string if that attribute isn't defined.
236      */

237     public final String JavaDoc getLongDescAttribute() {
238         return getAttributeValue("longdesc");
239     }
240
241
242     /**
243      * Return the value of the attribute "name". Refer to the
244      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
245      * documentation for details on the use of this attribute.
246      *
247      * @return The value of the attribute "name"
248      * or an empty string if that attribute isn't defined.
249      */

250     public final String JavaDoc getNameAttribute() {
251         return getAttributeValue("name");
252     }
253
254
255     /**
256      * Set the value of the "name" attribute.
257      *
258      * @param name The new window name.
259      */

260     public final void setNameAttribute(final String JavaDoc name) {
261         setAttributeValue("name", name);
262     }
263
264
265     /**
266      * Return the value of the attribute "src". Refer to the
267      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
268      * documentation for details on the use of this attribute.
269      *
270      * @return The value of the attribute "src"
271      * or an empty string if that attribute isn't defined.
272      */

273     public final String JavaDoc getSrcAttribute() {
274         return getAttributeValue("src");
275     }
276
277
278     /**
279      * Return the value of the attribute "frameborder". Refer to the
280      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
281      * documentation for details on the use of this attribute.
282      *
283      * @return The value of the attribute "frameborder"
284      * or an empty string if that attribute isn't defined.
285      */

286     public final String JavaDoc getFrameBorderAttribute() {
287         return getAttributeValue("frameborder");
288     }
289
290
291     /**
292      * Return the value of the attribute "marginwidth". Refer to the
293      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
294      * documentation for details on the use of this attribute.
295      *
296      * @return The value of the attribute "marginwidth"
297      * or an empty string if that attribute isn't defined.
298      */

299     public final String JavaDoc getMarginWidthAttribute() {
300         return getAttributeValue("marginwidth");
301     }
302
303
304     /**
305      * Return the value of the attribute "marginheight". Refer to the
306      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
307      * documentation for details on the use of this attribute.
308      *
309      * @return The value of the attribute "marginheight"
310      * or an empty string if that attribute isn't defined.
311      */

312     public final String JavaDoc getMarginHeightAttribute() {
313         return getAttributeValue("marginheight");
314     }
315
316
317     /**
318      * Return the value of the attribute "noresize". Refer to the
319      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
320      * documentation for details on the use of this attribute.
321      *
322      * @return The value of the attribute "noresize"
323      * or an empty string if that attribute isn't defined.
324      */

325     public final String JavaDoc getNoResizeAttribute() {
326         return getAttributeValue("noresize");
327     }
328
329
330     /**
331      * Return the value of the attribute "scrolling". Refer to the
332      * <a HREF='http://www.w3.org/TR/html401/'>HTML 4.01</a>
333      * documentation for details on the use of this attribute.
334      *
335      * @return The value of the attribute "scrolling"
336      * or an empty string if that attribute isn't defined.
337      */

338     public final String JavaDoc getScrollingAttribute() {
339         return getAttributeValue("scrolling");
340     }
341
342
343     /**
344      * Return the value of the attribute "onload". This attribute is not
345      * actually supported by the HTML specification however it is supported
346      * by the popular browsers.
347      *
348      * @return The value of the attribute "onload"
349      * or an empty string if that attribute isn't defined.
350      */

351     public final String JavaDoc getOnLoadAttribute() {
352         return getAttributeValue("onload");
353     }
354
355
356     /**
357      * Return the currently loaded page in the enclosed window.
358      * This is a facility method for <code>getEnclosedWindow().getEnclosedPage()</code>.
359      * @see WebWindow#getEnclosedPage()
360      * @return The currently loaded page in the enclosed window or null if no page has been loaded.
361      */

362     public Page getEnclosedPage() {
363         return getEnclosedWindow().getEnclosedPage();
364     }
365     
366     
367     /**
368      * Gets the window enclosed in this frame.
369      * @return the window
370      */

371     public WebWindow getEnclosedWindow() {
372         return enclosedWindow_;
373     }
374
375
376     /**
377      * Set the value of the "src" attribute. Also load the frame with the specified url if possible.
378      * @param attribute The new value
379      */

380     public final void setSrcAttribute( final String JavaDoc attribute ) {
381         setAttributeValue("src", attribute);
382         loadInnerPageIfPossible(attribute);
383     }
384 }
385
Popular Tags