All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.util.converter.URLConverter Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
/*
 * #%L
 * Nuiton Utils
 * %%
 * Copyright (C) 2004 - 2010 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package org.nuiton.util.converter;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;

import java.net.MalformedURLException;
import java.net.URL;

import static org.apache.commons.logging.LogFactory.getLog;
import static org.nuiton.i18n.I18n.t;

/**
 * classe pour convertir une chaine en un objet URL.
 *
 * @author Tony Chemit - [email protected]
 * @since 1.3 (replace {@code org.nuiton.util.URLConverter}).
 * @deprecated since 3.0 use instead {@link org.nuiton.converter.URLConverter} (Note: This converter is no more loaded by the {@link ConverterUtil#initConverters()})
 */
@Deprecated
public class URLConverter implements Converter {

    /** Logger. */
    private static final Log log = getLog(URLConverter.class);

    @Override
    public Object convert(Class aClass, Object value) {
        if (value == null) {
            throw new ConversionException(
                    t("nuitonutil.error.convertor.noValue", this));
        }
        if (isEnabled(aClass)) {
            Object result;
            if (isEnabled(value.getClass())) {
                result = value;
                return result;
            }
            if (value instanceof String) {
                result = valueOf((String) value);
                return result;
            }
        }
        throw new ConversionException(
                t("nuitonutil.error.no.convertor", aClass.getName(), value));
    }

    protected URL valueOf(String value) {
        try {
            URL result;
            result = new URL(value);
            return result;
        } catch (MalformedURLException e) {
            throw new ConversionException(
                    t("nuitonutil.error.url.convertor", value, this, e.getMessage()));
        }
    }

    public URLConverter() {
        if (log.isDebugEnabled()) {
            log.debug("init url converter " + this);
        }
    }

    protected boolean isEnabled(Class aClass) {
        return URL.class.equals(aClass);
    }

    public Class getType() {
        return URL.class;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy