Some fixups, escape colors prior to unescaping.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1767 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2006-01-20 22:46:36 +00:00
parent daf016d62f
commit 1cded0f96c
@@ -63,13 +63,16 @@ public class Label implements SwingConstants, LabelStyleConstants
public static final Pattern COLOR_PATTERN = public static final Pattern COLOR_PATTERN =
Pattern.compile("#([Xx]|[0-9A-Fa-f]{6}+)"); Pattern.compile("#([Xx]|[0-9A-Fa-f]{6}+)");
private static final Pattern ESCAPED_PATTERN =
Pattern.compile("#''([Xx]|[0-9A-Fa-f]{6}+)");
/** /**
* Filter out any color tags from the specified text. * Filter out any color tags from the specified text.
*/ */
public static String filterColors (String txt) public static String filterColors (String txt)
{ {
return (txt == null) ? null if (txt == null) return null;
: COLOR_PATTERN.matcher(txt).replaceAll(""); return COLOR_PATTERN.matcher(txt).replaceAll("");
} }
/** /**
@@ -78,13 +81,18 @@ public class Label implements SwingConstants, LabelStyleConstants
*/ */
public static String escapeColors (String txt) public static String escapeColors (String txt)
{ {
StringBuffer buf = new StringBuffer(); if (txt == null) return null;
Matcher m = COLOR_PATTERN.matcher(txt); return COLOR_PATTERN.matcher(txt).replaceAll("#''$1");
while (m.find()) { }
m.appendReplacement(buf, "#''" + m.group(1));
} /**
m.appendTail(buf); * Un-escape special tags so that they again look correct. Called by
return buf.toString(); * rendering components that do not understand the color tags
* to filter colors and unescape any escaped colors.
*/
public static String unescapeColors (String txt)
{
return unescapeColors(filterColors(txt), true);
} }
/** /**
@@ -92,14 +100,9 @@ public class Label implements SwingConstants, LabelStyleConstants
*/ */
private static String unescapeColors (String txt, boolean restore) private static String unescapeColors (String txt, boolean restore)
{ {
if (txt == null) return null;
String prefix = restore ? "#" : "%"; String prefix = restore ? "#" : "%";
StringBuffer buf = new StringBuffer(); return ESCAPED_PATTERN.matcher(txt).replaceAll(prefix + "$1");
Matcher m = Pattern.compile("#''([Xx]|[0-9A-Fa-f]{6}+)").matcher(txt);
while (m.find()) {
m.appendReplacement(buf, prefix + m.group(1));
}
m.appendTail(buf);
return buf.toString();
} }
/** /**