001/*
002 * Copyright 2002-2017 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.core.io;
018
019import java.beans.PropertyEditorSupport;
020import java.io.IOException;
021
022import org.springframework.core.env.PropertyResolver;
023import org.springframework.core.env.StandardEnvironment;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026import org.springframework.util.StringUtils;
027
028/**
029 * {@link java.beans.PropertyEditor Editor} for {@link Resource}
030 * descriptors, to automatically convert {@code String} locations
031 * e.g. {@code file:C:/myfile.txt} or {@code classpath:myfile.txt} to
032 * {@code Resource} properties instead of using a {@code String} location property.
033 *
034 * <p>The path may contain {@code ${...}} placeholders, to be
035 * resolved as {@link org.springframework.core.env.Environment} properties:
036 * e.g. {@code ${user.dir}}. Unresolvable placeholders are ignored by default.
037 *
038 * <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
039 * by default using a {@link DefaultResourceLoader}.
040 *
041 * @author Juergen Hoeller
042 * @author Dave Syer
043 * @author Chris Beams
044 * @since 28.12.2003
045 * @see Resource
046 * @see ResourceLoader
047 * @see DefaultResourceLoader
048 * @see PropertyResolver#resolvePlaceholders
049 */
050public class ResourceEditor extends PropertyEditorSupport {
051
052        private final ResourceLoader resourceLoader;
053
054        @Nullable
055        private PropertyResolver propertyResolver;
056
057        private final boolean ignoreUnresolvablePlaceholders;
058
059
060        /**
061         * Create a new instance of the {@link ResourceEditor} class
062         * using a {@link DefaultResourceLoader} and {@link StandardEnvironment}.
063         */
064        public ResourceEditor() {
065                this(new DefaultResourceLoader(), null);
066        }
067
068        /**
069         * Create a new instance of the {@link ResourceEditor} class
070         * using the given {@link ResourceLoader} and {@link PropertyResolver}.
071         * @param resourceLoader the {@code ResourceLoader} to use
072         * @param propertyResolver the {@code PropertyResolver} to use
073         */
074        public ResourceEditor(ResourceLoader resourceLoader, @Nullable PropertyResolver propertyResolver) {
075                this(resourceLoader, propertyResolver, true);
076        }
077
078        /**
079         * Create a new instance of the {@link ResourceEditor} class
080         * using the given {@link ResourceLoader}.
081         * @param resourceLoader the {@code ResourceLoader} to use
082         * @param propertyResolver the {@code PropertyResolver} to use
083         * @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
084         * if no corresponding property could be found in the given {@code propertyResolver}
085         */
086        public ResourceEditor(ResourceLoader resourceLoader, @Nullable PropertyResolver propertyResolver,
087                        boolean ignoreUnresolvablePlaceholders) {
088
089                Assert.notNull(resourceLoader, "ResourceLoader must not be null");
090                this.resourceLoader = resourceLoader;
091                this.propertyResolver = propertyResolver;
092                this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
093        }
094
095
096        @Override
097        public void setAsText(String text) {
098                if (StringUtils.hasText(text)) {
099                        String locationToUse = resolvePath(text).trim();
100                        setValue(this.resourceLoader.getResource(locationToUse));
101                }
102                else {
103                        setValue(null);
104                }
105        }
106
107        /**
108         * Resolve the given path, replacing placeholders with corresponding
109         * property values from the {@code environment} if necessary.
110         * @param path the original file path
111         * @return the resolved file path
112         * @see PropertyResolver#resolvePlaceholders
113         * @see PropertyResolver#resolveRequiredPlaceholders
114         */
115        protected String resolvePath(String path) {
116                if (this.propertyResolver == null) {
117                        this.propertyResolver = new StandardEnvironment();
118                }
119                return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
120                                this.propertyResolver.resolveRequiredPlaceholders(path));
121        }
122
123
124        @Override
125        @Nullable
126        public String getAsText() {
127                Resource value = (Resource) getValue();
128                try {
129                        // Try to determine URL for resource.
130                        return (value != null ? value.getURL().toExternalForm() : "");
131                }
132                catch (IOException ex) {
133                        // Couldn't determine resource URL - return null to indicate
134                        // that there is no appropriate text representation.
135                        return null;
136                }
137        }
138
139}