org.springframework.beans.propertyeditors
public class: FileEditor [javadoc |
source]
java.lang.Object
java.beans.PropertyEditorSupport
org.springframework.beans.propertyeditors.FileEditor
All Implemented Interfaces:
PropertyEditor
Editor for
java.io.File, to directly populate a File property
from a Spring resource location.
Supports Spring-style URL notation: any fully qualified standard URL
("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
NOTE: The behavior of this editor has changed in Spring 2.0.
Previously, it created a File instance directly from a filename.
As of Spring 2.0, it takes a standard Spring resource location as input;
this is consistent with URLEditor and InputStreamEditor now.
NOTE: In Spring 2.5 the following modification was made.
If a file name is specified without a URL prefix or without an absolute path
then we try to locate the file using standard ResourceLoader semantics.
If the file was not found, then a File instance is created assuming the file
name refers to a relative file location.
| Method from org.springframework.beans.propertyeditors.FileEditor Summary: |
|---|
|
getAsText, setAsText |
| Methods from java.beans.PropertyEditorSupport: |
|---|
|
addPropertyChangeListener, firePropertyChange, getAsText, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setAsText, setSource, setValue, supportsCustomEditor |
| Method from org.springframework.beans.propertyeditors.FileEditor Detail: |
public String getAsText() {
File value = (File) getValue();
return (value != null ? value.getPath() : "");
}
|
public void setAsText(String text) throws IllegalArgumentException {
// Check whether we got an absolute file path without "file:" prefix.
// For backwards compatibility, we'll consider those as straight file path.
if (StringUtils.hasText(text) && !ResourceUtils.isUrl(text)) {
File file = new File(text);
if (file.isAbsolute()) {
setValue(file);
return;
}
}
// Proceed with standard resource location parsing.
this.resourceEditor.setAsText(text);
Resource resource = (Resource) this.resourceEditor.getValue();
// Non URLs will be treated as relative paths if the resource was not found
if(ResourceUtils.isUrl(text) || resource.exists()) {
try {
setValue(resource != null ? resource.getFile() : null);
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not retrieve File for " + resource + ": " + ex.getMessage());
}
}
else {
// Create a relative File reference and hope for the best
File file = new File(text);
setValue(file);
}
}
|