|
| 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 | +} |
0 commit comments