001    /*******************************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     *******************************************************************************/
019    package org.apache.commons.convert;
020    
021    import java.io.IOException;
022    import java.net.InetAddress;
023    import java.net.MalformedURLException;
024    import java.net.URI;
025    import java.net.URISyntaxException;
026    import java.net.URL;
027    
028    /** java.net Converter classes. */
029    public class NetConverters implements ConverterLoader {
030    
031        public void loadConverters() {
032            Converters.loadContainedConverters(NetConverters.class);
033            Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
034            Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
035            Converters.registerConverter(new GenericSingletonToList<InetAddress>(InetAddress.class));
036            Converters.registerConverter(new GenericSingletonToList<URI>(URI.class));
037            Converters.registerConverter(new GenericSingletonToList<URL>(URL.class));
038            Converters.registerConverter(new GenericSingletonToSet<InetAddress>(InetAddress.class));
039            Converters.registerConverter(new GenericSingletonToSet<URI>(URI.class));
040            Converters.registerConverter(new GenericSingletonToSet<URL>(URL.class));
041        }
042    
043        /**
044         * An object that converts an <code>InetAddress</code> to a
045         * <code>String</code>.
046         */
047        public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
048            public InetAddressToString() {
049                super(InetAddress.class, String.class);
050            }
051    
052            public String convert(InetAddress obj) throws ConversionException {
053                String hostName = obj.getHostName();
054                if (hostName != null) return hostName;
055                return obj.getHostAddress();
056            }
057        }
058    
059        /**
060         * An object that converts a <code>String</code> to an
061         * <code>InetAddress</code>.
062         */
063        public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
064            public StringToInetAddress() {
065                super(String.class, InetAddress.class);
066            }
067    
068            public InetAddress convert(String obj) throws ConversionException {
069                try {
070                    return InetAddress.getByName(obj);
071                } catch (IOException e) {
072                    throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
073                }
074            }
075        }
076    
077        /**
078         * An object that converts a <code>String</code> to a
079         * <code>URI</code>.
080         */
081        public static class StringToURI extends AbstractConverter<String, URI> {
082            public StringToURI() {
083                super(String.class, URI.class);
084            }
085    
086            public URI convert(String obj) throws ConversionException {
087                try {
088                    return new URI(obj);
089                } catch (URISyntaxException e) {
090                    throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
091                }
092            }
093        }
094    
095        /**
096         * An object that converts a <code>String</code> to a
097         * <code>URL</code>.
098         */
099        public static class StringToURL extends AbstractConverter<String, URL> {
100            public StringToURL() {
101                super(String.class, URL.class);
102            }
103    
104            public URL convert(String obj) throws ConversionException {
105                try {
106                    return new URL(obj);
107                } catch (MalformedURLException e) {
108                    throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
109                }
110            }
111        }
112    
113        /**
114         * An object that converts a <code>URI</code> to a
115         * <code>URL</code>.
116         */
117        public static class URIToURL extends AbstractConverter<URI, URL> {
118            public URIToURL() {
119                super(URI.class, URL.class);
120            }
121    
122            public URL convert(URI obj) throws ConversionException {
123                try {
124                    return obj.toURL();
125                } catch (MalformedURLException e) {
126                    throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
127                }
128            }
129        }
130    
131        /**
132         * An object that converts a <code>URL</code> to a
133         * <code>URI</code>.
134         */
135        public static class URLToURI extends AbstractConverter<URL, URI> {
136            public URLToURI() {
137                super(URL.class, URI.class);
138            }
139    
140            public URI convert(URL obj) throws ConversionException {
141                try {
142                    return obj.toURI();
143                } catch (URISyntaxException e) {
144                    throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
145                }
146            }
147        }
148    }