parent
9704b809b1
commit
25537938d6
|
|
@ -18,12 +18,10 @@ package org.springframework.core.io.support;
|
||||||
|
|
||||||
import java.beans.PropertyEditorSupport;
|
import java.beans.PropertyEditorSupport;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
|
@ -54,6 +52,7 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Yanming Zhou
|
* @author Yanming Zhou
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 1.1.2
|
* @since 1.1.2
|
||||||
* @see org.springframework.core.io.Resource
|
* @see org.springframework.core.io.Resource
|
||||||
* @see ResourcePatternResolver
|
* @see ResourcePatternResolver
|
||||||
|
|
@ -112,35 +111,32 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Treat the given text as a location pattern or comma delimited location patterns and convert it to a Resource array.
|
* Treat the given text as a location pattern or comma delimited location patterns
|
||||||
|
* and convert it to a Resource array.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setAsText(String text) {
|
public void setAsText(String text) {
|
||||||
String pattern = resolvePath(text).trim();
|
String pattern = resolvePath(text).trim();
|
||||||
if (pattern.indexOf(',') > 0) {
|
String[] locationPatterns = StringUtils.commaDelimitedListToStringArray(pattern);
|
||||||
List<Resource> resources = new ArrayList<>();
|
if (locationPatterns.length == 1) {
|
||||||
for (String locationPattern : StringUtils.commaDelimitedListToStringArray(pattern)) {
|
setValue(getResources(locationPatterns[0]));
|
||||||
try {
|
|
||||||
for (Resource resource : this.resourcePatternResolver.getResources(locationPattern)) {
|
|
||||||
resources.add(resource);
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Resource[] resources = Arrays.stream(locationPatterns).map(String::trim)
|
||||||
|
.map(this::getResources).flatMap(Arrays::stream).toArray(Resource[]::new);
|
||||||
|
setValue(resources);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Resource[] getResources(String locationPattern) {
|
||||||
|
try {
|
||||||
|
return this.resourcePatternResolver.getResources(locationPattern);
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Could not resolve resource location pattern [" + locationPattern + "]: " + ex.getMessage());
|
"Could not resolve resource location pattern [" + locationPattern + "]: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setValue(resources.toArray());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
setValue(this.resourcePatternResolver.getResources(pattern));
|
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Treat the given value as a collection or array and convert it to a Resource array.
|
* Treat the given value as a collection or array and convert it to a Resource array.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -29,9 +29,12 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Tests for {@link ResourceArrayPropertyEditor}.
|
||||||
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Yanming Zhou
|
* @author Yanming Zhou
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
class ResourceArrayPropertyEditorTests {
|
class ResourceArrayPropertyEditorTests {
|
||||||
|
|
||||||
|
|
@ -87,12 +90,30 @@ class ResourceArrayPropertyEditorTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void commaDelimitedResources() {
|
void commaDelimitedResourcesWithSingleResource() {
|
||||||
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
||||||
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class,file:/foo/bar.txt");
|
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class,file:/test.txt");
|
||||||
Resource[] resources = (Resource[]) editor.getValue();
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
assertThat(resources).isNotNull();
|
assertThat(resources).isNotNull();
|
||||||
assertThat(resources[0]).isInstanceOf(ClassPathResource.class);
|
assertThat(resources[0]).isInstanceOfSatisfying(ClassPathResource.class,
|
||||||
assertThat(resources[1]).isInstanceOf(FileUrlResource.class);
|
resource -> assertThat(resource.exists()).isTrue());
|
||||||
|
assertThat(resources[1]).isInstanceOfSatisfying(FileUrlResource.class,
|
||||||
|
resource -> assertThat(resource.getFilename()).isEqualTo("test.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void commaDelimitedResourcesWithMultipleResources() {
|
||||||
|
PropertyEditor editor = new ResourceArrayPropertyEditor();
|
||||||
|
editor.setAsText("file:/test.txt, classpath:org/springframework/core/io/support/test-resources/*.txt");
|
||||||
|
Resource[] resources = (Resource[]) editor.getValue();
|
||||||
|
assertThat(resources).isNotNull();
|
||||||
|
assertThat(resources[0]).isInstanceOfSatisfying(FileUrlResource.class,
|
||||||
|
resource -> assertThat(resource.getFilename()).isEqualTo("test.txt"));
|
||||||
|
assertThat(resources).anySatisfy(candidate ->
|
||||||
|
assertThat(candidate.getFilename()).isEqualTo("resource1.txt"));
|
||||||
|
assertThat(resources).anySatisfy(candidate ->
|
||||||
|
assertThat(candidate.getFilename()).isEqualTo("resource2.txt"));
|
||||||
|
assertThat(resources).hasSize(3);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue