001/* 002 * Copyright 2002-2016 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; 021import java.net.URL; 022 023import org.springframework.core.io.Resource; 024import org.springframework.core.io.ResourceEditor; 025import org.springframework.util.Assert; 026 027/** 028 * Editor for {@code java.net.URL}, to directly populate a URL property 029 * instead of using a String property as bridge. 030 * 031 * <p>Supports Spring-style URL notation: any fully qualified standard URL 032 * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL, 033 * as well as Spring's context-specific relative file paths. 034 * 035 * <p>Note: A URL must specify a valid protocol, else it will be rejected 036 * upfront. However, the target resource does not necessarily have to exist 037 * at the time of URL creation; this depends on the specific resource type. 038 * 039 * @author Juergen Hoeller 040 * @since 15.12.2003 041 * @see java.net.URL 042 * @see org.springframework.core.io.ResourceEditor 043 * @see org.springframework.core.io.ResourceLoader 044 * @see FileEditor 045 * @see InputStreamEditor 046 */ 047public class URLEditor extends PropertyEditorSupport { 048 049 private final ResourceEditor resourceEditor; 050 051 052 /** 053 * Create a new URLEditor, using a default ResourceEditor underneath. 054 */ 055 public URLEditor() { 056 this.resourceEditor = new ResourceEditor(); 057 } 058 059 /** 060 * Create a new URLEditor, using the given ResourceEditor underneath. 061 * @param resourceEditor the ResourceEditor to use 062 */ 063 public URLEditor(ResourceEditor resourceEditor) { 064 Assert.notNull(resourceEditor, "ResourceEditor must not be null"); 065 this.resourceEditor = resourceEditor; 066 } 067 068 069 @Override 070 public void setAsText(String text) throws IllegalArgumentException { 071 this.resourceEditor.setAsText(text); 072 Resource resource = (Resource) this.resourceEditor.getValue(); 073 try { 074 setValue(resource != null ? resource.getURL() : null); 075 } 076 catch (IOException ex) { 077 throw new IllegalArgumentException("Could not retrieve URL for " + resource + ": " + ex.getMessage()); 078 } 079 } 080 081 @Override 082 public String getAsText() { 083 URL value = (URL) getValue(); 084 return (value != null ? value.toExternalForm() : ""); 085 } 086 087}