View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.io.IOException;
18  import java.io.PrintWriter;
19  import java.util.Map;
20  
21  import org.htmlunit.SgmlPage;
22  import org.htmlunit.util.NameValuePair;
23  import org.htmlunit.util.StringUtils;
24  
25  /**
26   * Wrapper for the HTML element "input".
27   *
28   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
29   * @author David K. Taylor
30   * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
31   * @author Daniel Gredler
32   * @author Ahmed Ashour
33   * @author Marc Guillemot
34   * @author Ronald Brill
35   * @author Frank Danek
36   */
37  public class HtmlSubmitInput extends HtmlInput implements LabelableElement {
38  
39      /**
40       * Value to use if no specified <code>value</code> attribute.
41       */
42      public static final String DEFAULT_VALUE = "Submit Query";
43  
44      /**
45       * Creates an instance.
46       *
47       * @param qualifiedName the qualified name of the element type to instantiate
48       * @param page the page that contains this element
49       * @param attributes the initial attributes
50       */
51      HtmlSubmitInput(final String qualifiedName, final SgmlPage page,
52              final Map<String, DomAttr> attributes) {
53          super(qualifiedName, page, attributes);
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
61          if (!isDisabled()) {
62              final HtmlForm form = getEnclosingForm();
63              if (form != null) {
64                  form.submit(this);
65                  return false;
66              }
67          }
68          super.doClickStateUpdate(shiftKey, ctrlKey);
69          return false;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void setValue(final String newValue) {
77          unmarkValueDirty();
78          setDefaultValue(newValue);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void setDefaultChecked(final boolean defaultChecked) {
86          // Empty.
87      }
88  
89      /**
90       * {@inheritDoc} This method <b>does nothing</b> for submit input elements.
91       * @see SubmittableElement#reset()
92       */
93      @Override
94      public void reset() {
95          // Empty.
96      }
97  
98      /**
99       * {@inheritDoc} Doesn't print the attribute if it is <code>value="Submit Query"</code>.
100      */
101     @Override
102     protected void printOpeningTagContentAsXml(final PrintWriter printWriter) {
103         printWriter.print(getTagName());
104 
105         for (final DomAttr attribute : getAttributesMap().values()) {
106             final String name = attribute.getNodeName();
107             final String value = attribute.getValue();
108             if (!VALUE_ATTRIBUTE.equals(name) || !DEFAULT_VALUE.equals(value)) {
109                 printWriter.print(" ");
110                 printWriter.print(name);
111                 printWriter.print("=\"");
112                 printWriter.print(StringUtils.escapeXmlAttributeValue(value));
113                 printWriter.print("\"");
114             }
115         }
116     }
117 
118     /**
119      * {@inheritDoc}
120      *
121      * Returns "Submit Query" if <code>name</code> attribute is defined and <code>value</code> attribute is not defined.
122      */
123     @Override
124     public NameValuePair[] getSubmitNameValuePairs() {
125         if (!getNameAttribute().isEmpty() && !hasAttribute(VALUE_ATTRIBUTE)) {
126             return new NameValuePair[]{new NameValuePair(getNameAttribute(), DEFAULT_VALUE)};
127         }
128         return super.getSubmitNameValuePairs();
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     protected boolean isRequiredSupported() {
136         return false;
137     }
138 }