001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.common.propertyeditor;
018    
019    import java.io.File;
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    
023    /**
024     * A property editor for URL typed properties.
025     *
026     * @version $Rev: 547737 $
027     */
028    public class URLEditor extends TextPropertyEditorSupport {
029        /**
030         * Convert the text value of the property into a URL object instance.
031         *
032         * @return a URL object constructed from the property text value.
033         */
034        public Object getValue() {
035            try {
036                // try to create directly from the text property.
037                URL url = new URL(getAsText().trim());
038                // this parsed correctly, but if this is a file object,
039                // we need to make sure this gets converted into the proper
040                // absolute directory form.
041                try {
042                    if (url.getProtocol().equals("file")) {
043                        // ok, this is a file URL, so get the file string portion,
044                        // convert that to a file object, then go through the URI()/URL()
045                        // conversion sequence to get a fully valid URL().
046                        return new File(url.getFile()).toURI().toURL();
047                    }
048                } catch (Exception e) {
049                    // any error here is returned as a property editor exception.
050                    throw new PropertyEditorException(e.getMessage(), e);
051                }
052    
053                return url;
054            } catch (MalformedURLException e) {
055                // this is a format error, but it could have been specified as a local
056                // file name. so try to create a file object and make a URL from that.
057            }
058    
059            try {
060                // The file class has direct support for returning as a URL, but the Javadoc
061                // for File.toURL() recommends converting the File object to a URI first
062                // so that untranslatable characters get handled correctly.
063                return new File(getAsText()).toURI().toURL();
064            } catch (MalformedURLException e) {
065                // any error here is returned as a property editor exception.
066                throw new PropertyEditorException(e.getMessage(), e);
067            }
068        }
069    }