001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020import java.io.IOException;
021
022import org.springframework.core.io.Resource;
023import org.springframework.core.io.ResourceEditor;
024import org.springframework.core.io.support.EncodedResource;
025import org.springframework.util.Assert;
026
027/**
028 * One-way PropertyEditor which can convert from a text String to a
029 * {@code java.io.Reader}, interpreting the given String as a Spring
030 * resource location (e.g. a URL String).
031 *
032 * <p>Supports Spring-style URL notation: any fully qualified standard URL
033 * ("file:", "http:", etc.) and Spring's special "classpath:" pseudo-URL.
034 *
035 * <p>Note that such readers usually do not get closed by Spring itself!
036 *
037 * @author Juergen Hoeller
038 * @since 4.2
039 * @see java.io.Reader
040 * @see org.springframework.core.io.ResourceEditor
041 * @see org.springframework.core.io.ResourceLoader
042 * @see InputStreamEditor
043 */
044public class ReaderEditor extends PropertyEditorSupport {
045
046        private final ResourceEditor resourceEditor;
047
048
049        /**
050         * Create a new ReaderEditor, using the default ResourceEditor underneath.
051         */
052        public ReaderEditor() {
053                this.resourceEditor = new ResourceEditor();
054        }
055
056        /**
057         * Create a new ReaderEditor, using the given ResourceEditor underneath.
058         * @param resourceEditor the ResourceEditor to use
059         */
060        public ReaderEditor(ResourceEditor resourceEditor) {
061                Assert.notNull(resourceEditor, "ResourceEditor must not be null");
062                this.resourceEditor = resourceEditor;
063        }
064
065
066        @Override
067        public void setAsText(String text) throws IllegalArgumentException {
068                this.resourceEditor.setAsText(text);
069                Resource resource = (Resource) this.resourceEditor.getValue();
070                try {
071                        setValue(resource != null ? new EncodedResource(resource).getReader() : null);
072                }
073                catch (IOException ex) {
074                        throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex);
075                }
076        }
077
078        /**
079         * This implementation returns {@code null} to indicate that
080         * there is no appropriate text representation.
081         */
082        @Override
083        public String getAsText() {
084                return null;
085        }
086
087}