View Javadoc

1   /*******************************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   *******************************************************************************/
19  package org.apache.commons.convert;
20  
21  import java.io.IOException;
22  import java.net.InetAddress;
23  import java.net.MalformedURLException;
24  import java.net.URI;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  
28  /** java.net Converter classes. */
29  public class NetConverters implements ConverterLoader {
30  
31      public void loadConverters() {
32          Converters.loadContainedConverters(NetConverters.class);
33          Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
34          Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
35          Converters.registerConverter(new GenericSingletonToList<InetAddress>(InetAddress.class));
36          Converters.registerConverter(new GenericSingletonToList<URI>(URI.class));
37          Converters.registerConverter(new GenericSingletonToList<URL>(URL.class));
38          Converters.registerConverter(new GenericSingletonToSet<InetAddress>(InetAddress.class));
39          Converters.registerConverter(new GenericSingletonToSet<URI>(URI.class));
40          Converters.registerConverter(new GenericSingletonToSet<URL>(URL.class));
41      }
42  
43      /**
44       * An object that converts an <code>InetAddress</code> to a
45       * <code>String</code>.
46       */
47      public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
48          public InetAddressToString() {
49              super(InetAddress.class, String.class);
50          }
51  
52          public String convert(InetAddress obj) throws ConversionException {
53              String hostName = obj.getHostName();
54              if (hostName != null) return hostName;
55              return obj.getHostAddress();
56          }
57      }
58  
59      /**
60       * An object that converts a <code>String</code> to an
61       * <code>InetAddress</code>.
62       */
63      public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
64          public StringToInetAddress() {
65              super(String.class, InetAddress.class);
66          }
67  
68          public InetAddress convert(String obj) throws ConversionException {
69              try {
70                  return InetAddress.getByName(obj);
71              } catch (IOException e) {
72                  throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
73              }
74          }
75      }
76  
77      /**
78       * An object that converts a <code>String</code> to a
79       * <code>URI</code>.
80       */
81      public static class StringToURI extends AbstractConverter<String, URI> {
82          public StringToURI() {
83              super(String.class, URI.class);
84          }
85  
86          public URI convert(String obj) throws ConversionException {
87              try {
88                  return new URI(obj);
89              } catch (URISyntaxException e) {
90                  throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
91              }
92          }
93      }
94  
95      /**
96       * An object that converts a <code>String</code> to a
97       * <code>URL</code>.
98       */
99      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 }