001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.io.File;
041: import java.net.URI;
042: import java.net.URISyntaxException;
043: import java.util.Map;
044:
045: import org.apache.commons.lang.StringUtils;
046:
047: import com.gargoylesoftware.htmlunit.KeyDataPair;
048: import com.gargoylesoftware.htmlunit.KeyValuePair;
049:
050: /**
051: * Wrapper for the HTML element "input".
052: *
053: * @version $Revision: 2132 $
054: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
055: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
056: * @author Daniel Gredler
057: * @author Ahmed Ashour
058: * @author Marc Guillemot
059: */
060: public class HtmlFileInput extends HtmlInput {
061:
062: private static final long serialVersionUID = 7925479292349207154L;
063: private String contentType_;
064:
065: /**
066: * Create an instance
067: *
068: * @param page The page that contains this element
069: * @param attributes the initial attributes
070: * @deprecated You should not directly construct HtmlFileInput.
071: */
072: //TODO: to be removed, deprecated after 1.11
073: public HtmlFileInput(final HtmlPage page, final Map attributes) {
074: this (null, TAG_NAME, page, attributes);
075: }
076:
077: /**
078: * Create an instance
079: *
080: * @param namespaceURI the URI that identifies an XML namespace.
081: * @param qualifiedName The qualified name of the element type to instantiate
082: * @param page The page that contains this element
083: * @param attributes the initial attributes
084: */
085: HtmlFileInput(final String namespaceURI,
086: final String qualifiedName, final HtmlPage page,
087: final Map attributes) {
088: super (namespaceURI, qualifiedName, page, attributes);
089: setAttributeValue("value", "");
090: if (page.getWebClient().getBrowserVersion().isIE()) {
091: setDefaultValue("");
092: }
093: }
094:
095: /**
096: * Return an array of KeyValuePairs that are the values that will be sent
097: * back to the server whenever the current form is submitted. Some of the
098: * values may be KeyDataPairs indicating that a File will be uploaded<p>
099: *
100: * THIS METHOD IS INTENDED FOR THE USE OF THE FRAMEWORK ONLY AND SHOULD NOT
101: * BE USED BY CONSUMERS OF HTMLUNIT. USE AT YOUR OWN RISK.
102: *
103: * @return See above
104: */
105: public KeyValuePair[] getSubmitKeyValuePairs() {
106: String value = getValueAttribute();
107:
108: if (StringUtils.isEmpty(value)) {
109: return new KeyValuePair[] { new KeyDataPair(
110: getNameAttribute(), new File(""), null, null) };
111: }
112:
113: File file = null;
114: // to tolerate file://
115: if (value.startsWith("file:/")) {
116: if (value.startsWith("file://")
117: && !value.startsWith("file:///")) {
118: value = "file:///" + value.substring(7);
119: }
120: try {
121: file = new File(new URI(value));
122: } catch (final URISyntaxException e) {
123: // nothing here
124: }
125: }
126:
127: if (file == null) {
128: file = new File(value);
129: }
130:
131: // contentType and charset are determined from browser and page
132: // perhaps it could be interesting to have setters for it in this class
133: // to give finer control to user
134: final String contentType;
135: if (contentType_ == null) {
136: contentType = getPage().getWebClient().guessContentType(
137: file);
138: } else {
139: contentType = contentType_;
140: }
141: final String charset = getPage().getPageEncoding();
142: return new KeyValuePair[] { new KeyDataPair(getNameAttribute(),
143: file, contentType, charset) };
144: }
145:
146: /**
147: * {@inheritDoc} This method <b>does nothing</b> for file input elements.
148: * @see SubmittableElement#reset()
149: */
150: public void reset() {
151: // Empty.
152: }
153:
154: /**
155: * {@inheritDoc} Overridden so that this does not set the value attribute when emulating
156: * Netscape browsers.
157: * @see HtmlInput#setDefaultValue(String)
158: */
159: public void setDefaultValue(final String defaultValue) {
160: setDefaultValue(defaultValue, false);
161: }
162:
163: /**
164: * Sets the content type value that should be send together with the uploaded file.
165: * If content type is not explicitly set, HtmlUnit will try to guess it from the file content.
166: * @param contentType the content type, <code>null</code> resets it
167: */
168: public void setContentType(final String contentType) {
169: contentType_ = contentType;
170: }
171:
172: /**
173: * Gets the content type that should be send together with the uploaded file.
174: * @return the content type, <code>null</code> if this has not been explicitly set
175: * and should be guessed from file content.
176: */
177: public String getContentType() {
178: return contentType_;
179: }
180: }
|