001package org.unix4j.convert;
002
003import java.net.MalformedURLException;
004import java.net.URI;
005import java.net.URL;
006
007public class URLConverters {
008        public static final ValueConverter<URL> URI = new ValueConverter<URL>() {
009                @Override
010                public URL convert(Object value) throws IllegalArgumentException {
011                        if (value instanceof URI) {
012                                try {
013                                        return ((URI)value).toURL();
014                                } catch (Exception E) {
015                                        return null;
016                                }
017                        }
018                        return null;
019                }
020        };
021        public static final ValueConverter<URL> STRING = new ValueConverter<URL>() {
022                @Override
023                public URL convert(Object value) throws IllegalArgumentException {
024                        if (value != null) {
025                                try {
026                                        return new URL(value.toString());
027                                } catch (MalformedURLException e) {
028                                        return null;
029                                }
030                        }
031                        return null;
032                }
033        };
034        public static final ValueConverter<URL> DEFAULT = new CompositeValueConverter<URL>().add(URI).add(STRING);
035}