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.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * One-way PropertyEditor which can convert from a text String to a
030 * {@code java.io.Reader}, interpreting the given String as a Spring
031 * resource location (e.g. a URL String).
032 *
033 * <p>Supports Spring-style URL notation: any fully qualified standard URL
034 * ("file:", "http:", etc.) and Spring's special "classpath:" pseudo-URL.
035 *
036 * <p>Note that such readers usually do not get closed by Spring itself!
037 *
038 * @author Juergen Hoeller
039 * @since 4.2
040 * @see java.io.Reader
041 * @see org.springframework.core.io.ResourceEditor
042 * @see org.springframework.core.io.ResourceLoader
043 * @see InputStreamEditor
044 */
045public class ReaderEditor extends PropertyEditorSupport {
046
047        private final ResourceEditor resourceEditor;
048
049
050        /**
051         * Create a new ReaderEditor, using the default ResourceEditor underneath.
052         */
053        public ReaderEditor() {
054                this.resourceEditor = new ResourceEditor();
055        }
056
057        /**
058         * Create a new ReaderEditor, using the given ResourceEditor underneath.
059         * @param resourceEditor the ResourceEditor to use
060         */
061        public ReaderEditor(ResourceEditor resourceEditor) {
062                Assert.notNull(resourceEditor, "ResourceEditor must not be null");
063                this.resourceEditor = resourceEditor;
064        }
065
066
067        @Override
068        public void setAsText(String text) throws IllegalArgumentException {
069                this.resourceEditor.setAsText(text);
070                Resource resource = (Resource) this.resourceEditor.getValue();
071                try {
072                        setValue(resource != null ? new EncodedResource(resource).getReader() : null);
073                }
074                catch (IOException ex) {
075                        throw new IllegalArgumentException("Failed to retrieve Reader for " + resource, ex);
076                }
077        }
078
079        /**
080         * This implementation returns {@code null} to indicate that
081         * there is no appropriate text representation.
082         */
083        @Override
084        @Nullable
085        public String getAsText() {
086                return null;
087        }
088
089}