Support XML properties in ResourcePropertySource
JDK 5 introduced an XML-based properties file syntax. This commit ensures that when such files are supplied as the underlying resource for a ResourcePropertySource instance, they are routed appropriately to Properties#loadFromXML as opposed to Properties#load. Issue: SPR-9896
This commit is contained in:
parent
2a41de00e3
commit
3a626f9319
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -28,7 +28,11 @@ import org.springframework.util.StringUtils;
|
|||
/**
|
||||
* Subclass of {@link PropertiesPropertySource} that loads a {@link Properties}
|
||||
* object from a given {@link org.springframework.core.io.Resource} or resource location such as
|
||||
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.properties"}.
|
||||
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.xml"}.
|
||||
* Both traditional and XML-based properties file formats are supported, however in order
|
||||
* for XML processing to take effect, the underlying {@code Resource}'s
|
||||
* {@link org.springframework.core.io.Resource#getFilename() getFilename()} method must
|
||||
* return non-{@code null} and end in ".xml".
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
|
|
@ -99,7 +103,13 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
|||
private static Properties loadPropertiesForResource(Resource resource) throws IOException {
|
||||
Properties props = new Properties();
|
||||
InputStream is = resource.getInputStream();
|
||||
props.load(is);
|
||||
String filename = resource.getFilename();
|
||||
if (filename != null && filename.endsWith(".xml")) {
|
||||
props.loadFromXML(is);
|
||||
}
|
||||
else {
|
||||
props.load(is);
|
||||
}
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException ex) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
<properties version="1.0">
|
||||
<entry key="foo">bar</entry>
|
||||
</properties>
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -39,6 +39,10 @@ public class ResourcePropertySourceTests {
|
|||
private static final String PROPERTIES_LOCATION = "classpath:" + PROPERTIES_PATH;
|
||||
private static final String PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + PROPERTIES_PATH + "]";
|
||||
|
||||
private static final String XML_PROPERTIES_PATH = "org/springframework/core/io/example.xml";
|
||||
private static final String XML_PROPERTIES_LOCATION = "classpath:" + XML_PROPERTIES_PATH;
|
||||
private static final String XML_PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + XML_PROPERTIES_PATH + "]";
|
||||
|
||||
@Test
|
||||
public void withLocationAndGeneratedName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION);
|
||||
|
|
@ -46,6 +50,13 @@ public class ResourcePropertySourceTests {
|
|||
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlWithLocationAndGeneratedName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION);
|
||||
assertEquals(ps.getProperty("foo"), "bar");
|
||||
assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withLocationAndExplicitName() throws IOException {
|
||||
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION);
|
||||
|
|
|
|||
Loading…
Reference in New Issue