felix@1090: package de.intevation.flys.utils; felix@1090: felix@1090: import java.awt.Color; felix@1090: felix@1090: import org.w3c.dom.Document; felix@1090: felix@1090: import de.intevation.artifacts.common.utils.XMLUtils; felix@1090: felix@1090: felix@1090: /** felix@1090: * Utility to deal with themes and their representations. felix@1090: */ felix@1090: public class ThemeUtil { felix@1711: public final static String XPATH_LINE_COLOR = felix@1711: "/theme/field[@name='linecolor']/@default"; felix@1711: felix@1711: public static final String XPATH_LINE_SIZE = felix@1711: "/theme/field[@name='linesize']/@default"; felix@1711: felix@1711: felix@1711: /** felix@1711: * Parses line width, defaulting to 0. felix@1711: * @param theme the theme felix@1711: */ felix@1711: public static int parseLineWidth(Document theme) { felix@1711: String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null); felix@1711: if (size == null || size.length() == 0) { felix@1711: return 0; felix@1711: } felix@1711: felix@1711: try { felix@1711: return Integer.valueOf(size); felix@1711: } felix@1711: catch (NumberFormatException nfe) { felix@1711: //logger.warn("Unable to set line size from string: '" + size + "'"); felix@1711: } felix@1711: return 0; felix@1711: } felix@1711: felix@1090: felix@1090: /** felix@1090: * Parse a string like "103, 100, 0" and return a corresping color. felix@1090: * @param rgbtext Color as string representation, e.g. "255,0,20". felix@1090: * @return Color, null in case of issues. felix@1090: */ felix@1090: public static Color parseRGB(String rgbtext) { felix@1090: if (rgbtext == null) { felix@1090: return null; felix@1090: } felix@1090: String rgb[] = rgbtext.split(","); felix@1090: Color c = null; felix@1090: try { felix@1090: c = new Color( felix@1090: Integer.valueOf(rgb[0].trim()), felix@1090: Integer.valueOf(rgb[1].trim()), felix@1090: Integer.valueOf(rgb[2].trim())); felix@1090: } felix@1090: catch (NumberFormatException nfe) { felix@1090: c = null; felix@1090: } felix@1090: return c; felix@1090: } felix@1090: felix@1090: /** felix@1090: * Gets color from color field. felix@1090: * @param theme the theme document. felix@1090: * @return color. felix@1090: */ felix@1090: public static Color parseLineColorField(Document theme) { felix@1090: String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null); felix@1090: return parseRGB(color); felix@1090: } felix@1090: }