Skip to content

Commit

Permalink
Added URL and URI
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Feb 14, 2024
1 parent 13c5f58 commit e66620c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,18 @@ private static void buildFactoryConversions() {
CONVERSION_DB.put(pair(Locale.class, String.class), LocaleConversions::toString);
CONVERSION_DB.put(pair(URL.class, String.class), StringConversions::toString);
CONVERSION_DB.put(pair(URI.class, String.class), StringConversions::toString);


// URL conversions
CONVERSION_DB.put(pair(Void.class, URL.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(String.class, URL.class), StringConversions::toURL);
CONVERSION_DB.put(pair(Map.class, URL.class), MapConversions::toURL);

// URI Conversions
CONVERSION_DB.put(pair(Void.class, URI.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(String.class, URI.class), StringConversions::toURI);
CONVERSION_DB.put(pair(Map.class, URI.class), MapConversions::toURI);


// Duration conversions supported
CONVERSION_DB.put(pair(Void.class, Duration.class), VoidConversions::toNull);
CONVERSION_DB.put(pair(Duration.class, Duration.class), Converter::identity);
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/MapConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -84,6 +87,13 @@ final class MapConversions {
private static final String ID = "id";
public static final String LANGUAGE = "language";
public static final String VARIANT = "variant";
public static final String JAR = "jar";
public static final String AUTHORITY = "authority";
public static final String REF = "ref";
public static final String PORT = "port";
public static final String FILE = "file";
public static final String HOST = "host";
public static final String PROTOCOL = "protocol";
private static String COUNTRY = "country";

private MapConversions() {}
Expand Down Expand Up @@ -183,6 +193,18 @@ static Timestamp toTimestamp(Object from, Converter converter) {
return fromValueForMultiKey(map, converter, Timestamp.class, TIMESTAMP_PARAMS);
}

private static final String[] TIMEZONE_PARAMS = new String[] { ZONE };
static TimeZone toTimeZone(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
ConverterOptions options = converter.getOptions();

if (map.containsKey(ZONE)) {
return converter.convert(map.get(ZONE), TimeZone.class);
} else {
return fromValueForMultiKey(map, converter, TimeZone.class, TIMEZONE_PARAMS);
}
}

private static final String[] CALENDAR_PARAMS = new String[] { TIME, ZONE };
static Calendar toCalendar(Object from, Converter converter) {
Map<?, ?> map = (Map<?, ?>) from;
Expand Down Expand Up @@ -436,6 +458,54 @@ static Year toYear(Object from, Converter converter) {
return fromSingleKey(from, converter, YEAR, Year.class);
}

static URL toURL(Object from, Converter converter) {
Map<String, Object> map = (Map<String, Object>)from;
StringBuilder builder = new StringBuilder(20);

try {
if (map.containsKey(VALUE) || map.containsKey(V)) {
return fromValue(map, converter, URL.class);
}

String protocol = (String) map.get(PROTOCOL);
String host = (String) map.get(HOST);
String file = (String) map.get(FILE);
String authority = (String) map.get(AUTHORITY);
String ref = (String) map.get(REF);
Long port = (Long) map.get(PORT);

builder.append(protocol);
builder.append(':');
if (!protocol.equalsIgnoreCase(JAR)) {
builder.append("//");
}
if (authority != null && !authority.isEmpty()) {
builder.append(authority);
} else {
if (host != null && !host.isEmpty()) {
builder.append(host);
}
if (!port.equals(-1L)) {
builder.append(":" + port);
}
}
if (file != null && !file.isEmpty()) {
builder.append(file);
}
if (ref != null && !ref.isEmpty()) {
builder.append("#" + ref);
}
return URI.create(builder.toString()).toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Cannot convert Map to URL. Malformed URL: '" + builder + "'");
}
}

static URI toURI(Object from, Converter converter) {
Map<?, ?> map = asMap(from);
return fromValue(map, converter, URI.class);
}

static Map<String, ?> initMap(Object from, Converter converter) {
Map<String, Object> map = new CompactLinkedMap<>();
map.put(V, from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.sql.Timestamp;
Expand Down Expand Up @@ -241,6 +244,27 @@ static BigDecimal toBigDecimal(Object from, Converter converter) {
}
}

static URL toURL(Object from, Converter converter) {
String str = StringUtilities.trimToNull(asString(from));
if (str == null) {
return null;
}
try {
URI uri = URI.create((String) from);
return uri.toURL();
} catch (MalformedURLException mue) {
throw new IllegalArgumentException("Cannot convert String '" + str);
}
}

static URI toURI(Object from, Converter converter) {
String str = StringUtilities.trimToNull(asString(from));
if (str == null) {
return null;
}
return URI.create((String) from);
}

static String enumToString(Object from, Converter converter) {
return ((Enum<?>) from).name();
}
Expand Down

0 comments on commit e66620c

Please sign in to comment.