001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.data;
020:
021: import java.io.IOException;
022: import java.util.List;
023: import java.util.logging.Logger;
024:
025: import org.restlet.resource.Representation;
026: import org.restlet.resource.StringRepresentation;
027: import org.restlet.util.Engine;
028: import org.restlet.util.Series;
029:
030: /**
031: * Form which is a specialized modifiable list of parameters.
032: *
033: * @author Jerome Louvel (contact@noelios.com)
034: */
035: public class Form extends Series<Parameter> {
036: /**
037: * Empty constructor.
038: */
039: public Form() {
040: super ();
041: }
042:
043: /**
044: * Constructor.
045: *
046: * @param initialCapacity
047: * The initial list capacity.
048: */
049: public Form(int initialCapacity) {
050: super (initialCapacity);
051: }
052:
053: /**
054: * Constructor.
055: *
056: * @param delegate
057: * The delegate list.
058: */
059: public Form(List<Parameter> delegate) {
060: super (delegate);
061: }
062:
063: /**
064: * Constructor.
065: *
066: * @param logger
067: * The logger to use.
068: * @param representation
069: * The representation to parse (URL encoded Web form supported).
070: * @throws IOException
071: */
072: public Form(Logger logger, Representation representation) {
073: Engine.getInstance().parse(logger, this , representation);
074: }
075:
076: /**
077: * Constructor.
078: *
079: * @param logger
080: * The logger to use.
081: * @param queryString
082: * The Web form parameters as a string.
083: * @param characterSet
084: * The supported character encoding.
085: * @throws IOException
086: */
087: public Form(Logger logger, String queryString,
088: CharacterSet characterSet) {
089: Engine.getInstance().parse(logger, this , queryString,
090: characterSet);
091: }
092:
093: /**
094: * Constructor.
095: *
096: * @param webForm
097: * The URL encoded Web form.
098: * @throws IOException
099: */
100: public Form(Representation webForm) {
101: this (Logger.getLogger(Form.class.getCanonicalName()), webForm);
102: }
103:
104: /**
105: * Constructor. Uses UTF-8 as the character set for encoding non-ASCII
106: * characters.
107: *
108: * @param queryString
109: * The Web form parameters as a string.
110: * @throws IOException
111: */
112: public Form(String queryString) {
113: this (Logger.getLogger(Form.class.getCanonicalName()),
114: queryString, CharacterSet.UTF_8);
115: }
116:
117: /**
118: * Constructor.
119: *
120: * @param queryString
121: * The Web form parameters as a string.
122: * @param characterSet
123: * The supported character encoding.
124: * @throws IOException
125: */
126: public Form(String queryString, CharacterSet characterSet) {
127: this (Logger.getLogger(Form.class.getCanonicalName()),
128: queryString, characterSet);
129: }
130:
131: @Override
132: public Parameter createEntry(String name, String value) {
133: return new Parameter(name, value);
134: }
135:
136: @Override
137: public Series<Parameter> createSeries(List<Parameter> delegate) {
138: if (delegate != null)
139: return new Form(delegate);
140: else
141: return new Form();
142: }
143:
144: /**
145: * Formats the form as a query string. Uses UTF-8 as the character set for
146: * encoding non-ASCII characters.
147: *
148: * @return The form as a query string.
149: */
150: public String getQueryString() {
151: return getQueryString(CharacterSet.UTF_8);
152: }
153:
154: /**
155: * Formats the form as a query string.
156: *
157: * @param characterSet
158: * The supported character encoding.
159: * @return The form as a query string.
160: */
161: public String getQueryString(CharacterSet characterSet) {
162: try {
163: return encode(characterSet);
164: } catch (IOException ioe) {
165: return null;
166: }
167: }
168:
169: /**
170: * Returns the form as a Web representation
171: * (MediaType.APPLICATION_WWW_FORM). Uses UTF-8 as the character set for
172: * encoding non-ASCII characters.
173: *
174: * @return The form as a Web representation.
175: */
176: public Representation getWebRepresentation() {
177: return getWebRepresentation(CharacterSet.UTF_8);
178: }
179:
180: /**
181: * Returns the form as a Web representation
182: * (MediaType.APPLICATION_WWW_FORM).
183: *
184: * @param characterSet
185: * The supported character encoding.
186: * @return The form as a Web representation.
187: */
188: public Representation getWebRepresentation(CharacterSet characterSet) {
189: return new StringRepresentation(getQueryString(characterSet),
190: MediaType.APPLICATION_WWW_FORM, null, characterSet);
191: }
192:
193: /**
194: * Encodes the form using the standard URI encoding mechanism and the UTF-8
195: * character set.
196: *
197: * @return The encoded form.
198: * @throws IOException
199: */
200: public String encode() throws IOException {
201: return encode(CharacterSet.UTF_8);
202: }
203:
204: /**
205: * URL encodes the form.
206: *
207: * @param characterSet
208: * The supported character encoding.
209: * @return The encoded form.
210: * @throws IOException
211: */
212: public String encode(CharacterSet characterSet) throws IOException {
213: StringBuilder sb = new StringBuilder();
214: for (int i = 0; i < size(); i++) {
215: if (i > 0)
216: sb.append('&');
217: get(i).encode(sb, characterSet);
218: }
219: return sb.toString();
220: }
221:
222: }
|