001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.core.io;
018:
019: import java.beans.PropertyEditorSupport;
020: import java.io.IOException;
021:
022: import org.springframework.util.Assert;
023: import org.springframework.util.StringUtils;
024: import org.springframework.util.SystemPropertyUtils;
025:
026: /**
027: * {@link java.beans.PropertyEditor Editor} for {@link Resource}
028: * descriptors, to automatically convert <code>String</code> locations
029: * e.g. <code>"file:C:/myfile.txt"</code> or
030: * <code>"classpath:myfile.txt"</code>) to <code>Resource</code>
031: * properties instead of using a <code>String</code> location property.
032: *
033: * <p>The path may contain <code>${...}</code> placeholders, to be resolved
034: * as system properties: e.g. <code>${user.dir}</code>.
035: *
036: * <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
037: * by default using a {@link DefaultResourceLoader}.
038: *
039: * @author Juergen Hoeller
040: * @since 28.12.2003
041: * @see Resource
042: * @see ResourceLoader
043: * @see DefaultResourceLoader
044: * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
045: * @see System#getProperty(String)
046: */
047: public class ResourceEditor extends PropertyEditorSupport {
048:
049: private final ResourceLoader resourceLoader;
050:
051: /**
052: * Create a new instance of the {@link ResourceEditor} class
053: * using a {@link DefaultResourceLoader}.
054: */
055: public ResourceEditor() {
056: this (new DefaultResourceLoader());
057: }
058:
059: /**
060: * Create a new instance of the {@link ResourceEditor} class
061: * using the given {@link ResourceLoader}.
062: * @param resourceLoader the <code>ResourceLoader</code> to use
063: */
064: public ResourceEditor(ResourceLoader resourceLoader) {
065: Assert.notNull(resourceLoader,
066: "ResourceLoader must not be null");
067: this .resourceLoader = resourceLoader;
068: }
069:
070: public void setAsText(String text) {
071: if (StringUtils.hasText(text)) {
072: String locationToUse = resolvePath(text).trim();
073: setValue(this .resourceLoader.getResource(locationToUse));
074: } else {
075: setValue(null);
076: }
077: }
078:
079: /**
080: * Resolve the given path, replacing placeholders with
081: * corresponding system property values if necessary.
082: * @param path the original file path
083: * @return the resolved file path
084: * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
085: */
086: protected String resolvePath(String path) {
087: return SystemPropertyUtils.resolvePlaceholders(path);
088: }
089:
090: public String getAsText() {
091: Resource value = (Resource) getValue();
092: try {
093: // Try to determine URL for resource.
094: return (value != null ? value.getURL().toExternalForm()
095: : "");
096: } catch (IOException ex) {
097: // Couldn't determine resource URL - return null to indicate
098: // that there is no appropriate text representation.
099: return null;
100: }
101: }
102:
103: }
|