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 */
017package org.apache.xbean.propertyeditor;
018
019import java.io.File;
020import java.net.MalformedURLException;
021import java.net.URL;
022
023/**
024 * A property editor for URL typed properties.
025 *
026 * @version $Rev: 6680 $
027 */
028public class URLEditor extends AbstractConverter {
029    public URLEditor() {
030        super(URL.class);
031    }
032
033    /**
034     * Convert the text value of the property into a URL object instance.
035     *
036     * @return a URL object constructed from the property text value.
037     */
038    protected Object toObjectImpl(String text) {
039        try {
040            // try to create directly from the text property.
041            URL url = new URL(text);
042            // this parsed correctly, but if this is a file object,
043            // we need to make sure this gets converted into the proper
044            // absolute directory form.
045            try {
046                if (url.getProtocol().equals("file")) {
047                    // ok, this is a file URL, so get the file string portion,
048                    // convert that to a file object, then go through the URI()/URL()
049                    // conversion sequence to get a fully valid URL().
050                    return new File(url.getFile()).toURI().toURL();
051                }
052            } catch (Exception e) {
053                // any error here is returned as a property editor exception.
054                throw new PropertyEditorException(e);
055            }
056
057            return url;
058        } catch (MalformedURLException e) {
059            // this is a format error, but it could have been specified as a local
060            // file name. so try to create a file object and make a URL from that.
061        }
062
063        try {
064            // The file class has direct support for returning as a URL, but the Javadoc
065            // for File.toURL() recommends converting the File object to a URI first
066            // so that untranslatable characters get handled correctly.
067            return new File(text).toURI().toURL();
068        } catch (MalformedURLException e) {
069            // any error here is returned as a property editor exception.
070            throw new PropertyEditorException(e);
071        }
072    }
073}