org.nuiton.util.converter.URIConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nuiton-utils Show documentation
Show all versions of nuiton-utils Show documentation
Library of usefull classes to be used in any project.
/*
* #%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.URI;
import java.net.URISyntaxException;
import static org.apache.commons.logging.LogFactory.getLog;
import static org.nuiton.i18n.I18n.t;
/**
* classe pour convertir une chaine en un objet URI.
*
* @author Tony Chemit - [email protected]
* @since 1.3 (replace {@code org.nuiton.util.URIConverter}).
* @deprecated since 3.0 use instead {@link org.nuiton.converter.URIConverter} (Note: This converter is no more loaded by the {@link ConverterUtil#initConverters()})
*/
@Deprecated
public class URIConverter implements Converter {
/** Logger. */
private static final Log log = getLog(URIConverter.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 URI valueOf(String value) {
try {
URI result;
result = new URI(value);
return result;
} catch (URISyntaxException e) {
throw new ConversionException(
t("nuitonutil.error.url.convertor", value, this, e.getMessage()));
}
}
public URIConverter() {
if (log.isDebugEnabled()) {
log.debug("init uri converter " + this);
}
}
protected boolean isEnabled(Class> aClass) {
return URI.class.equals(aClass);
}
public Class> getType() {
return URI.class;
}
}