1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.io.IOException ; 21 import java.net.URL ; 22 23 import org.springframework.core.io.Resource; 24 import org.springframework.core.io.ResourceEditor; 25 import org.springframework.util.Assert; 26 27 47 public class URLEditor extends PropertyEditorSupport { 48 49 private final ResourceEditor resourceEditor; 50 51 52 55 public URLEditor() { 56 this.resourceEditor = new ResourceEditor(); 57 } 58 59 63 public URLEditor(ResourceEditor resourceEditor) { 64 Assert.notNull(resourceEditor, "ResourceEditor must not be null"); 65 this.resourceEditor = resourceEditor; 66 } 67 68 69 public void setAsText(String text) throws IllegalArgumentException { 70 this.resourceEditor.setAsText(text); 71 Resource resource = (Resource) this.resourceEditor.getValue(); 72 try { 73 setValue(resource != null ? resource.getURL() : null); 74 } 75 catch (IOException ex) { 76 throw new IllegalArgumentException ("Could not retrieve URL for " + resource + ": " + ex.getMessage()); 77 } 78 } 79 80 public String getAsText() { 81 URL value = (URL ) getValue(); 82 return (value != null ? value.toExternalForm() : ""); 83 } 84 85 } 86 | Popular Tags |