I did something similar on a small scale that was suitable for what I need to do at the time.
first I used enums for all the common style attributes I was interested in
example
private enum TextAlignment {
LEFT,
RIGHT,
CENTER,
JUSTIFY,
NONE;
}
private enum TextDecoration {
BLINK,
UNDERLYIN,
OVERLINE,
LINE_THROUGH,
NONE;
}
I should have put this into a class hierarchy but for my purpose it wasn't necessary
before formatting the tag I simply sent my formatter function a list of styles to apply to the tag.
A series of case statements then adds the style into the style element and returns the tag in html format back as a string.
private String tag(String tag ,String data, TextAlignment aline, boolean bold, Color bgColor, Color fgColor, boolean blink) {
StringBuilder sb = new StringBuilder(data.length() + 128);
sb.append("<");
sb.append(tag);
sb.append(" style=\"");
switch (aline) {
case NONE:
break;
default:
sb.append("text-align:");
sb.append(aline.toString().toLowerCase());
sb.append(";");
break;
}
if (fgColor != null) {
sb.append("color:");
String rgb = Integer.toHexString(fgColor.getRGB());
sb.append(rgb.substring(2, rgb.length()));
sb.append(";");
}
if (bgColor != null) {
sb.append("background-color:");
String rgb = Integer.toHexString(bgColor.getRGB());
sb.append(rgb.substring(2, rgb.length()));
sb.append(";");
}
..........
you get the idea