001: /*
002: * Copyright 2002-2006 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.beans.propertyeditors;
018:
019: import java.beans.PropertyEditorSupport;
020: import java.io.File;
021: import java.io.IOException;
022:
023: import org.springframework.core.io.Resource;
024: import org.springframework.core.io.ResourceEditor;
025: import org.springframework.util.Assert;
026: import org.springframework.util.ResourceUtils;
027: import org.springframework.util.StringUtils;
028:
029: /**
030: * Editor for <code>java.io.File</code>, to directly populate a File property
031: * from a Spring resource location.
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><b>NOTE:</b> The behavior of this editor has changed in Spring 2.0.
037: * Previously, it created a File instance directly from a filename.
038: * As of Spring 2.0, it takes a standard Spring resource location as input;
039: * this is consistent with URLEditor and InputStreamEditor now.
040: *
041: * @author Juergen Hoeller
042: * @since 09.12.2003
043: * @see java.io.File
044: * @see org.springframework.core.io.ResourceEditor
045: * @see org.springframework.core.io.ResourceLoader
046: * @see URLEditor
047: * @see InputStreamEditor
048: */
049: public class FileEditor extends PropertyEditorSupport {
050:
051: private final ResourceEditor resourceEditor;
052:
053: /**
054: * Create a new FileEditor,
055: * using the default ResourceEditor underneath.
056: */
057: public FileEditor() {
058: this .resourceEditor = new ResourceEditor();
059: }
060:
061: /**
062: * Create a new FileEditor,
063: * using the given ResourceEditor underneath.
064: * @param resourceEditor the ResourceEditor to use
065: */
066: public FileEditor(ResourceEditor resourceEditor) {
067: Assert.notNull(resourceEditor,
068: "ResourceEditor must not be null");
069: this .resourceEditor = resourceEditor;
070: }
071:
072: public void setAsText(String text) throws IllegalArgumentException {
073: // Check whether we got an absolute file path without "file:" prefix.
074: // For backwards compatibility, we'll consider those as straight file path.
075: if (StringUtils.hasText(text) && !ResourceUtils.isUrl(text)) {
076: File file = new File(text);
077: if (file.isAbsolute()) {
078: setValue(file);
079: return;
080: }
081: }
082:
083: // Proceed with standard resource location parsing.
084: this .resourceEditor.setAsText(text);
085: Resource resource = (Resource) this .resourceEditor.getValue();
086: try {
087: setValue(resource != null ? resource.getFile() : null);
088: } catch (IOException ex) {
089: throw new IllegalArgumentException(
090: "Could not retrieve File for " + resource + ": "
091: + ex.getMessage());
092: }
093: }
094:
095: public String getAsText() {
096: File value = (File) getValue();
097: return (value != null ? value.getPath() : "");
098: }
099:
100: }
|