From b28310b05110edcf81cba09247a2f138464d28c6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 22 Apr 2010 19:02:05 +0000 Subject: [PATCH] added default InputSourceEditor for SAX InputSource construction with a URL String as system id (SPR-7099) --- .../beans/PropertyEditorRegistrySupport.java | 4 + .../propertyeditors/InputSourceEditor.java | 86 +++++++++++++++++++ .../propertyeditors/InputStreamEditor.java | 9 +- .../support/ResourceEditorRegistrar.java | 4 + 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputSourceEditor.java diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java index 669806bbef5..0735f2003cc 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java @@ -42,6 +42,8 @@ import java.util.TimeZone; import java.util.UUID; import java.util.regex.Pattern; +import org.xml.sax.InputSource; + import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor; import org.springframework.beans.propertyeditors.CharArrayPropertyEditor; import org.springframework.beans.propertyeditors.CharacterEditor; @@ -54,6 +56,7 @@ import org.springframework.beans.propertyeditors.CustomCollectionEditor; import org.springframework.beans.propertyeditors.CustomMapEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.FileEditor; +import org.springframework.beans.propertyeditors.InputSourceEditor; import org.springframework.beans.propertyeditors.InputStreamEditor; import org.springframework.beans.propertyeditors.LocaleEditor; import org.springframework.beans.propertyeditors.PatternEditor; @@ -169,6 +172,7 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { this.defaultEditors.put(Currency.class, new CurrencyEditor()); this.defaultEditors.put(File.class, new FileEditor()); this.defaultEditors.put(InputStream.class, new InputStreamEditor()); + this.defaultEditors.put(InputSource.class, new InputSourceEditor()); this.defaultEditors.put(Locale.class, new LocaleEditor()); this.defaultEditors.put(Pattern.class, new PatternEditor()); this.defaultEditors.put(Properties.class, new PropertiesEditor()); diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputSourceEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputSourceEditor.java new file mode 100644 index 00000000000..3701b55cc3d --- /dev/null +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputSourceEditor.java @@ -0,0 +1,86 @@ +/* + * Copyright 2002-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.propertyeditors; + +import java.beans.PropertyEditorSupport; +import java.io.IOException; + +import org.xml.sax.InputSource; + +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceEditor; +import org.springframework.util.Assert; + +/** + * Editor for org.xml.sax.InputSource, converting from a + * Spring resource location String to a SAX InputSource object. + * + *

Supports Spring-style URL notation: any fully qualified standard URL + * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL. + * + * @author Juergen Hoeller + * @since 3.0.3 + * @see org.xml.sax.InputSource + * @see org.springframework.core.io.ResourceEditor + * @see org.springframework.core.io.ResourceLoader + * @see org.springframework.beans.propertyeditors.URLEditor + * @see org.springframework.beans.propertyeditors.FileEditor + */ +public class InputSourceEditor extends PropertyEditorSupport { + + private final ResourceEditor resourceEditor; + + + /** + * Create a new InputSourceEditor, + * using the default ResourceEditor underneath. + */ + public InputSourceEditor() { + this.resourceEditor = new ResourceEditor(); + } + + /** + * Create a new InputSourceEditor, + * using the given ResourceEditor underneath. + * @param resourceEditor the ResourceEditor to use + */ + public InputSourceEditor(ResourceEditor resourceEditor) { + Assert.notNull(resourceEditor, "ResourceEditor must not be null"); + this.resourceEditor = resourceEditor; + } + + + @Override + public void setAsText(String text) throws IllegalArgumentException { + this.resourceEditor.setAsText(text); + Resource resource = (Resource) this.resourceEditor.getValue(); + try { + setValue(resource != null ? new InputSource(resource.getURL().toString()) : null); + } + catch (IOException ex) { + throw new IllegalArgumentException( + "Could not retrieve URL for " + resource + ": " + ex.getMessage()); + } + } + + @Override + public String getAsText() { + InputSource value = (InputSource) getValue(); + return (value != null ? value.getSystemId() : ""); + } + +} diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputStreamEditor.java b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputStreamEditor.java index c9e2c4dc827..7099897730c 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputStreamEditor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/InputStreamEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2010 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. @@ -14,7 +14,6 @@ * limitations under the License. */ - package org.springframework.beans.propertyeditors; import java.beans.PropertyEditorSupport; @@ -25,9 +24,9 @@ import org.springframework.core.io.ResourceEditor; import org.springframework.util.Assert; /** - * One-way PropertyEditor, which can convert from a text string to a - * java.io.InputStream, allowing InputStream properties - * to be set directly as a text string. + * One-way PropertyEditor which can convert from a text String to a + * java.io.InputStream, interpreting the given String + * as Spring resource location (e.g. a URL String). * *

Supports Spring-style URL notation: any fully qualified standard URL * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL. diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java b/org.springframework.beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java index 22c0e452a23..7bed0ba4b9c 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java @@ -21,10 +21,13 @@ import java.io.InputStream; import java.net.URI; import java.net.URL; +import org.xml.sax.InputSource; + import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.propertyeditors.ClassEditor; import org.springframework.beans.propertyeditors.FileEditor; +import org.springframework.beans.propertyeditors.InputSourceEditor; import org.springframework.beans.propertyeditors.InputStreamEditor; import org.springframework.beans.propertyeditors.URIEditor; import org.springframework.beans.propertyeditors.URLEditor; @@ -79,6 +82,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar { ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader); registry.registerCustomEditor(Resource.class, baseEditor); registry.registerCustomEditor(InputStream.class, new InputStreamEditor(baseEditor)); + registry.registerCustomEditor(InputSource.class, new InputSourceEditor(baseEditor)); registry.registerCustomEditor(File.class, new FileEditor(baseEditor)); registry.registerCustomEditor(URL.class, new URLEditor(baseEditor));