View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  
23  /**
24   * A property editor for URL typed properties.
25   *
26   * @version $Rev: 356022 $
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 getValue() {
35          try {
36              // try to create directly from the text property.
37              URL url = new URL(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(url.getFile()).toURI().toURL();
47                  }
48              } catch (Exception 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 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(getAsText()).toURI().toURL();
64          } catch (MalformedURLException e) {
65              // any error here is returned as a property editor exception.
66              throw new PropertyEditorException(e);
67          }
68      }
69  }