2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.data;
36
import java.io.IOException;
37
import java.util.List;
39
import org.restlet.engine.util.FormUtils;
40
import org.restlet.representation.Representation;
41
import org.restlet.representation.StringRepresentation;
42
import org.restlet.util.Series;
45
* Form which is a specialized modifiable list of parameters.
48
* href="http://wiki.restlet.org/docs_2.0/13-restlet/27-restlet/330-restlet/58-restlet.html">User
49
* Guide - Getting parameter values</a>
50
* @author Jerome Louvel
52
public class Form extends Series<Parameter> {
63
* @param initialCapacity
64
* The initial list capacity.
66
public Form(int initialCapacity) {
67
super(initialCapacity);
76
public Form(List<Parameter> delegate) {
84
* The URL encoded Web form.
87
public Form(Representation webForm) {
88
FormUtils.parse(this, webForm);
92
* Constructor. Uses UTF-8 as the character set for encoding non-ASCII
96
* The Web form parameters as a string.
99
public Form(String queryString) {
100
this(queryString, CharacterSet.UTF_8);
104
* Constructor. Uses UTF-8 as the character set for encoding non-ASCII
107
* @param parametersString
108
* The parameters string to parse.
110
* The separator character to append between parameters.
111
* @throws IOException
113
public Form(String parametersString, char separator) {
114
this(parametersString, CharacterSet.UTF_8, separator);
121
* The Web form parameters as a string.
122
* @param characterSet
123
* The supported character encoding.
124
* @throws IOException
126
public Form(String queryString, CharacterSet characterSet) {
127
this(queryString, characterSet, '&');
133
* @param parametersString
134
* The parameters string to parse.
135
* @param characterSet
136
* The supported character encoding.
138
* The separator character to append between parameters.
139
* @throws IOException
141
public Form(String parametersString, CharacterSet characterSet,
143
FormUtils.parse(this, parametersString, characterSet, true, separator);
147
public Parameter createEntry(String name, String value) {
148
return new Parameter(name, value);
152
public Series<Parameter> createSeries(List<Parameter> delegate) {
153
if (delegate != null) {
154
return new Form(delegate);
161
* Encodes the form using the standard URI encoding mechanism and the UTF-8
164
* @return The encoded form.
165
* @throws IOException
167
public String encode() throws IOException {
168
return encode(CharacterSet.UTF_8);
172
* URL encodes the form. The '&' character is used as a separator.
174
* @param characterSet
175
* The supported character encoding.
176
* @return The encoded form.
177
* @throws IOException
179
public String encode(CharacterSet characterSet) throws IOException {
180
return encode(characterSet, '&');
184
* URL encodes the form.
186
* @param characterSet
187
* The supported character encoding.
189
* The separator character to append between parameters.
190
* @return The encoded form.
191
* @throws IOException
193
public String encode(CharacterSet characterSet, char separator)
195
final StringBuilder sb = new StringBuilder();
196
for (int i = 0; i < size(); i++) {
198
sb.append(separator);
200
get(i).encode(sb, characterSet);
203
return sb.toString();
207
* Formats the form as a matrix path string. Uses UTF-8 as the character set
208
* for encoding non-ASCII characters.
210
* @return The form as a matrix string.
211
* @see <a href="http://www.w3.org/DesignIssues/MatrixURIs.html">Matrix URIs
212
* by Tim Berners Lee</a>
214
public String getMatrixString() {
215
return getMatrixString(CharacterSet.UTF_8);
219
* Formats the form as a query string.
221
* @param characterSet
222
* The supported character encoding.
223
* @return The form as a matrix string.
224
* @see <a href="http://www.w3.org/DesignIssues/MatrixURIs.html">Matrix URIs
225
* by Tim Berners Lee</a>
227
public String getMatrixString(CharacterSet characterSet) {
229
return encode(characterSet, ';');
230
} catch (IOException ioe) {
236
* Formats the form as a query string. Uses UTF-8 as the character set for
237
* encoding non-ASCII characters.
239
* @return The form as a query string.
241
public String getQueryString() {
242
return getQueryString(CharacterSet.UTF_8);
246
* Formats the form as a query string.
248
* @param characterSet
249
* The supported character encoding.
250
* @return The form as a query string.
252
public String getQueryString(CharacterSet characterSet) {
254
return encode(characterSet);
255
} catch (IOException ioe) {
261
* Returns the form as a Web representation
262
* (MediaType.APPLICATION_WWW_FORM). Uses UTF-8 as the character set for
263
* encoding non-ASCII characters.
265
* @return The form as a Web representation.
267
public Representation getWebRepresentation() {
268
return getWebRepresentation(CharacterSet.UTF_8);
272
* Returns the form as a Web representation
273
* (MediaType.APPLICATION_WWW_FORM).
275
* @param characterSet
276
* The supported character encoding.
277
* @return The form as a Web representation.
279
public Representation getWebRepresentation(CharacterSet characterSet) {
280
return new StringRepresentation(getQueryString(characterSet),
281
MediaType.APPLICATION_WWW_FORM, null, characterSet);