From a420e845918a45078dc4a6bfba9872297bb90bff Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 22 Jan 2013 14:14:15 +0100 Subject: [PATCH] Backport "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 Backport-Commit: 3a626f93197d7f0fd4266d9877550a8d330017e5 --- .../core/io/support/ResourcePropertySource.java | 16 +++++++++++++--- .../java/org/springframework/core/io/example.xml | 6 ++++++ .../io/support/ResourcePropertySourceTests.java | 13 ++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 org.springframework.core/src/test/java/org/springframework/core/io/example.xml diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java index f67a4579705..705d8d44e71 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/support/ResourcePropertySource.java @@ -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) { diff --git a/org.springframework.core/src/test/java/org/springframework/core/io/example.xml b/org.springframework.core/src/test/java/org/springframework/core/io/example.xml new file mode 100644 index 00000000000..1d638537e10 --- /dev/null +++ b/org.springframework.core/src/test/java/org/springframework/core/io/example.xml @@ -0,0 +1,6 @@ + + + + bar + + diff --git a/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java index 1b77a9f542c..1bbc3cb2a1c 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/io/support/ResourcePropertySourceTests.java @@ -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);