1 16 17 package org.springframework.core.io; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.io.IOException ; 21 22 import org.springframework.util.Assert; 23 import org.springframework.util.StringUtils; 24 import org.springframework.util.SystemPropertyUtils; 25 26 47 public class ResourceEditor extends PropertyEditorSupport { 48 49 private final ResourceLoader resourceLoader; 50 51 52 56 public ResourceEditor() { 57 this(new DefaultResourceLoader()); 58 } 59 60 65 public ResourceEditor(ResourceLoader resourceLoader) { 66 Assert.notNull(resourceLoader, "ResourceLoader must not be null"); 67 this.resourceLoader = resourceLoader; 68 } 69 70 71 public void setAsText(String text) { 72 if (StringUtils.hasText(text)) { 73 String locationToUse = resolvePath(text).trim(); 74 setValue(this.resourceLoader.getResource(locationToUse)); 75 } 76 else { 77 setValue(null); 78 } 79 } 80 81 88 protected String resolvePath(String path) { 89 return SystemPropertyUtils.resolvePlaceholders(path); 90 } 91 92 93 public String getAsText() { 94 Resource value = (Resource) getValue(); 95 try { 96 return (value != null ? value.getURL().toExternalForm() : ""); 98 } 99 catch (IOException ex) { 100 return null; 103 } 104 } 105 106 } 107 | Popular Tags |