KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > InputStreamEditor


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
18 package org.springframework.beans.propertyeditors;
19
20 import java.beans.PropertyEditorSupport JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.springframework.core.io.Resource;
24 import org.springframework.core.io.ResourceEditor;
25 import org.springframework.util.Assert;
26
27 /**
28  * One-way PropertyEditor, which can convert from a text string to a
29  * <code>java.io.InputStream</code>, allowing InputStream properties
30  * to be set directly as a text string.
31  *
32  * <p>Supports Spring-style URL notation: any fully qualified standard URL
33  * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
34  *
35  * <p>Note that in the default usage, the stream is not closed by Spring itself!
36  *
37  * @author Juergen Hoeller
38  * @since 1.0.1
39  * @see java.io.InputStream
40  * @see org.springframework.core.io.ResourceEditor
41  * @see org.springframework.core.io.ResourceLoader
42  * @see URLEditor
43  * @see FileEditor
44  */

45 public class InputStreamEditor extends PropertyEditorSupport JavaDoc {
46
47     private final ResourceEditor resourceEditor;
48
49
50     /**
51      * Create a new InputStreamEditor,
52      * using the default ResourceEditor underneath.
53      */

54     public InputStreamEditor() {
55         this.resourceEditor = new ResourceEditor();
56     }
57
58     /**
59      * Create a new InputStreamEditor,
60      * using the given ResourceEditor underneath.
61      * @param resourceEditor the ResourceEditor to use
62      */

63     public InputStreamEditor(ResourceEditor resourceEditor) {
64         Assert.notNull(resourceEditor, "ResourceEditor must not be null");
65         this.resourceEditor = resourceEditor;
66     }
67
68
69     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
70         this.resourceEditor.setAsText(text);
71         Resource resource = (Resource) this.resourceEditor.getValue();
72         try {
73             setValue(resource != null ? resource.getInputStream() : null);
74         }
75         catch (IOException JavaDoc ex) {
76             throw new IllegalArgumentException JavaDoc(
77                     "Could not retrieve InputStream for " + resource + ": " + ex.getMessage());
78         }
79     }
80
81     /**
82      * This implementation returns <code>null</code> to indicate that
83      * there is no appropriate text representation.
84      */

85     public String JavaDoc getAsText() {
86         return null;
87     }
88
89 }
90
Popular Tags