org.springframework.core.io.support
public class: ResourceArrayPropertyEditor [javadoc |
source]
java.lang.Object
java.beans.PropertyEditorSupport
org.springframework.core.io.support.ResourceArrayPropertyEditor
All Implemented Interfaces:
PropertyEditor
Editor for
org.springframework.core.io.Resource arrays, to
automatically convert
String location patterns
(e.g.
"file:C:/my*.txt" or
"classpath*:myfile.txt")
to
Resource array properties. Can also translate a collection
or array of location patterns into a merged Resource array.
The path may contain ${...} placeholders, to be resolved
as system properties: e.g. ${user.dir}.
Delegates to a ResourcePatternResolver ,
by default using a PathMatchingResourcePatternResolver .
| Methods from java.beans.PropertyEditorSupport: |
|---|
|
addPropertyChangeListener, firePropertyChange, getAsText, getCustomEditor, getJavaInitializationString, getSource, getTags, getValue, isPaintable, paintValue, removePropertyChangeListener, setAsText, setSource, setValue, supportsCustomEditor |
| Method from org.springframework.core.io.support.ResourceArrayPropertyEditor Detail: |
protected String resolvePath(String path) {
return SystemPropertyUtils.resolvePlaceholders(path);
}
Resolve the given path, replacing placeholders with
corresponding system property values if necessary. |
public void setAsText(String text) {
String pattern = resolvePath(text).trim();
try {
setValue(this.resourcePatternResolver.getResources(pattern));
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
}
}
Treat the given text as location pattern and convert it to a Resource array. |
public void setValue(Object value) throws IllegalArgumentException {
if (value instanceof Collection || (value instanceof Object[] && !(value instanceof Resource[]))) {
Collection input = (value instanceof Collection ? (Collection) value : Arrays.asList((Object[]) value));
List merged = new ArrayList();
for (Iterator it = input.iterator(); it.hasNext();) {
Object element = it.next();
if (element instanceof String) {
// A location pattern: resolve it into a Resource array.
// Might point to a single resource or to multiple resources.
String pattern = resolvePath((String) element).trim();
try {
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
if (!merged.contains(resource)) {
merged.add(resource);
}
}
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
}
}
else if (element instanceof Resource) {
// A Resource object: add it to the result.
if (!merged.contains(element)) {
merged.add(element);
}
}
else {
throw new IllegalArgumentException("Cannot convert element [" + element + "] to [" +
Resource.class.getName() + "]: only location String and Resource object supported");
}
}
super.setValue(merged.toArray(new Resource[merged.size()]));
}
else {
// An arbitrary value: probably a String or a Resource array.
// setAsText will be called for a String; a Resource array will be used as-is.
super.setValue(value);
}
}
Treat the given value as collection or array and convert it to a Resource array.
Considers String elements as location patterns, and takes Resource elements as-is. |