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.io.PrintWriter;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: import org.apache.commons.lang.StringEscapeUtils;
046:
047: import com.gargoylesoftware.htmlunit.KeyValuePair;
048: import com.gargoylesoftware.htmlunit.Page;
049:
050: /**
051: * Wrapper for the HTML element "input".
052: *
053: * @version $Revision: 2132 $
054: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
055: * @author David K. Taylor
056: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
057: * @author Daniel Gredler
058: * @author Ahmed Ashour
059: */
060: public class HtmlSubmitInput extends HtmlInput {
061:
062: private static final long serialVersionUID = -615974535731910492L;
063:
064: /**
065: * Value to use if no specified <tt>value</tt> attribute.
066: */
067: private static final String DEFAULT_VALUE = "Submit Query";
068:
069: /**
070: * Create an instance
071: *
072: * @param page The page that contains this element
073: * @param attributes the initial attributes
074: * @deprecated You should not directly construct HtmlSubmitInput.
075: */
076: //TODO: to be removed, deprecated after 1.11
077: public HtmlSubmitInput(final HtmlPage page, final Map attributes) {
078: this (null, TAG_NAME, page, attributes);
079: }
080:
081: /**
082: * Create an instance
083: *
084: * @param namespaceURI the URI that identifies an XML namespace.
085: * @param qualifiedName The qualified name of the element type to instantiate
086: * @param page The page that contains this element
087: * @param attributes the initial attributes
088: */
089: HtmlSubmitInput(final String namespaceURI,
090: final String qualifiedName, final HtmlPage page,
091: final Map attributes) {
092: super (namespaceURI, qualifiedName, page, attributes);
093: if (getPage().getWebClient().getBrowserVersion().isIE()
094: && !isAttributeDefined("value")) {
095: setAttributeValue("value", DEFAULT_VALUE);
096: }
097: }
098:
099: /**
100: * This method will be called if there either wasn't an onclick handler or there was
101: * but the result of that handler was true. This is the default behavior of clicking
102: * the element. The default implementation returns the current page - subclasses
103: * requiring different behavior (like {@link HtmlSubmitInput}) will override this
104: * method.
105: *
106: * @param defaultPage The default page to return if the action does not
107: * load a new page.
108: * @return The page that is currently loaded after execution of this method
109: * @throws IOException If an IO error occurred
110: */
111: protected Page doClickAction(final Page defaultPage)
112: throws IOException {
113: final HtmlForm form = getEnclosingForm();
114: if (form != null) {
115: return form.submit(this );
116: } else {
117: return super .doClickAction(defaultPage);
118: }
119: }
120:
121: /**
122: * {@inheritDoc} This method <b>does nothing</b> for submit input elements.
123: * @see SubmittableElement#reset()
124: */
125: public void reset() {
126: // Empty.
127: }
128:
129: /**
130: * {@inheritDoc} Returns "Submit Query" if <tt>value</tt> attribute is not defined.
131: */
132: public String asText() {
133: String text = super .asText();
134: if (text == ATTRIBUTE_NOT_DEFINED) {
135: text = DEFAULT_VALUE;
136: }
137: return text;
138: }
139:
140: /**
141: * {@inheritDoc} Doesn't print the attribute if it is <tt>value="Submit Query"</tt>.
142: */
143: protected void printOpeningTagContentAsXml(
144: final PrintWriter printWriter) {
145: printWriter.print(getTagName());
146:
147: for (final Iterator it = getAttributeEntriesIterator(); it
148: .hasNext();) {
149: final HtmlAttr attribute = (HtmlAttr) it.next();
150: if (!attribute.getNodeName().equals("value")
151: || !attribute.getHtmlValue().equals(DEFAULT_VALUE)) {
152: printWriter.print(" ");
153: final String name = attribute.getNodeName();
154: printWriter.print(name);
155: printWriter.print("=\"");
156: printWriter.print(StringEscapeUtils.escapeXml(attribute
157: .getNodeValue()));
158: printWriter.print("\"");
159: }
160: }
161: }
162:
163: /**
164: * {@inheritDoc}
165: *
166: * Returns "Submit Query" if <tt>name</tt> attribute is defined and <tt>value</tt> attribute is not defined.
167: */
168: public KeyValuePair[] getSubmitKeyValuePairs() {
169: if (getNameAttribute().length() != 0
170: && !isAttributeDefined("value")) {
171: return new KeyValuePair[] { new KeyValuePair(
172: getNameAttribute(), DEFAULT_VALUE) };
173: } else {
174: return super.getSubmitKeyValuePairs();
175: }
176: }
177: }
|