Fixed lousy implementation of color code parsing.

fix-message-formatting
Danny van Heumen 11 years ago
parent cd0c18d40b
commit 2f7c4996eb

@ -84,27 +84,17 @@ public static String parse(final String text)
} }
break; break;
case '\u0003': case '\u0003':
Color foreground = null;
Color background = null; Color background = null;
try // first parse foreground color code
Color foreground = parseForegroundColor(text.substring(i + 1));
if (foreground != null)
{ {
// parse foreground color code
foreground = parseForegroundColor(text.substring(i + 1));
i += 2; i += 2;
// parse background color code
background = parseBackgroundColor(text.substring(i + 1)); background = parseBackgroundColor(text.substring(i + 1));
i += INDEX_END_COLOR_CODE; if (background != null)
} {
catch (IllegalArgumentException e) i += INDEX_END_COLOR_CODE;
{ }
LOGGER.debug(
"Invalid color code: " + text.substring(i + 1), e);
}
catch (ArrayIndexOutOfBoundsException e)
{
LOGGER.debug("Unknown color code referenced: "
+ text.substring(i + 1));
} }
if (foreground == null && background == null) if (foreground == null && background == null)
{ {
@ -146,30 +136,21 @@ private static Color parseBackgroundColor(final String text)
color = color % Color.values().length; color = color % Color.values().length;
return Color.values()[color]; return Color.values()[color];
} }
throw new IllegalArgumentException( return null;
"no color separator present, hence no background color present");
} }
catch (StringIndexOutOfBoundsException e) catch (StringIndexOutOfBoundsException e)
{ {
// Abort parsing background color. Assume only // Abort parsing background color. Assume only
// foreground color available. // foreground color available.
throw new IllegalArgumentException( LOGGER.trace("Abort parsing background color because text ended. "
"text stopped before the background color code was finished"); + "Assuming only foreground color was available.");
return null;
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
// No background color defined, ignoring ... // No background color defined, ignoring ...
throw new IllegalArgumentException( LOGGER.trace("No background color defined. Ignoring ...");
"value isn't a background color code: " return null;
+ text.substring(1, INDEX_END_COLOR_CODE));
}
catch (ArrayIndexOutOfBoundsException e)
{
LOGGER.debug("Specified IRC color is not a known color code: "
+ e.getMessage());
throw new IllegalArgumentException("background color value "
+ text.substring(1, INDEX_END_COLOR_CODE)
+ " is not a known color");
} }
} }
@ -192,22 +173,12 @@ private static Color parseForegroundColor(final String text)
// Invalid control code, since text has ended. // Invalid control code, since text has ended.
LOGGER.trace("ArrayIndexOutOfBounds during foreground " LOGGER.trace("ArrayIndexOutOfBounds during foreground "
+ "color control code parsing."); + "color control code parsing.");
throw new IllegalArgumentException("missing foreground color code"); return null;
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
// FIXME correctly print out color code (as a number or hex number) LOGGER.trace("Invalid foreground color code encountered.", e);
// Invalid text color value return null;
LOGGER.trace("Invalid foreground color code encountered.");
throw new IllegalArgumentException(
"invalid foreground color code: " + text.substring(0, 2));
}
catch (ArrayIndexOutOfBoundsException e)
{
LOGGER.debug("Specified IRC color is not a known color code: "
+ e.getMessage());
throw new IllegalArgumentException("foreground color value "
+ text.substring(0, 2) + " is not a known color");
} }
} }

Loading…
Cancel
Save