001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.io.IOException;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: import com.gargoylesoftware.htmlunit.Page;
045:
046: /**
047: * Wrapper for the html element "img".
048: *
049: * @version $Revision: 2132 $
050: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
051: * @author David K. Taylor
052: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
053: * @author Ahmed Ashour
054: */
055: public class HtmlImage extends ClickableElement {
056:
057: private static final long serialVersionUID = -2304247017681577696L;
058:
059: /** the HTML tag represented by this element */
060: public static final String TAG_NAME = "img";
061: private int lastClickX_;
062: private int lastClickY_;
063:
064: /**
065: * Create an instance of HtmlImage
066: *
067: * @param page The HtmlPage that contains this element.
068: * @param attributes the initial attributes
069: * @deprecated You should not directly construct HtmlImage.
070: */
071: //TODO: to be removed, deprecated after 1.11
072: public HtmlImage(final HtmlPage page, final Map attributes) {
073: this (null, TAG_NAME, page, attributes);
074: }
075:
076: /**
077: * Create an instance of HtmlImage
078: *
079: * @param namespaceURI the URI that identifies an XML namespace.
080: * @param qualifiedName The qualified name of the element type to instantiate
081: * @param page The HtmlPage that contains this element.
082: * @param attributes the initial attributes
083: */
084: HtmlImage(final String namespaceURI, final String qualifiedName,
085: final HtmlPage page, final Map attributes) {
086: super (namespaceURI, qualifiedName, page, attributes);
087: }
088:
089: /**
090: * Return the value of the attribute "src". Refer to the
091: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
092: * documentation for details on the use of this attribute.
093: *
094: * @return The value of the attribute "src"
095: * or an empty string if that attribute isn't defined.
096: */
097: public final String getSrcAttribute() {
098: return getAttributeValue("src");
099: }
100:
101: /**
102: * Return the value of the attribute "alt". Refer to the
103: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
104: * documentation for details on the use of this attribute.
105: *
106: * @return The value of the attribute "alt"
107: * or an empty string if that attribute isn't defined.
108: */
109: public final String getAltAttribute() {
110: return getAttributeValue("alt");
111: }
112:
113: /**
114: * Return the value of the attribute "name". Refer to the
115: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
116: * documentation for details on the use of this attribute.
117: *
118: * @return The value of the attribute "name"
119: * or an empty string if that attribute isn't defined.
120: */
121: public final String getNameAttribute() {
122: return getAttributeValue("name");
123: }
124:
125: /**
126: * Return the value of the attribute "longdesc". Refer to the
127: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
128: * documentation for details on the use of this attribute.
129: *
130: * @return The value of the attribute "longdesc"
131: * or an empty string if that attribute isn't defined.
132: */
133: public final String getLongDescAttribute() {
134: return getAttributeValue("longdesc");
135: }
136:
137: /**
138: * Return the value of the attribute "height". Refer to the
139: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
140: * documentation for details on the use of this attribute.
141: *
142: * @return The value of the attribute "height"
143: * or an empty string if that attribute isn't defined.
144: */
145: public final String getHeightAttribute() {
146: return getAttributeValue("height");
147: }
148:
149: /**
150: * Return the value of the attribute "width". Refer to the
151: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
152: * documentation for details on the use of this attribute.
153: *
154: * @return The value of the attribute "width"
155: * or an empty string if that attribute isn't defined.
156: */
157: public final String getWidthAttribute() {
158: return getAttributeValue("width");
159: }
160:
161: /**
162: * Return the value of the attribute "usemap". Refer to the
163: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
164: * documentation for details on the use of this attribute.
165: *
166: * @return The value of the attribute "usemap"
167: * or an empty string if that attribute isn't defined.
168: */
169: public final String getUseMapAttribute() {
170: return getAttributeValue("usemap");
171: }
172:
173: /**
174: * Return the value of the attribute "ismap". Refer to the
175: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
176: * documentation for details on the use of this attribute.
177: *
178: * @return The value of the attribute "ismap"
179: * or an empty string if that attribute isn't defined.
180: */
181: public final String getIsmapAttribute() {
182: return getAttributeValue("ismap");
183: }
184:
185: /**
186: * Return the value of the attribute "align". Refer to the
187: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
188: * documentation for details on the use of this attribute.
189: *
190: * @return The value of the attribute "align"
191: * or an empty string if that attribute isn't defined.
192: */
193: public final String getAlignAttribute() {
194: return getAttributeValue("align");
195: }
196:
197: /**
198: * Return the value of the attribute "border". Refer to the
199: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
200: * documentation for details on the use of this attribute.
201: *
202: * @return The value of the attribute "border"
203: * or an empty string if that attribute isn't defined.
204: */
205: public final String getBorderAttribute() {
206: return getAttributeValue("border");
207: }
208:
209: /**
210: * Return the value of the attribute "hspace". Refer to the
211: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
212: * documentation for details on the use of this attribute.
213: *
214: * @return The value of the attribute "hspace"
215: * or an empty string if that attribute isn't defined.
216: */
217: public final String getHspaceAttribute() {
218: return getAttributeValue("hspace");
219: }
220:
221: /**
222: * Return the value of the attribute "vspace". Refer to the
223: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
224: * documentation for details on the use of this attribute.
225: *
226: * @return The value of the attribute "vspace"
227: * or an empty string if that attribute isn't defined.
228: */
229: public final String getVspaceAttribute() {
230: return getAttributeValue("vspace");
231: }
232:
233: /**
234: * Simulate clicking this element at the given position.
235: * This makes only sense for an image map (currently only server side) where the position matters.
236: *
237: * @param x the x position of the click
238: * @param y the y position of the click
239: * @return The page that occupies this window after this element is
240: * clicked. It may be the same window or it may be a freshly loaded one.
241: * @exception IOException If an IO error occurs
242: */
243: public Page click(final int x, final int y) throws IOException {
244:
245: lastClickX_ = x;
246: lastClickY_ = y;
247: return super .click();
248: }
249:
250: /**
251: * Simulate clicking this element.
252: *
253: * @return The page that occupies this window after this element is
254: * clicked. It may be the same window or it may be a freshly loaded one.
255: * @exception IOException If an IO error occurs
256: */
257: public Page click() throws IOException {
258: return click(0, 0);
259: }
260:
261: /**
262: * Performs the click action on the enclosing A tag (if any).
263: * @param defaultPage The default page to return if the action does not load a new page.
264: * @return The page that is currently loaded after execution of this method
265: * @throws IOException If an IO error occurred
266: */
267: protected Page doClickAction(final Page defaultPage)
268: throws IOException {
269: if (getUseMapAttribute() != ATTRIBUTE_NOT_DEFINED) {
270: // remove initial '#'
271: final String mapName = getUseMapAttribute().substring(1);
272: final HtmlElement doc = getPage().getDocumentHtmlElement();
273: final HtmlMap map = (HtmlMap) doc
274: .getOneHtmlElementByAttribute("map", "name",
275: mapName);
276: for (final Iterator it = map.getChildElementsIterator(); it
277: .hasNext();) {
278: final HtmlElement element = (HtmlElement) it.next();
279: if (element instanceof HtmlArea) {
280: final HtmlArea area = (HtmlArea) element;
281: if (area.containsPoint(lastClickX_, lastClickY_)) {
282: return area.doClickAction(defaultPage);
283: }
284: }
285: }
286: return super .doClickAction(defaultPage);
287: } else {
288: final HtmlAnchor anchor = (HtmlAnchor) getEnclosingElement("a");
289: if (anchor == null) {
290: return super .doClickAction(defaultPage);
291: } else {
292: if (getIsmapAttribute() != ATTRIBUTE_NOT_DEFINED) {
293: final String suffix = "?" + lastClickX_ + ","
294: + lastClickY_;
295: return anchor.doClickAction(defaultPage, suffix);
296: } else {
297: return anchor.doClickAction(defaultPage);
298: }
299: }
300: }
301: }
302:
303: }
|