1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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 | |
|
29 | 2 | public class NetConverters implements ConverterLoader { |
30 | |
|
31 | |
public void loadConverters() { |
32 | 2 | Converters.loadContainedConverters(NetConverters.class); |
33 | 2 | Converters.registerConverter(new GenericToStringConverter<URI>(URI.class)); |
34 | 2 | Converters.registerConverter(new GenericToStringConverter<URL>(URL.class)); |
35 | 2 | Converters.registerConverter(new GenericSingletonToList<InetAddress>(InetAddress.class)); |
36 | 2 | Converters.registerConverter(new GenericSingletonToList<URI>(URI.class)); |
37 | 2 | Converters.registerConverter(new GenericSingletonToList<URL>(URL.class)); |
38 | 2 | Converters.registerConverter(new GenericSingletonToSet<InetAddress>(InetAddress.class)); |
39 | 2 | Converters.registerConverter(new GenericSingletonToSet<URI>(URI.class)); |
40 | 2 | Converters.registerConverter(new GenericSingletonToSet<URL>(URL.class)); |
41 | 2 | } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 1 | public static class InetAddressToString extends AbstractConverter<InetAddress, String> { |
48 | |
public InetAddressToString() { |
49 | 3 | super(InetAddress.class, String.class); |
50 | 3 | } |
51 | |
|
52 | |
public String convert(InetAddress obj) throws ConversionException { |
53 | 1 | String hostName = obj.getHostName(); |
54 | 1 | if (hostName != null) return hostName; |
55 | 0 | return obj.getHostAddress(); |
56 | |
} |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 1 | public static class StringToInetAddress extends AbstractConverter<String, InetAddress> { |
64 | |
public StringToInetAddress() { |
65 | 2 | super(String.class, InetAddress.class); |
66 | 2 | } |
67 | |
|
68 | |
public InetAddress convert(String obj) throws ConversionException { |
69 | |
try { |
70 | 1 | return InetAddress.getByName(obj); |
71 | 0 | } catch (IOException e) { |
72 | 0 | throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); |
73 | |
} |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 1 | public static class StringToURI extends AbstractConverter<String, URI> { |
82 | |
public StringToURI() { |
83 | 2 | super(String.class, URI.class); |
84 | 2 | } |
85 | |
|
86 | |
public URI convert(String obj) throws ConversionException { |
87 | |
try { |
88 | 1 | return new URI(obj); |
89 | 0 | } catch (URISyntaxException e) { |
90 | 0 | throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); |
91 | |
} |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | 1 | public static class StringToURL extends AbstractConverter<String, URL> { |
100 | |
public StringToURL() { |
101 | 2 | super(String.class, URL.class); |
102 | 2 | } |
103 | |
|
104 | |
public URL convert(String obj) throws ConversionException { |
105 | |
try { |
106 | 1 | return new URL(obj); |
107 | 0 | } catch (MalformedURLException e) { |
108 | 0 | throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); |
109 | |
} |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 1 | public static class URIToURL extends AbstractConverter<URI, URL> { |
118 | |
public URIToURL() { |
119 | 3 | super(URI.class, URL.class); |
120 | 3 | } |
121 | |
|
122 | |
public URL convert(URI obj) throws ConversionException { |
123 | |
try { |
124 | 1 | return obj.toURL(); |
125 | 0 | } catch (MalformedURLException e) { |
126 | 0 | throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); |
127 | |
} |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | 1 | public static class URLToURI extends AbstractConverter<URL, URI> { |
136 | |
public URLToURI() { |
137 | 2 | super(URL.class, URI.class); |
138 | 2 | } |
139 | |
|
140 | |
public URI convert(URL obj) throws ConversionException { |
141 | |
try { |
142 | 1 | return obj.toURI(); |
143 | 0 | } catch (URISyntaxException e) { |
144 | 0 | throw (ConversionException) new ConversionException(e.getMessage()).initCause(e); |
145 | |
} |
146 | |
} |
147 | |
} |
148 | |
} |