01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.beans.propertyeditors;
18:
19: import java.beans.PropertyEditorSupport;
20: import java.io.IOException;
21:
22: import org.springframework.core.io.Resource;
23: import org.springframework.core.io.ResourceEditor;
24: import org.springframework.util.Assert;
25:
26: /**
27: * One-way PropertyEditor, which can convert from a text string to a
28: * <code>java.io.InputStream</code>, allowing InputStream properties
29: * to be set directly as a text string.
30: *
31: * <p>Supports Spring-style URL notation: any fully qualified standard URL
32: * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
33: *
34: * <p>Note that in the default usage, the stream is not closed by Spring itself!
35: *
36: * @author Juergen Hoeller
37: * @since 1.0.1
38: * @see java.io.InputStream
39: * @see org.springframework.core.io.ResourceEditor
40: * @see org.springframework.core.io.ResourceLoader
41: * @see URLEditor
42: * @see FileEditor
43: */
44: public class InputStreamEditor extends PropertyEditorSupport {
45:
46: private final ResourceEditor resourceEditor;
47:
48: /**
49: * Create a new InputStreamEditor,
50: * using the default ResourceEditor underneath.
51: */
52: public InputStreamEditor() {
53: this .resourceEditor = new ResourceEditor();
54: }
55:
56: /**
57: * Create a new InputStreamEditor,
58: * using the given ResourceEditor underneath.
59: * @param resourceEditor the ResourceEditor to use
60: */
61: public InputStreamEditor(ResourceEditor resourceEditor) {
62: Assert.notNull(resourceEditor,
63: "ResourceEditor must not be null");
64: this .resourceEditor = resourceEditor;
65: }
66:
67: public void setAsText(String text) throws IllegalArgumentException {
68: this .resourceEditor.setAsText(text);
69: Resource resource = (Resource) this .resourceEditor.getValue();
70: try {
71: setValue(resource != null ? resource.getInputStream()
72: : null);
73: } catch (IOException ex) {
74: throw new IllegalArgumentException(
75: "Could not retrieve InputStream for " + resource
76: + ": " + ex.getMessage());
77: }
78: }
79:
80: /**
81: * This implementation returns <code>null</code> to indicate that
82: * there is no appropriate text representation.
83: */
84: public String getAsText() {
85: return null;
86: }
87:
88: }
|