KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > propertyeditors > FileEditor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.propertyeditors;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
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 /**
30  * Editor for <code>java.io.File</code>, to directly populate a File property
31  * from a Spring resource location.
32  *
33  * <p>Supports Spring-style URL notation: any fully qualified standard URL
34  * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL.
35  *
36  * <p><b>NOTE:</b> The behavior of this editor has changed in Spring 2.0.
37  * Previously, it created a File instance directly from a filename.
38  * As of Spring 2.0, it takes a standard Spring resource location as input;
39  * this is consistent with URLEditor and InputStreamEditor now.
40  *
41  * @author Juergen Hoeller
42  * @since 09.12.2003
43  * @see java.io.File
44  * @see org.springframework.core.io.ResourceEditor
45  * @see org.springframework.core.io.ResourceLoader
46  * @see URLEditor
47  * @see InputStreamEditor
48  */

49 public class FileEditor extends PropertyEditorSupport JavaDoc {
50
51     private final ResourceEditor resourceEditor;
52
53
54     /**
55      * Create a new FileEditor,
56      * using the default ResourceEditor underneath.
57      */

58     public FileEditor() {
59         this.resourceEditor = new ResourceEditor();
60     }
61
62     /**
63      * Create a new FileEditor,
64      * using the given ResourceEditor underneath.
65      * @param resourceEditor the ResourceEditor to use
66      */

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 JavaDoc text) throws IllegalArgumentException JavaDoc {
74         // Check whether we got an absolute file path without "file:" prefix.
75
// For backwards compatibility, we'll consider those as straight file path.
76
if (StringUtils.hasText(text) && !ResourceUtils.isUrl(text)) {
77             File JavaDoc file = new File JavaDoc(text);
78             if (file.isAbsolute()) {
79                 setValue(file);
80                 return;
81             }
82         }
83
84         // Proceed with standard resource location parsing.
85
this.resourceEditor.setAsText(text);
86         Resource resource = (Resource) this.resourceEditor.getValue();
87         try {
88             setValue(resource != null ? resource.getFile() : null);
89         }
90         catch (IOException JavaDoc ex) {
91             throw new IllegalArgumentException JavaDoc(
92                     "Could not retrieve File for " + resource + ": " + ex.getMessage());
93         }
94     }
95
96     public String JavaDoc getAsText() {
97         File JavaDoc value = (File JavaDoc) getValue();
98         return (value != null ? value.getPath() : "");
99     }
100
101 }
102
Popular Tags