KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > common > propertyeditor > URLEditor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.common.propertyeditor;
18
19 import java.io.File JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 /**
24  * A property editor for URL typed properties.
25  *
26  * @version $Rev: 476049 $
27  */

28 public class URLEditor extends TextPropertyEditorSupport {
29     /**
30      * Convert the text value of the property into a URL object instance.
31      *
32      * @return a URL object constructed from the property text value.
33      */

34     public Object JavaDoc getValue() {
35         try {
36             // try to create directly from the text property.
37
URL JavaDoc url = new URL JavaDoc(getAsText().trim());
38             // this parsed correctly, but if this is a file object,
39
// we need to make sure this gets converted into the proper
40
// absolute directory form.
41
try {
42                 if (url.getProtocol().equals("file")) {
43                     // ok, this is a file URL, so get the file string portion,
44
// convert that to a file object, then go through the URI()/URL()
45
// conversion sequence to get a fully valid URL().
46
return new File JavaDoc(url.getFile()).toURI().toURL();
47                 }
48             } catch (Exception JavaDoc e) {
49                 // any error here is returned as a property editor exception.
50
throw new PropertyEditorException(e);
51             }
52
53             return url;
54         } catch (MalformedURLException JavaDoc e) {
55             // this is a format error, but it could have been specified as a local
56
// file name. so try to create a file object and make a URL from that.
57
}
58
59         try {
60             // The file class has direct support for returning as a URL, but the Javadoc
61
// for File.toURL() recommends converting the File object to a URI first
62
// so that untranslatable characters get handled correctly.
63
return new File JavaDoc(getAsText()).toURI().toURL();
64         } catch (MalformedURLException JavaDoc e) {
65             // any error here is returned as a property editor exception.
66
throw new PropertyEditorException(e);
67         }
68     }
69 }
70
Popular Tags