Skip to content

Commit b28310b

Browse files
committed
added default InputSourceEditor for SAX InputSource construction with a URL String as system id (SPR-7099)
1 parent 20d3085 commit b28310b

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.UUID;
4343
import java.util.regex.Pattern;
4444

45+
import org.xml.sax.InputSource;
46+
4547
import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor;
4648
import org.springframework.beans.propertyeditors.CharArrayPropertyEditor;
4749
import org.springframework.beans.propertyeditors.CharacterEditor;
@@ -54,6 +56,7 @@
5456
import org.springframework.beans.propertyeditors.CustomMapEditor;
5557
import org.springframework.beans.propertyeditors.CustomNumberEditor;
5658
import org.springframework.beans.propertyeditors.FileEditor;
59+
import org.springframework.beans.propertyeditors.InputSourceEditor;
5760
import org.springframework.beans.propertyeditors.InputStreamEditor;
5861
import org.springframework.beans.propertyeditors.LocaleEditor;
5962
import org.springframework.beans.propertyeditors.PatternEditor;
@@ -169,6 +172,7 @@ private void doRegisterDefaultEditors() {
169172
this.defaultEditors.put(Currency.class, new CurrencyEditor());
170173
this.defaultEditors.put(File.class, new FileEditor());
171174
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
175+
this.defaultEditors.put(InputSource.class, new InputSourceEditor());
172176
this.defaultEditors.put(Locale.class, new LocaleEditor());
173177
this.defaultEditors.put(Pattern.class, new PatternEditor());
174178
this.defaultEditors.put(Properties.class, new PropertiesEditor());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.propertyeditors;
18+
19+
import java.beans.PropertyEditorSupport;
20+
import java.io.IOException;
21+
22+
import org.xml.sax.InputSource;
23+
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.core.io.ResourceEditor;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* Editor for <code>org.xml.sax.InputSource</code>, converting from a
30+
* Spring resource location String to a SAX InputSource object.
31+
*
32+
* <p>Supports Spring-style URL notation: any fully qualified standard URL
33+
* ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
34+
*
35+
* @author Juergen Hoeller
36+
* @since 3.0.3
37+
* @see org.xml.sax.InputSource
38+
* @see org.springframework.core.io.ResourceEditor
39+
* @see org.springframework.core.io.ResourceLoader
40+
* @see org.springframework.beans.propertyeditors.URLEditor
41+
* @see org.springframework.beans.propertyeditors.FileEditor
42+
*/
43+
public class InputSourceEditor extends PropertyEditorSupport {
44+
45+
private final ResourceEditor resourceEditor;
46+
47+
48+
/**
49+
* Create a new InputSourceEditor,
50+
* using the default ResourceEditor underneath.
51+
*/
52+
public InputSourceEditor() {
53+
this.resourceEditor = new ResourceEditor();
54+
}
55+
56+
/**
57+
* Create a new InputSourceEditor,
58+
* using the given ResourceEditor underneath.
59+
* @param resourceEditor the ResourceEditor to use
60+
*/
61+
public InputSourceEditor(ResourceEditor resourceEditor) {
62+
Assert.notNull(resourceEditor, "ResourceEditor must not be null");
63+
this.resourceEditor = resourceEditor;
64+
}
65+
66+
67+
@Override
68+
public void setAsText(String text) throws IllegalArgumentException {
69+
this.resourceEditor.setAsText(text);
70+
Resource resource = (Resource) this.resourceEditor.getValue();
71+
try {
72+
setValue(resource != null ? new InputSource(resource.getURL().toString()) : null);
73+
}
74+
catch (IOException ex) {
75+
throw new IllegalArgumentException(
76+
"Could not retrieve URL for " + resource + ": " + ex.getMessage());
77+
}
78+
}
79+
80+
@Override
81+
public String getAsText() {
82+
InputSource value = (InputSource) getValue();
83+
return (value != null ? value.getSystemId() : "");
84+
}
85+
86+
}

org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputStreamEditor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2006 the original author or authors.
2+
* Copyright 2002-2010 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.
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
package org.springframework.beans.propertyeditors;
1918

2019
import java.beans.PropertyEditorSupport;
@@ -25,9 +24,9 @@
2524
import org.springframework.util.Assert;
2625

2726
/**
28-
* One-way PropertyEditor, which can convert from a text string to a
29-
* <code>java.io.InputStream</code>, allowing InputStream properties
30-
* to be set directly as a text string.
27+
* One-way PropertyEditor which can convert from a text String to a
28+
* <code>java.io.InputStream</code>, interpreting the given String
29+
* as Spring resource location (e.g. a URL String).
3130
*
3231
* <p>Supports Spring-style URL notation: any fully qualified standard URL
3332
* ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.

org.springframework.beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import java.net.URI;
2222
import java.net.URL;
2323

24+
import org.xml.sax.InputSource;
25+
2426
import org.springframework.beans.PropertyEditorRegistrar;
2527
import org.springframework.beans.PropertyEditorRegistry;
2628
import org.springframework.beans.propertyeditors.ClassEditor;
2729
import org.springframework.beans.propertyeditors.FileEditor;
30+
import org.springframework.beans.propertyeditors.InputSourceEditor;
2831
import org.springframework.beans.propertyeditors.InputStreamEditor;
2932
import org.springframework.beans.propertyeditors.URIEditor;
3033
import org.springframework.beans.propertyeditors.URLEditor;
@@ -79,6 +82,7 @@ public void registerCustomEditors(PropertyEditorRegistry registry) {
7982
ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader);
8083
registry.registerCustomEditor(Resource.class, baseEditor);
8184
registry.registerCustomEditor(InputStream.class, new InputStreamEditor(baseEditor));
85+
registry.registerCustomEditor(InputSource.class, new InputSourceEditor(baseEditor));
8286
registry.registerCustomEditor(File.class, new FileEditor(baseEditor));
8387
registry.registerCustomEditor(URL.class, new URLEditor(baseEditor));
8488

0 commit comments

Comments
 (0)