KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > ResourceEditor


1 /*
2  * Copyright 2002-2007 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.core.io;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24 import org.springframework.util.SystemPropertyUtils;
25
26 /**
27  * {@link java.beans.PropertyEditor Editor} for {@link Resource}
28  * descriptors, to automatically convert <code>String</code> locations
29  * e.g. <code>"file:C:/myfile.txt"</code> or
30  * <code>"classpath:myfile.txt"</code>) to <code>Resource</code>
31  * properties instead of using a <code>String</code> location property.
32  *
33  * <p>The path may contain <code>${...}</code> placeholders, to be resolved
34  * as system properties: e.g. <code>${user.dir}</code>.
35  *
36  * <p>Delegates to a {@link ResourceLoader} to do the heavy lifting,
37  * by default using a {@link DefaultResourceLoader}.
38  *
39  * @author Juergen Hoeller
40  * @since 28.12.2003
41  * @see Resource
42  * @see ResourceLoader
43  * @see DefaultResourceLoader
44  * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
45  * @see System#getProperty(String)
46  */

47 public class ResourceEditor extends PropertyEditorSupport JavaDoc {
48
49     private final ResourceLoader resourceLoader;
50
51
52     /**
53      * Create a new instance of the {@link ResourceEditor} class
54      * using a {@link DefaultResourceLoader}.
55      */

56     public ResourceEditor() {
57         this(new DefaultResourceLoader());
58     }
59
60     /**
61      * Create a new instance of the {@link ResourceEditor} class
62      * using the given {@link ResourceLoader}.
63      * @param resourceLoader the <code>ResourceLoader</code> to use
64      */

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 JavaDoc text) {
72         if (StringUtils.hasText(text)) {
73             String JavaDoc locationToUse = resolvePath(text).trim();
74             setValue(this.resourceLoader.getResource(locationToUse));
75         }
76         else {
77             setValue(null);
78         }
79     }
80
81     /**
82      * Resolve the given path, replacing placeholders with
83      * corresponding system property values if necessary.
84      * @param path the original file path
85      * @return the resolved file path
86      * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
87      */

88     protected String JavaDoc resolvePath(String JavaDoc path) {
89         return SystemPropertyUtils.resolvePlaceholders(path);
90     }
91
92
93     public String JavaDoc getAsText() {
94         Resource value = (Resource) getValue();
95         try {
96             // Try to determine URL for resource.
97
return (value != null ? value.getURL().toExternalForm() : "");
98         }
99         catch (IOException JavaDoc ex) {
100             // Couldn't determine resource URL - return null to indicate
101
// that there is no appropriate text representation.
102
return null;
103         }
104     }
105
106 }
107
Popular Tags