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;
16  
17  import java.io.Serializable;
18  
19  import org.htmlunit.util.MimeType;
20  
21  /**
22   * A collection of constants that represent the various ways a form can be encoded when submitted.
23   *
24   * @author Brad Clarke
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public final class FormEncodingType implements Serializable {
29  
30      /** URL-encoded form encoding. */
31      public static final FormEncodingType URL_ENCODED = new FormEncodingType("application/x-www-form-urlencoded");
32  
33      /** Multipart form encoding (used to be a constant in HttpClient but it was deprecated with no alternative). */
34      public static final FormEncodingType MULTIPART = new FormEncodingType("multipart/form-data");
35  
36      /** text/plain. */
37      public static final FormEncodingType TEXT_PLAIN = new FormEncodingType(MimeType.TEXT_PLAIN);
38  
39      private final String name_;
40  
41      private FormEncodingType(final String name) {
42          name_ = name;
43      }
44  
45      /**
46       * Returns the name of this encoding type.
47       *
48       * @return the name of this encoding type
49       */
50      public String getName() {
51          return name_;
52      }
53  
54      /**
55       * Returns the constant that matches the specified name.
56       *
57       * @param name the name to search by
58       * @return the constant corresponding to the specified name, {@link #URL_ENCODED} if none match.
59       */
60      public static FormEncodingType getInstance(final String name) {
61          if (MULTIPART.getName().equalsIgnoreCase(name)) {
62              return MULTIPART;
63          }
64  
65          if (TEXT_PLAIN.getName().equalsIgnoreCase(name)) {
66              return TEXT_PLAIN;
67          }
68  
69          return URL_ENCODED;
70      }
71  
72      /**
73       * Returns a string representation of this object.
74       * @return a string representation of this object
75       */
76      @Override
77      public String toString() {
78          return "EncodingType[name=" + getName() + "]";
79      }
80  }