Package org.geotools.util.logging
Class LoggedFormat<T>
- Object
-
- Format
-
- LoggedFormat<T>
-
- All Implemented Interfaces:
Serializable
,Cloneable
public class LoggedFormat<T> extends Format
Wraps aFormat
object in order to either parse fully a string, or log a warning. This class provides aparse(java.lang.String)
method which performs the following tasks:- Checks if the string was fully parsed and log a warning if it was not. This is different
than the default
parseObject(String)
behavior which check only if the begining of the string was parsed and ignore any remaining characters. - Ensures that the parsed object is of some specific class specified at construction time.
- If the string can't be fully parsed or is not of the expected class, logs a warning.
- Since:
- 2.4
- Author:
- Martin Desruisseaux
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
LoggedFormat(Format format, Class<T> type)
Creates a new format wrapping the specified one.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringBuffer
format(Object value, StringBuffer toAppendTo, FieldPosition position)
Formats the specified object.AttributedCharacterIterator
formatToCharacterIterator(Object value)
Formats the specified object.static String
formatUnparsable(String text, int index, int errorIndex, Locale locale)
Formats an error message for an unparsable string.static LogRecord
formatUnparsable(String text, int index, int errorIndex, Locale locale, Level level)
Formats a log record for an unparsable string.static <T> LoggedFormat<T>
getInstance(Format format, Class<T> type)
Creates a new format wrapping the specified one.protected Locale
getWarningLocale()
Returns the locale to use for formatting warnings.protected void
logWarning(LogRecord warning)
Logs a warning.T
parse(String text)
Parses the specified string.Object
parseObject(String text)
Parses text from a string to produce an object.Object
parseObject(String text, ParsePosition position)
Parses text from a string to produce an object.void
setCaller(Class<?> caller, String method)
Sets the source class name and source method name for the warnings eventually emitted by theparse(java.lang.String)
method.void
setLevel(Level level)
Sets the logger level for the warnings eventually emitted by theparse(java.lang.String)
method.void
setLogger(String logger)
Sets the logger where to send the warnings eventually emitted by theparse(java.lang.String)
method.String
toString()
Returns a string representation for debugging purpose.
-
-
-
Constructor Detail
-
LoggedFormat
protected LoggedFormat(Format format, Class<T> type)
Creates a new format wrapping the specified one.- Parameters:
format
- The format to use for parsing and formatting.type
- The expected type of parsed values.
-
-
Method Detail
-
getInstance
public static <T> LoggedFormat<T> getInstance(Format format, Class<T> type)
Creates a new format wrapping the specified one.- Parameters:
format
- The format to use for parsing and formatting.type
- The expected type of parsed values.
-
setLogger
public void setLogger(String logger)
Sets the logger where to send the warnings eventually emitted by theparse(java.lang.String)
method.- Parameters:
logger
- The logger where to log warnings, ornull
if none.
-
setLevel
public void setLevel(Level level)
Sets the logger level for the warnings eventually emitted by theparse(java.lang.String)
method. The default value isLevel.WARNING
.- Parameters:
level
- The new logging level.- Since:
- 2.5
-
setCaller
public void setCaller(Class<?> caller, String method)
Sets the source class name and source method name for the warnings eventually emitted by theparse(java.lang.String)
method.- Parameters:
caller
- The class to declare as the warning emitter, ornull
if none.method
- The method to declare as the warning emitter, ornull
if none.
-
parse
public T parse(String text)
Parses the specified string. If the string can't be parsed, then this method returnsnull
. If it can be parsed at least partially and is of the kind specified at construction time, then it is returned. If the string has not been fully parsed, then a log record is prepared and logged.- Parameters:
text
- The text to parse, ornull
.- Returns:
- The parsed object, or
null
iftext
was null or can't be parsed.
-
parseObject
public Object parseObject(String text) throws ParseException
Parses text from a string to produce an object. This method delegates the work to the format specified at construction time. This method to not perform any logging.- Overrides:
parseObject
in classFormat
- Parameters:
text
- The text to parse.- Returns:
- An object parsed from the string.
- Throws:
ParseException
- if parsing failed.
-
parseObject
public Object parseObject(String text, ParsePosition position)
Parses text from a string to produce an object. This method delegates the work to the format specified at construction time. This method to not perform any logging.- Specified by:
parseObject
in classFormat
- Parameters:
text
- The text to parse.position
- Index and error index information.- Returns:
- An object parsed from the string, or
null
in case of error.
-
format
public StringBuffer format(Object value, StringBuffer toAppendTo, FieldPosition position)
Formats the specified object. This method delegates the work to the format specified at construction time.- Specified by:
format
in classFormat
- Parameters:
value
- The object to format.toAppendTo
- The buffer where the text is to be appended.position
- Identifies a field in the formatted text.- Returns:
- The string buffer passed in with formatted text appended.
-
formatToCharacterIterator
public AttributedCharacterIterator formatToCharacterIterator(Object value)
Formats the specified object. This method delegates the work to the format specified at construction time.- Overrides:
formatToCharacterIterator
in classFormat
- Parameters:
value
- The object to format.- Returns:
- The character iterator describing the formatted value.
-
logWarning
protected void logWarning(LogRecord warning)
Logs a warning. This method is invoked automatically by theparse
method when a text can't be fully parsed. The default implementation logs the warning to the logger specified by the last call to thesetLogger
method. Subclasses may override this method if they want to change the log record before the logging.- Parameters:
warning
- The warning to log.
-
getWarningLocale
protected Locale getWarningLocale()
Returns the locale to use for formatting warnings. The default implementation returns the default locale.
-
formatUnparsable
public static String formatUnparsable(String text, int index, int errorIndex, Locale locale)
Formats an error message for an unparsable string. This method performs the same work thatformatUnparsable(..., Level)
, except that the result is returned as aString
rather than aLogRecord
. This is provided as a convenience method for creating the message to give to an exception constructor.- Parameters:
text
- The unparsable string.index
- The parse position. This is usuallyParsePosition.getIndex()
.errorIndex
- The index where the error occured. This is usuallyParsePosition.getErrorIndex()
.locale
- The locale for the message, ornull
for the default one.- Returns:
- A formatted error message.
- Since:
- 2.5
-
formatUnparsable
public static LogRecord formatUnparsable(String text, int index, int errorIndex, Locale locale, Level level)
Formats a log record for an unparsable string. This method is invoked by theparse
method for formatting the log record to be given to thelogWarning(java.util.logging.LogRecord)
method. It is made public as a convenience for implementors who wish to manage loggings outside thisLoggedFormat
class.- Parameters:
text
- The unparsable string.index
- The parse position. This is usuallyParsePosition.getIndex()
.errorIndex
- The index where the error occured. This is usuallyParsePosition.getErrorIndex()
.locale
- The locale for the log message, ornull
for the default one.level
- The log record level.- Returns:
- A formatted log record.
- Since:
- 2.5
-
toString
public String toString()
Returns a string representation for debugging purpose.- Overrides:
toString
in classObject
-
-