KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23
24 import org.springframework.core.io.ClassPathResource;
25 import org.springframework.util.ClassUtils;
26 import org.springframework.util.ResourceUtils;
27
28 /**
29  * Editor for <code>java.net.URI</code>, to directly populate a URI property
30  * instead of using a String property as bridge.
31  *
32  * <p>Supports Spring-style URI notation: any fully qualified standard URI
33  * ("file:", "http:", etc) and Spring's special "classpath:" pseudo-URL,
34  * which will be resolved to a corresponding URI.
35  *
36  * <p>Note: A URI is more relaxed than a URL in that it does not require
37  * a valid protocol to be specified. Any scheme within a valid URI syntax
38  * is allowed, even without a matching protocol handler being registered.
39  *
40  * <p>Since <code>java.net.URI</code> is only available on JDK 1.4 or higher,
41  * this editor is only available on JDK 1.4 or higher as well.
42  *
43  * @author Juergen Hoeller
44  * @since 2.0.2
45  * @see java.net.URI
46  * @see URLEditor
47  */

48 public class URIEditor extends PropertyEditorSupport JavaDoc {
49
50     private final ClassLoader JavaDoc classLoader;
51
52
53     /**
54      * Create a new URIEditor,
55      * using the default ClassLoader for "classpath:" resources.
56      */

57     public URIEditor() {
58         this.classLoader = ClassUtils.getDefaultClassLoader();
59     }
60
61     /**
62      * Create a new URIEditor,
63      * using the given ClassLoader for "classpath:" resources.
64      * @param classLoader the ClassLoader to use
65      */

66     public URIEditor(ClassLoader JavaDoc classLoader) {
67         this.classLoader = classLoader;
68     }
69
70
71     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
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 JavaDoc(resource.getURL().toString()));
80             }
81             catch (IOException JavaDoc ex) {
82                 throw new IllegalArgumentException JavaDoc("Could not retrieve URI for " + resource + ": " + ex.getMessage());
83             }
84             catch (URISyntaxException JavaDoc ex) {
85                 throw new IllegalArgumentException JavaDoc("Invalid URI syntax: " + ex);
86             }
87         }
88         else {
89             try {
90                 setValue(new URI JavaDoc(text));
91             }
92             catch (URISyntaxException JavaDoc ex) {
93                 throw new IllegalArgumentException JavaDoc("Invalid URI syntax: " + ex);
94             }
95         }
96     }
97
98     public String JavaDoc getAsText() {
99         URI JavaDoc value = (URI JavaDoc) getValue();
100         return (value != null ? value.toString() : "");
101     }
102
103 }
104
Popular Tags