Skip to content

Commit 3a626f9

Browse files
committed
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
1 parent 2a41de0 commit 3a626f9

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

spring-core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,11 @@
2828
/**
2929
* Subclass of {@link PropertiesPropertySource} that loads a {@link Properties}
3030
* object from a given {@link org.springframework.core.io.Resource} or resource location such as
31-
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.properties"}.
31+
* {@code "classpath:/com/myco/foo.properties"} or {@code "file:/path/to/file.xml"}.
32+
* Both traditional and XML-based properties file formats are supported, however in order
33+
* for XML processing to take effect, the underlying {@code Resource}'s
34+
* {@link org.springframework.core.io.Resource#getFilename() getFilename()} method must
35+
* return non-{@code null} and end in ".xml".
3236
*
3337
* @author Chris Beams
3438
* @since 3.1
@@ -99,7 +103,13 @@ private static Resource getResourceForLocation(String location, ClassLoader clas
99103
private static Properties loadPropertiesForResource(Resource resource) throws IOException {
100104
Properties props = new Properties();
101105
InputStream is = resource.getInputStream();
102-
props.load(is);
106+
String filename = resource.getFilename();
107+
if (filename != null && filename.endsWith(".xml")) {
108+
props.loadFromXML(is);
109+
}
110+
else {
111+
props.load(is);
112+
}
103113
try {
104114
is.close();
105115
} catch (IOException ex) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties version="1.0">
4+
<entry key="foo">bar</entry>
5+
</properties>
6+

spring-core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,13 +39,24 @@ public class ResourcePropertySourceTests {
3939
private static final String PROPERTIES_LOCATION = "classpath:" + PROPERTIES_PATH;
4040
private static final String PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + PROPERTIES_PATH + "]";
4141

42+
private static final String XML_PROPERTIES_PATH = "org/springframework/core/io/example.xml";
43+
private static final String XML_PROPERTIES_LOCATION = "classpath:" + XML_PROPERTIES_PATH;
44+
private static final String XML_PROPERTIES_RESOURCE_DESCRIPTION = "class path resource [" + XML_PROPERTIES_PATH + "]";
45+
4246
@Test
4347
public void withLocationAndGeneratedName() throws IOException {
4448
PropertySource<?> ps = new ResourcePropertySource(PROPERTIES_LOCATION);
4549
assertEquals(ps.getProperty("foo"), "bar");
4650
assertThat(ps.getName(), is(PROPERTIES_RESOURCE_DESCRIPTION));
4751
}
4852

53+
@Test
54+
public void xmlWithLocationAndGeneratedName() throws IOException {
55+
PropertySource<?> ps = new ResourcePropertySource(XML_PROPERTIES_LOCATION);
56+
assertEquals(ps.getProperty("foo"), "bar");
57+
assertThat(ps.getName(), is(XML_PROPERTIES_RESOURCE_DESCRIPTION));
58+
}
59+
4960
@Test
5061
public void withLocationAndExplicitName() throws IOException {
5162
PropertySource<?> ps = new ResourcePropertySource("ps1", PROPERTIES_LOCATION);

0 commit comments

Comments
 (0)