added default InputSourceEditor for SAX InputSource construction with a URL String as system id (SPR-7099)

This commit is contained in:
Juergen Hoeller 2010-04-22 19:02:05 +00:00
parent 20d3085dc8
commit b28310b051
4 changed files with 98 additions and 5 deletions

View File

@ -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());

View File

@ -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 <code>org.xml.sax.InputSource</code>, converting from a
* Spring resource location String to a SAX InputSource object.
*
* <p>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() : "");
}
}

View File

@ -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
* <code>java.io.InputStream</code>, allowing InputStream properties
* to be set directly as a text string.
* One-way PropertyEditor which can convert from a text String to a
* <code>java.io.InputStream</code>, interpreting the given String
* as Spring resource location (e.g. a URL String).
*
* <p>Supports Spring-style URL notation: any fully qualified standard URL
* ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.

View File

@ -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));