29 * However, the following notice accompanied the original version of this |
29 * However, the following notice accompanied the original version of this |
30 * file: |
30 * file: |
31 * |
31 * |
32 * The MIT License |
32 * The MIT License |
33 * |
33 * |
34 * Copyright (c) 2004-2014 Paul R. Holser, Jr. |
34 * Copyright (c) 2004-2015 Paul R. Holser, Jr. |
35 * |
35 * |
36 * Permission is hereby granted, free of charge, to any person obtaining |
36 * Permission is hereby granted, free of charge, to any person obtaining |
37 * a copy of this software and associated documentation files (the |
37 * a copy of this software and associated documentation files (the |
38 * "Software"), to deal in the Software without restriction, including |
38 * "Software"), to deal in the Software without restriction, including |
39 * without limitation the rights to use, copy, modify, merge, publish, |
39 * without limitation the rights to use, copy, modify, merge, publish, |
55 |
55 |
56 package jdk.internal.joptsimple.util; |
56 package jdk.internal.joptsimple.util; |
57 |
57 |
58 import java.net.InetAddress; |
58 import java.net.InetAddress; |
59 import java.net.UnknownHostException; |
59 import java.net.UnknownHostException; |
|
60 import java.util.Locale; |
60 |
61 |
61 import jdk.internal.joptsimple.ValueConversionException; |
62 import jdk.internal.joptsimple.ValueConversionException; |
62 import jdk.internal.joptsimple.ValueConverter; |
63 import jdk.internal.joptsimple.ValueConverter; |
|
64 import jdk.internal.joptsimple.internal.Messages; |
63 |
65 |
64 /** |
66 /** |
65 * Converts values to {@link java.net.InetAddress} using {@link InetAddress#getByName(String) getByName}. |
67 * Converts values to {@link java.net.InetAddress} using {@link InetAddress#getByName(String) getByName}. |
66 * |
68 * |
67 * @author <a href="mailto:r@ymund.de">Raymund F\u00FCl\u00F6p</a> |
69 * @author <a href="mailto:r@ymund.de">Raymund F\u00FCl\u00F6p</a> |
68 */ |
70 */ |
69 public class InetAddressConverter implements ValueConverter<InetAddress> { |
71 public class InetAddressConverter implements ValueConverter<InetAddress> { |
70 public InetAddress convert( String value ) { |
72 public InetAddress convert( String value ) { |
71 try { |
73 try { |
72 return InetAddress.getByName( value ); |
74 return InetAddress.getByName( value ); |
73 } catch ( UnknownHostException e ) { |
75 } |
74 throw new ValueConversionException( "Cannot convert value [" + value + " into an InetAddress", e ); |
76 catch ( UnknownHostException e ) { |
|
77 throw new ValueConversionException( message( value ) ); |
75 } |
78 } |
76 } |
79 } |
77 |
80 |
78 public Class<InetAddress> valueType() { |
81 public Class<InetAddress> valueType() { |
79 return InetAddress.class; |
82 return InetAddress.class; |
80 } |
83 } |
81 |
84 |
82 public String valuePattern() { |
85 public String valuePattern() { |
83 return null; |
86 return null; |
84 } |
87 } |
|
88 |
|
89 private String message( String value ) { |
|
90 return Messages.message( |
|
91 Locale.getDefault(), |
|
92 "jdk.internal.joptsimple.ExceptionMessages", |
|
93 InetAddressConverter.class, |
|
94 "message", |
|
95 value ); |
|
96 } |
85 } |
97 } |