KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > FormInput


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jan 15, 2006
23  */

24 package org.lobobrowser.html;
25
26 /**
27  * The <code>FormInput</code> class contains the state
28  * of an HTML form input item.
29  */

30 public class FormInput {
31     //private final InputStream inputStream;
32
//private final String charset;
33
public static final FormInput[] EMPTY_ARRAY = new FormInput[0];
34     private final String JavaDoc name;
35     private final String JavaDoc textValue;
36     private final java.io.File JavaDoc fileValue;
37     
38     /**
39      * Constructs a <code>FormInput</code>
40      * with a text value.
41      * @param name The name of the input.
42      * @param value The value of the input.
43      */

44     public FormInput(String JavaDoc name, String JavaDoc value) {
45         super();
46         this.name = name;
47         this.textValue = value;
48         this.fileValue = null;
49     }
50     
51     /**
52      * Constructs a <code>FormInput</code>
53      * with a file value.
54      * @param name The name of the input.
55      * @param value The value of the input.
56      */

57     public FormInput(String JavaDoc name, java.io.File JavaDoc value) {
58         this.name = name;
59         this.textValue = null;
60         this.fileValue = value;
61     }
62     
63     /**
64      * Gets the name of the input.
65      */

66     public String JavaDoc getName() {
67         return this.name;
68     }
69
70     public boolean isText() {
71         return this.textValue != null;
72     }
73     
74     public boolean isFile() {
75         return this.fileValue != null;
76     }
77     
78     public String JavaDoc getTextValue() {
79         return this.textValue;
80     }
81     
82     public java.io.File JavaDoc getFileValue() {
83         return this.fileValue;
84     }
85
86     /**
87      * Always returns UTF-8.
88      * @deprecated The method is implemented only to provide some backward compatibility.
89      */

90     public String JavaDoc getCharset() {
91         return "UTF-8";
92     }
93     
94     /**
95      * Gets data as an input stream. The caller
96      * is responsible for closing the stream.
97      * @deprecated Call either {@link #getTextValue()} or {@link #getFileValue()} instead.
98      */

99     public java.io.InputStream JavaDoc getInputStream() throws java.io.IOException JavaDoc {
100         if(this.isText()) {
101             return new java.io.ByteArrayInputStream JavaDoc(this.getTextValue().getBytes("UTF-8"));
102         }
103         else if(this.isFile()) {
104             return new java.io.FileInputStream JavaDoc(this.getFileValue());
105         }
106         else {
107             return null;
108         }
109     }
110     
111     /**
112      * Shows a string representation of the <code>FormInput</code>
113      * that may be useful in debugging.
114      * @see #getTextValue()
115      */

116     public String JavaDoc toString() {
117         return "FormInput[name=" + this.name + ",textValue=" + this.textValue + "]";
118     }
119 }
120
Popular Tags