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.awt.geom.Ellipse2D;
041: import java.awt.geom.GeneralPath;
042: import java.awt.geom.Rectangle2D;
043: import java.io.IOException;
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.util.Map;
047:
048: import org.apache.commons.lang.StringUtils;
049:
050: import com.gargoylesoftware.htmlunit.Page;
051: import com.gargoylesoftware.htmlunit.TextUtil;
052: import com.gargoylesoftware.htmlunit.WebClient;
053: import com.gargoylesoftware.htmlunit.WebRequestSettings;
054: import com.gargoylesoftware.htmlunit.WebWindow;
055:
056: /**
057: * Wrapper for the html element "area".
058: *
059: * @version $Revision: 2132 $
060: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
061: * @author David K. Taylor
062: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
063: * @author Marc Guillemot
064: * @author Ahmed Ashour
065: */
066: public class HtmlArea extends FocusableElement {
067:
068: private static final long serialVersionUID = 8933911141016200386L;
069:
070: /** the HTML tag represented by this element */
071: public static final String TAG_NAME = "area";
072:
073: /**
074: * Create an instance of HtmlArea
075: *
076: * @param page The HtmlPage that contains this element.
077: * @param attributes the initial attributes
078: * @deprecated You should not directly construct HtmlArea.
079: */
080: //TODO: to be removed, deprecated after 1.11
081: public HtmlArea(final HtmlPage page, final Map attributes) {
082: this (null, TAG_NAME, page, attributes);
083: }
084:
085: /**
086: * Create an instance of HtmlArea
087: *
088: * @param namespaceURI the URI that identifies an XML namespace.
089: * @param qualifiedName The qualified name of the element type to instantiate
090: * @param page The HtmlPage that contains this element.
091: * @param attributes the initial attributes
092: */
093: HtmlArea(final String namespaceURI, final String qualifiedName,
094: final HtmlPage page, final Map attributes) {
095: super (namespaceURI, qualifiedName, page, attributes);
096: }
097:
098: /**
099: * This method will be called if there either wasn't an onclick handler or there was
100: * but the result of that handler was true. This is the default behavior of clicking
101: * the element. The default implementation returns the current page - subclasses
102: * requiring different behavior (like {@link HtmlSubmitInput}) will override this
103: * method.
104: *
105: * @param defaultPage The default page to return if the action does not
106: * load a new page.
107: * @return The page that is currently loaded after execution of this method
108: * @throws IOException If an IO error occurred
109: */
110: protected Page doClickAction(final Page defaultPage)
111: throws IOException {
112: final HtmlPage enclosingPage = getPage();
113: final WebClient webClient = enclosingPage.getWebClient();
114:
115: final String href = getHrefAttribute();
116: if (href != null && href.length() > 0) {
117: final HtmlPage page = getPage();
118: if (TextUtil.startsWithIgnoreCase(href, "javascript:")) {
119: return page.executeJavaScriptIfPossible(href,
120: "javascript url", getStartLineNumber())
121: .getNewPage();
122: } else {
123: final URL url;
124: try {
125: url = enclosingPage
126: .getFullyQualifiedUrl(getHrefAttribute());
127: } catch (final MalformedURLException e) {
128: throw new IllegalStateException("Not a valid url: "
129: + getHrefAttribute());
130: }
131: final WebRequestSettings settings = new WebRequestSettings(
132: url);
133: final WebWindow webWindow = enclosingPage
134: .getEnclosingWindow();
135: return webClient.getPage(webWindow, enclosingPage
136: .getResolvedTarget(getTargetAttribute()),
137: settings);
138: }
139: } else {
140: return defaultPage;
141: }
142: }
143:
144: /**
145: * Return the value of the attribute "shape". Refer to the
146: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
147: * documentation for details on the use of this attribute.
148: *
149: * @return The value of the attribute "shape"
150: * or an empty string if that attribute isn't defined.
151: */
152: public final String getShapeAttribute() {
153: return getAttributeValue("shape");
154: }
155:
156: /**
157: * Return the value of the attribute "coords". Refer to the
158: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
159: * documentation for details on the use of this attribute.
160: *
161: * @return The value of the attribute "coords"
162: * or an empty string if that attribute isn't defined.
163: */
164: public final String getCoordsAttribute() {
165: return getAttributeValue("coords");
166: }
167:
168: /**
169: * Return the value of the attribute "href". Refer to the
170: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
171: * documentation for details on the use of this attribute.
172: *
173: * @return The value of the attribute "href"
174: * or an empty string if that attribute isn't defined.
175: */
176: public final String getHrefAttribute() {
177: return getAttributeValue("href");
178: }
179:
180: /**
181: * Return the value of the attribute "nohref". Refer to the
182: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
183: * documentation for details on the use of this attribute.
184: *
185: * @return The value of the attribute "nohref"
186: * or an empty string if that attribute isn't defined.
187: */
188: public final String getNoHrefAttribute() {
189: return getAttributeValue("nohref");
190: }
191:
192: /**
193: * Return the value of the attribute "alt". Refer to the
194: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
195: * documentation for details on the use of this attribute.
196: *
197: * @return The value of the attribute "alt"
198: * or an empty string if that attribute isn't defined.
199: */
200: public final String getAltAttribute() {
201: return getAttributeValue("alt");
202: }
203:
204: /**
205: * Return the value of the attribute "tabindex". Refer to the
206: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
207: * documentation for details on the use of this attribute.
208: *
209: * @return The value of the attribute "tabindex"
210: * or an empty string if that attribute isn't defined.
211: */
212: public final String getTabIndexAttribute() {
213: return getAttributeValue("tabindex");
214: }
215:
216: /**
217: * Return the value of the attribute "accesskey". Refer to the
218: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
219: * documentation for details on the use of this attribute.
220: *
221: * @return The value of the attribute "accesskey"
222: * or an empty string if that attribute isn't defined.
223: */
224: public final String getAccessKeyAttribute() {
225: return getAttributeValue("accesskey");
226: }
227:
228: /**
229: * Return the value of the attribute "onfocus". Refer to the
230: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
231: * documentation for details on the use of this attribute.
232: *
233: * @return The value of the attribute "onfocus"
234: * or an empty string if that attribute isn't defined.
235: */
236: public final String getOnFocusAttribute() {
237: return getAttributeValue("onfocus");
238: }
239:
240: /**
241: * Return the value of the attribute "onblur". Refer to the
242: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
243: * documentation for details on the use of this attribute.
244: *
245: * @return The value of the attribute "onblur"
246: * or an empty string if that attribute isn't defined.
247: */
248: public final String getOnBlurAttribute() {
249: return getAttributeValue("onblur");
250: }
251:
252: /**
253: * Return the value of the attribute "target". Refer to the
254: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
255: * documentation for details on the use of this attribute.
256: *
257: * @return The value of the attribute "target"
258: * or an empty string if that attribute isn't defined.
259: */
260: public final String getTargetAttribute() {
261: return getAttributeValue("target");
262: }
263:
264: /**
265: * Indicates if this area contains the give point
266: * @param x the x coordinate of the point
267: * @param y the y coordinate of the point
268: * @return <code>true</code> if the point is contained in this area
269: */
270: boolean containsPoint(final int x, final int y) {
271: final String shape = StringUtils.defaultIfEmpty(
272: getShapeAttribute(), "rect").toLowerCase();
273:
274: if ("default".equals(shape) && getCoordsAttribute() != null) {
275: return true;
276: } else if ("rect".equals(shape) && getCoordsAttribute() != null) {
277: final String[] coords = getCoordsAttribute().split(",");
278: final double leftX = Double.parseDouble(coords[0].trim());
279: final double topY = Double.parseDouble(coords[1].trim());
280: final double rightX = Double.parseDouble(coords[2].trim());
281: final double bottomY = Double.parseDouble(coords[3].trim());
282: final Rectangle2D rectangle = new Rectangle2D.Double(leftX,
283: topY, rightX - leftX + 1, bottomY - topY + 1);
284: if (rectangle.contains(x, y)) {
285: return true;
286: }
287: } else if ("circle".equals(shape)
288: && getCoordsAttribute() != null) {
289: final String[] coords = getCoordsAttribute().split(",");
290: final double centerX = Double.parseDouble(coords[0].trim());
291: final double centerY = Double.parseDouble(coords[1].trim());
292: final String radiusString = coords[2].trim();
293:
294: final int radius;
295: try {
296: radius = Integer.parseInt(radiusString);
297: } catch (final NumberFormatException nfe) {
298: throw new NumberFormatException("Circle radius of "
299: + radiusString + " is not yet implemented.");
300: }
301: final Ellipse2D ellipse = new Ellipse2D.Double(centerX
302: - radius / 2, centerY - radius / 2, radius, radius);
303: if (ellipse.contains(x, y)) {
304: return true;
305: }
306: } else if ("poly".equals(shape) && getCoordsAttribute() != null) {
307: final String[] coords = getCoordsAttribute().split(",");
308: final GeneralPath path = new GeneralPath();
309: for (int i = 0; i + 1 < coords.length; i += 2) {
310: if (i == 0) {
311: path.moveTo(Float.parseFloat(coords[i]), Float
312: .parseFloat(coords[i + 1]));
313: } else {
314: path.lineTo(Float.parseFloat(coords[i]), Float
315: .parseFloat(coords[i + 1]));
316: }
317: }
318: path.closePath();
319: if (path.contains(x, y)) {
320: return true;
321: }
322: }
323:
324: return false;
325: }
326: }
|