1 16 17 package org.springframework.beans.propertyeditors; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.io.File ; 21 import java.io.IOException ; 22 23 import org.springframework.core.io.Resource; 24 import org.springframework.core.io.ResourceEditor; 25 import org.springframework.util.Assert; 26 import org.springframework.util.ResourceUtils; 27 import org.springframework.util.StringUtils; 28 29 49 public class FileEditor extends PropertyEditorSupport { 50 51 private final ResourceEditor resourceEditor; 52 53 54 58 public FileEditor() { 59 this.resourceEditor = new ResourceEditor(); 60 } 61 62 67 public FileEditor(ResourceEditor resourceEditor) { 68 Assert.notNull(resourceEditor, "ResourceEditor must not be null"); 69 this.resourceEditor = resourceEditor; 70 } 71 72 73 public void setAsText(String text) throws IllegalArgumentException { 74 if (StringUtils.hasText(text) && !ResourceUtils.isUrl(text)) { 77 File file = new File (text); 78 if (file.isAbsolute()) { 79 setValue(file); 80 return; 81 } 82 } 83 84 this.resourceEditor.setAsText(text); 86 Resource resource = (Resource) this.resourceEditor.getValue(); 87 try { 88 setValue(resource != null ? resource.getFile() : null); 89 } 90 catch (IOException ex) { 91 throw new IllegalArgumentException ( 92 "Could not retrieve File for " + resource + ": " + ex.getMessage()); 93 } 94 } 95 96 public String getAsText() { 97 File value = (File ) getValue(); 98 return (value != null ? value.getPath() : ""); 99 } 100 101 } 102 | Popular Tags |