|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
ObjectObjectConverters
public class ObjectConverters
Performs conversions of objects encountered during XML (un)marshalling. Each method in this class is a converter and can be invoked at (un)marshalling time. The default implementation is straightforward and documented in the javadoc of each method.
This class provides a way to handle the errors which may exist in some XML documents. For
example a URL in the document may be malformed, causing a MalformedURLException to
be thrown. If this error is not handled, it will cause the (un)marshalling of the entire
document to fail. An application may want to change this behavior by replacing URLs that
are known to be erroneous by fixed versions of those URLs. Example:
class URLFixer extends ObjectConverters {
public URL toURL(URI uri) throws MalformedURLException {
try {
return super.toURL(uri);
} catch (MalformedURLException e) {
if (uri.equals(KNOWN_ERRONEOUS_URI) {
return FIXED_URL;
} else {
throw e;
}
}
}
}
See the XML.CONVERTERS javadoc for an example of registering a custom
ObjectConverters to a (un)marshaller.
| utility/geotk-xml-base (download) | View source code for this class |
| Field Summary | |
|---|---|
static ObjectConverters |
DEFAULT
The default, thread-safe and immutable instance. |
| Constructor Summary | |
|---|---|
protected |
ObjectConverters()
Creates a default ObjectConverters. |
| Method Summary | ||
|---|---|---|
protected
|
exceptionOccured(T value,
Class<T> sourceType,
Class<?> targetType,
Exception exception)
Invoked when an exception occurred in any toXXX(...) method. |
|
Locale |
toLocale(String value)
Converts the given string to a locale. |
|
NilReason |
toNilReason(String value)
Converts the given string to a NilReason. |
|
Unit<?> |
toUnit(String value)
Converts the given string to a unit. |
|
URI |
toURI(String value)
Converts the given string to a URI. |
|
URI |
toURI(URL value)
Converts the given URL to a URI. |
|
URL |
toURL(URI value)
Converts the given URI to a URL. |
|
UUID |
toUUID(String value)
Converts the given string to a Universal Unique Identifier. |
|
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final ObjectConverters DEFAULT
ObjectConverters
was explicitly set.
| Constructor Detail |
|---|
protected ObjectConverters()
ObjectConverters. This is for subclasses only,
since new instances are useful only if at least one method is overridden.
| Method Detail |
|---|
protected <T> boolean exceptionOccured(T value,
Class<T> sourceType,
Class<?> targetType,
Exception exception)
toXXX(...) method. The default implementation
does nothing and return false, which will cause the (un)marshalling process of the
whole XML document to fail.
This method provides a single hook that subclasses can override in order to provide their
own error handling for every methods defined in this class, like the example documented in
the XML.CONVERTERS javadoc. Subclasses also have the possibility to override individual
toXXX(...) methods, like the example provided in this class
javadoc.
T - The compile-time type of the sourceType argument.value - The value that can't be converted.sourceType - The base type of the value to convert. This is determined by the argument
type of the method that caught the exception. For example the source type is always
URI.class if the exception has been caught by the toURL(URI) method.targetType - The expected type of the converted object.exception - The exception that occurred during the attempt to convert.
true if the (un)marshalling process should continue despite this error,
or false (the default) if the exception should be propagated, thus causing
the (un)marshalling to fail.
public Locale toLocale(String value)
throws IllegalArgumentException
'_'
character and the country code (again either as 2 or 3 letters), optionally followed
by '_' and the variant.
value - The string to convert to a locale, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
IllegalArgumentException - If the given string can not be converted to a locale.
public Unit<?> toUnit(String value)
throws IllegalArgumentException
exceptionOccured
in case of error:
return Units.valueOf(value);
value - The string to convert to a unit, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
IllegalArgumentException - If the given string can not be converted to a unit.Units.valueOf(String)
public UUID toUUID(String value)
throws IllegalArgumentException
exceptionOccured in case of error:
return UUID.fromString(value);
value - The string to convert to a UUID, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
IllegalArgumentException - If the given string can not be converted to a UUID.UUID.fromString(String)
public URI toURI(String value)
throws URISyntaxException
URI(String) constructor would not accept (for example
replacing space by %20), then performs the following work (omitting the check for
null value and the call to exceptionOccured in case of error):
return new URI(escapedValue);
value - The string to convert to a URI, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
URISyntaxException - If the given string can not be converted to a URI.URI.URI(String)
public URI toURI(URL value)
throws URISyntaxException
exceptionOccured
in case of error:
return value.toURI();
value - The URL to convert to a URI, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
URISyntaxException - If the given URL can not be converted to a URI.URL.toURI()
public URL toURL(URI value)
throws MalformedURLException
exceptionOccured
in case of error:
return value.toURL();
value - The URI to convert to a URL, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
MalformedURLException - If the given URI can not be converted to a URL.URI.toURL()
public NilReason toNilReason(String value)
throws URISyntaxException
NilReason. The default implementation is as below,
omitting the check for null value and the call to exceptionOccured
in case of error:
return NilReason.valueOf(value);
value - The string to convert to a nil reason, or null.
null if the given value was null of if an
exception was thrown and exceptionOccured returned true.
URISyntaxException - If the given string can not be converted to a nil reason.NilReason.valueOf(String)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||