001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.propertyeditors;
018
019import java.beans.PropertyEditorSupport;
020import java.io.IOException;
021
022import org.xml.sax.InputSource;
023
024import org.springframework.core.io.Resource;
025import org.springframework.core.io.ResourceEditor;
026import org.springframework.util.Assert;
027
028/**
029 * Editor for {@code org.xml.sax.InputSource}, converting from a
030 * Spring resource location String to a SAX InputSource object.
031 *
032 * <p>Supports Spring-style URL notation: any fully qualified standard URL
033 * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
034 *
035 * @author Juergen Hoeller
036 * @since 3.0.3
037 * @see org.xml.sax.InputSource
038 * @see org.springframework.core.io.ResourceEditor
039 * @see org.springframework.core.io.ResourceLoader
040 * @see URLEditor
041 * @see FileEditor
042 */
043public class InputSourceEditor extends PropertyEditorSupport {
044
045        private final ResourceEditor resourceEditor;
046
047
048        /**
049         * Create a new InputSourceEditor,
050         * using the default ResourceEditor underneath.
051         */
052        public InputSourceEditor() {
053                this.resourceEditor = new ResourceEditor();
054        }
055
056        /**
057         * Create a new InputSourceEditor,
058         * using the given ResourceEditor underneath.
059         * @param resourceEditor the ResourceEditor to use
060         */
061        public InputSourceEditor(ResourceEditor resourceEditor) {
062                Assert.notNull(resourceEditor, "ResourceEditor must not be null");
063                this.resourceEditor = resourceEditor;
064        }
065
066
067        @Override
068        public void setAsText(String text) throws IllegalArgumentException {
069                this.resourceEditor.setAsText(text);
070                Resource resource = (Resource) this.resourceEditor.getValue();
071                try {
072                        setValue(resource != null ? new InputSource(resource.getURL().toString()) : null);
073                }
074                catch (IOException ex) {
075                        throw new IllegalArgumentException(
076                                        "Could not retrieve URL for " + resource + ": " + ex.getMessage());
077                }
078        }
079
080        @Override
081        public String getAsText() {
082                InputSource value = (InputSource) getValue();
083                return (value != null ? value.getSystemId() : "");
084        }
085
086}