1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 24 import org.springframework.core.io.ClassPathResource; 25 import org.springframework.util.ClassUtils; 26 import org.springframework.util.ResourceUtils; 27 28 48 public class URIEditor extends PropertyEditorSupport { 49 50 private final ClassLoader classLoader; 51 52 53 57 public URIEditor() { 58 this.classLoader = ClassUtils.getDefaultClassLoader(); 59 } 60 61 66 public URIEditor(ClassLoader classLoader) { 67 this.classLoader = classLoader; 68 } 69 70 71 public void setAsText(String text) throws IllegalArgumentException { 72 if (text == null) { 73 setValue(null); 74 } 75 else if (text.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) { 76 ClassPathResource resource = 77 new ClassPathResource(text.substring(ResourceUtils.CLASSPATH_URL_PREFIX.length()), this.classLoader); 78 try { 79 setValue(new URI (resource.getURL().toString())); 80 } 81 catch (IOException ex) { 82 throw new IllegalArgumentException ("Could not retrieve URI for " + resource + ": " + ex.getMessage()); 83 } 84 catch (URISyntaxException ex) { 85 throw new IllegalArgumentException ("Invalid URI syntax: " + ex); 86 } 87 } 88 else { 89 try { 90 setValue(new URI (text)); 91 } 92 catch (URISyntaxException ex) { 93 throw new IllegalArgumentException ("Invalid URI syntax: " + ex); 94 } 95 } 96 } 97 98 public String getAsText() { 99 URI value = (URI ) getValue(); 100 return (value != null ? value.toString() : ""); 101 } 102 103 } 104 | Popular Tags |