KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > multipart > support > StringMultipartFileEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
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  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.multipart.support;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.web.multipart.MultipartFile;
26
27 /**
28  * Custom {@link java.beans.PropertyEditor} for converting
29  * {@link org.springframework.web.multipart.MultipartFile MultipartFiles}
30  * to Strings.
31  *
32  * <p>Allows one to specify the charset to use.
33  *
34  * @author Juergen Hoeller
35  * @since 13.10.2003
36  */

37 public class StringMultipartFileEditor extends PropertyEditorSupport JavaDoc {
38
39     /** Static to avoid creating a new logger every time */
40     private static final Log logger = LogFactory.getLog(StringMultipartFileEditor.class);
41
42     private final String JavaDoc charsetName;
43
44
45     /**
46      * Create a new {@link StringMultipartFileEditor}, using the default charset.
47      */

48     public StringMultipartFileEditor() {
49         this.charsetName = null;
50     }
51
52     /**
53      * Create a new {@link StringMultipartFileEditor}, using the given charset.
54      * @param charsetName valid charset name
55      * @see java.lang.String#String(byte[],String)
56      */

57     public StringMultipartFileEditor(String JavaDoc charsetName) {
58         this.charsetName = charsetName;
59     }
60
61
62     public void setAsText(String JavaDoc text) {
63         setValue(text);
64     }
65
66     public void setValue(Object JavaDoc value) {
67         if (value instanceof MultipartFile) {
68             MultipartFile multipartFile = (MultipartFile) value;
69             try {
70                 super.setValue(this.charsetName != null ?
71                         new String JavaDoc(multipartFile.getBytes(), this.charsetName) :
72                         new String JavaDoc(multipartFile.getBytes()));
73             }
74             catch (IOException JavaDoc ex) {
75                 logger.warn("Cannot read contents of multipart file", ex);
76                 throw new IllegalArgumentException JavaDoc("Cannot read contents of multipart file: " + ex.getMessage());
77             }
78         }
79         else {
80             super.setValue(value);
81         }
82     }
83
84 }
85
Popular Tags