felix@1090: package de.intevation.flys.utils;
felix@1090: 
christian@3254: import de.intevation.artifacts.common.utils.XMLUtils;
christian@3254: import de.intevation.flys.artifacts.model.MapserverStyle;
christian@3254: import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
christian@3254: import de.intevation.flys.artifacts.model.MapserverStyle.Expression;
christian@3254: import de.intevation.flys.artifacts.model.MapserverStyle.Label;
christian@3254: import de.intevation.flys.artifacts.model.MapserverStyle.Style;
raimund@1738: 
felix@1090: import java.awt.Color;
raimund@1738: import java.awt.Font;
felix@1090: 
ingo@2616: import javax.xml.xpath.XPathConstants;
ingo@2616: 
christian@3254: import org.apache.log4j.Logger;
felix@1090: import org.w3c.dom.Document;
ingo@2616: import org.w3c.dom.Element;
ingo@2616: import org.w3c.dom.NodeList;
felix@1090: 
felix@1090: 
felix@1090: /**
felix@1090:  * Utility to deal with themes and their representations.
felix@1090:  */
felix@1090: public class ThemeUtil {
raimund@1738: 
raimund@1738:     private static Logger logger =
raimund@1738:         Logger.getLogger(ThemeUtil.class);
raimund@1738: 
felix@2016:     public final static String XPATH_FILL_COLOR =
felix@2016:         "/theme/field[@name='fillcolor']/@default";
felix@2016: 
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: 
raimund@1753:     public static final String XPATH_LINE_STYLE =
raimund@1753:         "/theme/field[@name='linetype']/@default";
raimund@1753: 
ingo@2076:     public static final String XPATH_POINT_SIZE =
ingo@2076:         "/theme/field[@name='pointsize']/@default";
ingo@2076: 
ingo@3090:     public static final String XPATH_POINT_COLOR =
ingo@3090:         "/theme/field[@name='pointcolor']/@default";
ingo@3090: 
felix@2016:     public final static String XPATH_SHOW_BORDER =
felix@2016:         "/theme/field[@name='showborder']/@default";
felix@2016: 
raimund@1748:     public final static String XPATH_SHOW_POINTS =
raimund@1748:         "/theme/field[@name='showpoints']/@default";
raimund@1748: 
raimund@1753:     public final static String XPATH_SHOW_LINE =
raimund@1753:         "/theme/field[@name='showlines']/@default";
raimund@1753: 
felix@2996:     public final static String XPATH_SHOW_VERTICAL_LINE =
felix@2996:         "/theme/field[@name='showverticalline']/@default";
felix@2996: 
felix@2996:     public final static String XPATH_SHOW_HORIZONTAL_LINE =
felix@2996:         "/theme/field[@name='showhorizontalline']/@default";
felix@2996: 
felix@2648:     public final static String XPATH_SHOW_LINE_LABEL =
felix@2648:         "/theme/field[@name='showlinelabel']/@default";
felix@2648: 
felix@2656:     public final static String XPATH_LINE_LABEL_FONT =
felix@2659:         "/theme/field[@name='linelabelfont']/@default";
felix@2656: 
felix@2656:     public final static String XPATH_LINE_LABEL_COLOR =
felix@2659:         "/theme/field[@name='linelabelcolor']/@default";
felix@2656: 
felix@2656:     public final static String XPATH_LINE_LABEL_SIZE =
felix@2659:         "/theme/field[@name='linelabelsize']/@default";
felix@2656: 
felix@2656:     public final static String XPATH_LINE_LABEL_STYLE =
felix@2659:         "/theme/field[@name='linelabelstyle']/@default";
felix@2656: 
felix@2656:     public final static String XPATH_LINE_LABEL_BGCOLOR =
felix@2659:         "/theme/field[@name='linelabelbgcolor']/@default";
felix@2656: 
felix@2656:     public final static String XPATH_LINE_LABEL_SHOWBG =
felix@2659:         "/theme/field[@name='linelabelshowbg']/@default";
felix@2656: 
felix@2663:     public final static String XPATH_SHOW_WIDTH =
felix@2663:         "/theme/field[@name='showwidth']/@default";
felix@2663: 
felix@2663:     public final static String XPATH_SHOW_LEVEL =
felix@2663:         "/theme/field[@name='showwidth']/@default";
felix@2663: 
felix@2032:     public final static String XPATH_TRANSPARENCY =
felix@2032:         "/theme/field[@name='transparent']/@default";
sascha@3256: 
christian@3254:     public final static String XPATH_TRANSPARENCY_ALPHA =
christian@3254:         "/theme/field[@name='alpha']/@default";
felix@2032: 
felix@2666:     public final static String XPATH_SHOW_AREA =
felix@2666:         "/theme/field[@name='showarea']/@default";
felix@2666: 
felix@2674:     public final static String XPATH_SHOW_MIDDLE_HEIGHT =
felix@2674:         "/theme/field[@name='showmiddleheight']/@default";
felix@2674: 
raimund@1738:     public final static String XPATH_TEXT_COLOR =
raimund@1738:         "/theme/field[@name='textcolor']/@default";
raimund@1738: 
raimund@1738:     public final static String XPATH_TEXT_SIZE =
raimund@1738:         "/theme/field[@name='textsize']/@default";
raimund@1738: 
raimund@1738:     public final static String XPATH_TEXT_FONT =
raimund@1738:         "/theme/field[@name='font']/@default";
felix@1711: 
raimund@1748:     public final static String XPATH_TEXT_STYLE =
raimund@1748:         "/theme/field[@name='textstyle']/@default";
raimund@1748: 
raimund@1748:     public final static String XPATH_TEXT_ORIENTATION =
raimund@1748:         "/theme/field[@name='textorientation']/@default";
raimund@1748: 
raimund@1748:     public final static String XPATH_TEXT_BACKGROUND =
raimund@1750:         "/theme/field[@name='backgroundcolor']/@default";
raimund@1748: 
raimund@1748:     public final static String XPATH_SHOW_BACKGROUND =
raimund@1748:         "/theme/field[@name='showbackground']/@default";
raimund@1748: 
ingo@1816:     public final static String XPATH_SYMBOL =
ingo@1816:         "/theme/field[@name='symbol']/@default";
ingo@1816: 
ingo@2072:     public final static String XPATH_SHOW_MINIMUM =
ingo@2072:         "/theme/field[@name='showminimum']/@default";
ingo@2072: 
ingo@2072:     public final static String XPATH_SHOW_MAXIMUM =
ingo@2072:         "/theme/field[@name='showmaximum']/@default";
ingo@2072: 
ingo@2616:     public final static String XPATH_WSPLGEN_FIELDS =
ingo@2616:         "/theme[@name='WSPLGEN']/field";
ingo@2616: 
felix@2016: 
felix@2016:     /** Parse string to be boolean with default if empty or unrecognized. */
felix@2016:     public static boolean parseBoolean(String value, boolean defaultsTo) {
felix@2016:         if (value == null || value.length() == 0) {
felix@2016:             return defaultsTo;
felix@2016:         }
felix@2016:         if (value.equals("false")) {
felix@2016:             return false;
felix@2016:         }
felix@2016:         else if (value.equals("true")) {
felix@2016:             return true;
felix@2016:         }
felix@2016:         else {
felix@2016:             return defaultsTo;
felix@2016:         }
felix@2016:     }
felix@2016: 
felix@2016: 
ingo@2076:     public static int parseInteger(String value, int defaultsTo) {
ingo@2076:         if (value == null || value.length() == 0) {
ingo@2076:             return defaultsTo;
ingo@2076:         }
ingo@2076: 
ingo@2076:         try {
ingo@2076:             return Integer.valueOf(value);
ingo@2076:         }
ingo@2076:         catch (NumberFormatException nfe) {
ingo@2076:             // do nothing
ingo@2076:         }
ingo@2076: 
ingo@2076:         return defaultsTo;
ingo@2076:     }
ingo@2076: 
ingo@2076: 
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) {
raimund@1753:             logger.warn("Unable to set line size from string: '" + size + "'");
felix@1711:         }
felix@1711:         return 0;
felix@1711:     }
felix@1711: 
felix@1090: 
ingo@2076:     public static int parsePointWidth(Document theme) {
ingo@2076:         String width = XMLUtils.xpathString(theme, XPATH_POINT_SIZE, null);
ingo@2076: 
ingo@2076:         return parseInteger(width, 3);
ingo@2076:     }
ingo@2076: 
ingo@2076: 
ingo@3090:     public static Color parsePointColor(Document theme) {
ingo@3090:         String color = XMLUtils.xpathString(theme, XPATH_POINT_COLOR, null);
christian@3155:         logger.debug("parsePointColor(): color = " + color);
ingo@3090:         return parseColor(color);
ingo@3090:     }
ingo@3090: 
ingo@3090: 
felix@1090:     /**
raimund@1753:      * Parses the line style, defaulting to '10'.
raimund@1753:      * @param theme The theme.
raimund@1753:      */
raimund@1753:     public static float[] parseLineStyle(Document theme) {
raimund@1753:         String dash = XMLUtils.xpathString(theme, XPATH_LINE_STYLE, null);
raimund@1753: 
raimund@1753:         float[] def = {10};
raimund@1753:         if (dash == null || dash.length() == 0) {
raimund@1753:             return def;
raimund@1753:         }
raimund@1753: 
raimund@1753:         String[] pattern = dash.split(",");
raimund@1753:         if(pattern.length == 1) {
raimund@1753:             return def;
raimund@1753:         }
raimund@1753: 
raimund@1753:         try {
raimund@1753:             float[] dashes = new float[pattern.length];
raimund@1753:             for (int i = 0; i < pattern.length; i++) {
raimund@1753:                 dashes[i] = Float.parseFloat(pattern[i]);
raimund@1753:             }
raimund@1753:             return dashes;
raimund@1753:         }
raimund@1753:         catch(NumberFormatException nfe) {
raimund@1753:             logger.warn("Unable to set dash from string: '" + dash + "'");
raimund@1753:             return def;
raimund@1753:         }
raimund@1753:     }
raimund@1753: 
raimund@1753: 
raimund@1753:     /**
raimund@1738:      * Parses text size, defaulting to 10.
raimund@1738:      * @param theme The theme.
raimund@1738:      */
felix@2656:     public static int parseTextSize(Document theme, String path) {
felix@2656:         String size = XMLUtils.xpathString(theme, path, null);
raimund@1738:         if (size == null || size.length() == 0) {
raimund@1738:             return 10;
raimund@1738:         }
raimund@1738: 
raimund@1738:         try {
raimund@1738:             return Integer.valueOf(size);
raimund@1738:         }
raimund@1738:         catch (NumberFormatException nfe) {
raimund@1738:         }
raimund@1738:         return 10;
raimund@1738:     }
raimund@1738: 
raimund@1738: 
felix@2656:     public static int parseTextSize(Document theme) {
felix@2656:         return parseTextSize(theme, XPATH_TEXT_SIZE);
felix@2656:     }
felix@2656: 
felix@2656: 
felix@2656:     public static int parseLineLabelSize(Document theme) {
felix@2656:         return parseTextSize(theme, XPATH_LINE_LABEL_SIZE);
felix@2656:     }
felix@2656: 
felix@2656: 
raimund@1738:     /**
raimund@1748:      * Parses the attribute 'showpoints', defaults to false.
raimund@1748:      * @param theme The theme.
raimund@1748:      */
raimund@1748:     public static boolean parseShowPoints(Document theme) {
raimund@1748:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINTS, null);
felix@2016:         return parseBoolean(show, false);
raimund@1748:     }
raimund@1748: 
felix@2666:     /**
felix@2674:      * Parses the attribute 'showmiddleheight', defaults to false.
felix@2674:      * @param theme The theme.
felix@2674:      */
felix@2674:     public static boolean parseShowMiddleHeight(Document theme) {
felix@2674:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_MIDDLE_HEIGHT, null);
felix@2674:         return parseBoolean(show, false);
felix@2674:     }
felix@2674: 
felix@2674:     /**
felix@2666:      * Parses the attribute 'showarea', defaults to false.
felix@2666:      * @param theme The theme.
felix@2666:      */
felix@2666:     public static boolean parseShowArea(Document theme) {
felix@2666:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_AREA, null);
felix@2666:         return parseBoolean(show, false);
felix@2666:     }
raimund@1748: 
raimund@1748:     /**
felix@2996:      * Parses the attribute 'showverticalline', defaults to true.
felix@2996:      * @param theme The theme.
felix@2996:      */
felix@2996:     public static boolean parseShowVerticalLine(Document theme) {
felix@2996:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_VERTICAL_LINE, null);
felix@2996:         return parseBoolean(show, true);
felix@2996:     }
felix@2996: 
felix@2996:     /**
felix@2996:      * Parses the attribute 'showhorizontalline', defaults to true.
felix@2996:      * @param theme The theme.
felix@2996:      */
felix@2996:     public static boolean parseShowHorizontalLine(Document theme) {
felix@2996:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_HORIZONTAL_LINE, null);
felix@2996:         return parseBoolean(show, true);
felix@2996:     }
felix@2996: 
felix@2996:     /**
raimund@1753:      * Parses the attribute 'showlines', defaults to true.
raimund@1753:      * @param theme The theme.
raimund@1753:      */
raimund@1753:     public static boolean parseShowLine(Document theme) {
raimund@1753:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE, null);
felix@2016:         return parseBoolean(show, true);
raimund@1753:     }
raimund@1753: 
felix@2648:     /**
felix@2648:      * Parses the attribute 'showlinelabel', defaults to true.
felix@2648:      * @param theme The theme.
felix@2648:      */
felix@2648:     public static boolean parseShowLineLabel(Document theme) {
felix@2648:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE_LABEL, null);
ingo@3098:         return parseBoolean(show, false);
felix@2648:     }
raimund@1753: 
raimund@1753:     /**
raimund@1738:      * Parses text color.
raimund@1738:      * @param theme The theme.
raimund@1738:      */
raimund@1738:     public static Color parseTextColor(Document theme) {
ingo@1876:         return parseRGB(getTextColorString(theme));
raimund@1738:     }
raimund@1738: 
sascha@3076: 
felix@2660:     /** Parse color for line label(s text). */
felix@2660:     public static Color parseLineLabelTextColor(Document theme) {
felix@2660:         return parseRGB(getLineLabelTextColorString(theme));
felix@2660:     }
felix@2660: 
felix@2660:     /** Parse bg color for line label(s text). */
felix@2660:     public static Color parseLineLabelBGColor(Document theme) {
felix@2660:         return parseRGB(getLineLabelBGColorString(theme));
felix@2660:     }
felix@2660: 
felix@2660:     // TODO showbg and bg color for linelabels
felix@2660: 
raimund@1738: 
raimund@1738:     /**
raimund@1738:      * Parses the font.
raimund@1738:      * @param theme The theme.
raimund@1738:      */
raimund@1738:     public static Font parseTextFont(Document theme) {
raimund@1738:         String font = XMLUtils.xpathString(theme, XPATH_TEXT_FONT, null);
raimund@1738:         if (font == null || font.length() == 0) {
raimund@1738:             return null;
raimund@1738:         }
raimund@1738: 
raimund@1738:         int size = parseTextSize(theme);
raimund@1750:         int style = parseTextStyle(theme);
raimund@1750:         Font f = new Font (font, style, size);
raimund@1738:         return f;
raimund@1738:     }
raimund@1738: 
felix@2656:     /** Parse font (name, size and style) for linelabels. */
felix@2656:     public static Font parseLineLabelFont(Document theme) {
felix@2656:         String font = XMLUtils.xpathString(theme, XPATH_LINE_LABEL_FONT, null);
felix@2656:         if (font == null || font.length() == 0) {
felix@2656:             return null;
felix@2656:         }
felix@2656: 
felix@2656:         int size = parseLineLabelSize(theme);
felix@2656:         int style = parseLineLabelStyle(theme);
felix@2656: 
felix@2656:         Font f = new Font (font, style, size);
felix@2656:         return f;
felix@2656:     }
felix@2656: 
raimund@1738: 
raimund@1738:     /**
raimund@1748:      * Parses the text style, defaults to 'Font.PLAIN'.
raimund@1748:      * @param theme The theme.
raimund@1748:      */
felix@2656:     public static int parseTextStyle(Document theme, String path) {
felix@2656:         String style = XMLUtils.xpathString(theme, path, null);
raimund@1748:         if (style == null || style.length() == 0) {
raimund@1748:             return Font.PLAIN;
raimund@1748:         }
raimund@1748: 
raimund@1748:         if (style.equals("italic")) {
raimund@1748:             return Font.ITALIC;
raimund@1748:         }
raimund@1748:         else if (style.equals("bold")) {
raimund@1748:             return Font.BOLD;
raimund@1748:         }
raimund@1748:         else {
raimund@1748:             return Font.PLAIN;
raimund@1748:         }
raimund@1748:     }
raimund@1748: 
raimund@1748: 
felix@2656:     public static int parseTextStyle(Document theme) {
felix@2656:         return parseTextStyle(theme, XPATH_TEXT_STYLE);
felix@2656:     }
felix@2656: 
felix@2656: 
felix@2656:     public static int parseLineLabelStyle(Document theme) {
felix@2656:         return parseTextStyle(theme, XPATH_LINE_LABEL_STYLE);
felix@2656:     }
felix@2656: 
felix@2656: 
felix@2663:     public static boolean parseShowWidth(Document theme) {
felix@2663:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_WIDTH, null);
felix@2663:         return parseBoolean(show, false);
felix@2663:     }
felix@2663: 
felix@2663:     public static boolean parseShowLevel(Document theme) {
felix@2663:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_LEVEL, null);
felix@2663:         return parseBoolean(show, false);
felix@2663:     }
felix@2663: 
raimund@1748:     /**
raimund@1748:      * Parses the textorientation, defaults to 'vertical'.
raimund@1748:      * @param theme The theme.
raimund@1748:      */
raimund@1748:     public static String parseTextOrientation(Document theme) {
raimund@1748:         String o = XMLUtils.xpathString(theme, XPATH_TEXT_ORIENTATION, null);
christian@3205:         if ("true".equals(o)) {
raimund@1750:             return "horizontal";
raimund@1750:         }
raimund@1750:         else {
raimund@1750:             return "vertical";
raimund@1750:         }
raimund@1748:     }
raimund@1748: 
raimund@1748: 
raimund@1748:     /**
raimund@1748:      * Parses the text background color, defaults to white.
raimund@1748:      * @param theme The theme.
raimund@1748:      */
raimund@1748:     public static Color parseTextBackground(Document theme) {
ingo@1878:         String color = getBackgroundColorString(theme);
raimund@1748:         if (color == null || color.length() == 0) {
raimund@1748:             return Color.WHITE;
raimund@1748:         }
raimund@1748:         return parseRGB(color);
raimund@1748:     }
raimund@1748: 
raimund@1748: 
raimund@1748:     /**
raimund@1748:      * Parses the attribute whether to show background or not, defaults to
raimund@1748:      * false.
raimund@1748:      * @param theme The theme.
raimund@1748:      */
raimund@1748:     public static boolean parseShowTextBackground(Document theme) {
raimund@1748:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_BACKGROUND, null);
felix@2016:         return parseBoolean(show, false);
raimund@1748:     }
raimund@1748: 
raimund@1748: 
raimund@1748:     /**
felix@2662:      * Parses the attribute whether to show line labels background or not,
felix@2662:      * defaults to false.
felix@2662:      * @param theme The theme.
felix@2662:      */
felix@2662:     public static boolean parseShowLineLabelBG(Document theme) {
felix@2662:         String show = XMLUtils.xpathString(theme, XPATH_LINE_LABEL_SHOWBG, null);
felix@2662:         return parseBoolean(show, false);
felix@2662:     }
felix@2662: 
ingo@3090: 
ingo@3090:     public static Color parseColor(String colorString) {
ingo@3090:         if (colorString == null || colorString.length() == 0) {
ingo@3090:             return null;
ingo@3090:         }
ingo@3090:         else if (colorString.indexOf("#") == 0) {
ingo@3090:             return parseHexColor(colorString);
ingo@3090:         }
ingo@3090:         else if (colorString.indexOf(",") >= 0) {
ingo@3090:             return parseRGB(colorString);
ingo@3090:         }
ingo@3090: 
ingo@3090:         return null;
ingo@3090:     }
ingo@3090: 
ingo@3090: 
ingo@3090:     /**
ingo@3090:      * Parse a string like "#00CC22" and return the corresponding color.
ingo@3090:      *
ingo@3090:      * @param hex The hex color value.
ingo@3090:      *
ingo@3090:      * @return a Color or null, if <i>hex</i> is empty.
ingo@3090:      */
ingo@3090:     public static Color parseHexColor(String hex) {
ingo@3090:         if (hex == null) {
ingo@3090:             return null;
ingo@3090:         }
ingo@3090: 
ingo@3090:         return Color.decode(hex);
ingo@3090:     }
ingo@3090: 
felix@2662:     /**
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: 
raimund@1748: 
ingo@1793:     public static String getLineColorString(Document theme) {
ingo@1793:         return XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
ingo@1793:     }
ingo@1793: 
felix@2016:     /** Get show border as string. */
felix@2016:     public static String getShowBorderString(Document theme) {
felix@2016:         return XMLUtils.xpathString(theme, XPATH_SHOW_BORDER, null);
felix@2016:     }
felix@2016: 
felix@2016:     /** Get fill color as string. */
felix@2016:     public static String getFillColorString(Document theme) {
felix@2016:         return XMLUtils.xpathString(theme, XPATH_FILL_COLOR, null);
felix@2016:     }
ingo@1793: 
ingo@1878:     public static String getBackgroundColorString(Document theme) {
ingo@1878:         return XMLUtils.xpathString(theme, XPATH_TEXT_BACKGROUND, null);
ingo@1878:     }
ingo@1878: 
ingo@1878: 
ingo@1876:     public static String getTextColorString(Document theme) {
ingo@1876:         return XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null);
ingo@1876:     }
ingo@1876: 
felix@2660:     public static String getLineLabelTextColorString(Document theme) {
felix@2660:         return XMLUtils.xpathString(theme, XPATH_LINE_LABEL_COLOR, null);
felix@2660:     }
felix@2660: 
felix@2660:     public static String getLineLabelBGColorString(Document theme) {
felix@2660:         return XMLUtils.xpathString(theme, XPATH_LINE_LABEL_BGCOLOR, null);
felix@2660:     }
ingo@1876: 
ingo@1816:     public static String getSymbol(Document theme) {
ingo@1816:         return XMLUtils.xpathString(theme, XPATH_SYMBOL, null);
ingo@1816:     }
ingo@1816: 
felix@2032:     public static String getTransparencyString(Document theme) {
felix@2032:         return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY, null);
felix@2032:     }
sascha@3256: 
sascha@3256: 
christian@3254:     public static String getTransparencyAlpha(Document theme) {
christian@3254:         return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY_ALPHA, null);
christian@3254:     }
felix@2032: 
ingo@1816: 
ingo@2072:     public static String getShowMinimum(Document theme) {
ingo@2072:         return XMLUtils.xpathString(theme, XPATH_SHOW_MINIMUM, null);
ingo@2072:     }
sascha@3256: 
ingo@2072: 
ingo@2072:     public static String getShowMaximum(Document theme) {
ingo@2072:         return XMLUtils.xpathString(theme, XPATH_SHOW_MAXIMUM, null);
ingo@2072:     }
ingo@2072: 
ingo@2072: 
felix@1090:     /**
felix@1090:      * Gets color from color field.
felix@1090:      * @param theme    the theme document.
felix@1090:      * @return color.
felix@1090:      */
felix@2016:     public static Color parseFillColorField(Document theme) {
felix@2016:         return parseRGB(getFillColorString(theme));
felix@2016:     }
felix@2016: 
felix@2016:     public static boolean parseShowBorder(Document theme) {
felix@2016:         return parseBoolean(getShowBorderString(theme), false);
felix@2016:     }
felix@2016: 
felix@2032:     public static boolean parseTransparency(Document theme) {
felix@2032:         return parseBoolean(getTransparencyString(theme), false);
felix@2032:     }
felix@2016: 
felix@2016:     /**
felix@2016:      * Gets color from color field.
felix@2016:      * @param theme    the theme document.
felix@2016:      * @return color.
felix@2016:      */
felix@1090:     public static Color parseLineColorField(Document theme) {
christian@3155:         String lineColorStr = getLineColorString(theme);
sascha@3160:         logger.debug("parseLineColorField: lineColorStr = " +
christian@3155:                 (lineColorStr == null ? "null" : lineColorStr));
christian@3155:         return parseRGB(lineColorStr);
ingo@1793:     }
ingo@1793: 
ingo@1793: 
ingo@2072:     public static boolean parseShowMinimum(Document theme) {
ingo@2072:         return parseBoolean(getShowMinimum(theme), false);
ingo@2072:     }
ingo@2072: 
ingo@2072:     public static boolean parseShowMaximum(Document theme) {
ingo@2072:         return parseBoolean(getShowMaximum(theme), false);
ingo@2072:     }
ingo@2072: 
ingo@2072: 
ingo@2616:     public static String createWSPLGENStyle(Document theme) {
ingo@2616:         NodeList categories = (NodeList) XMLUtils.xpath(
ingo@2616:             theme,
ingo@2616:             XPATH_WSPLGEN_FIELDS,
ingo@2616:             XPathConstants.NODESET);
ingo@2616: 
ingo@2616:         return createWSPLGENStyle(categories).toString();
ingo@2616:     }
ingo@2616: 
ingo@2616:     protected static MapserverStyle createWSPLGENStyle(NodeList categories) {
ingo@2616:         MapserverStyle ms = new MapserverStyle();
ingo@2616: 
ingo@2616:         for (int i = 0, n = categories.getLength(); i < n; i++) {
ingo@2616:             Element  cat = (Element) categories.item(i);
ingo@2616:             String color = cat.getAttribute("default");
ingo@2616:             String name  = cat.getAttribute("name");
ingo@2616: 
ingo@2616:             String expr;
ingo@2616: 
ingo@2616:             try {
ingo@2616:                 int length = name.length();
ingo@2616:                 int idx    = Integer.valueOf(name.substring(length-1, length));
ingo@2616: 
ingo@2616:                 expr = createWSPLGENExpression(idx);
ingo@2616:             }
ingo@2616:             catch (NumberFormatException nfe) {
ingo@2616:                 logger.warn("Error while parsing WSPLGEN category.", nfe);
ingo@2616:                 continue;
ingo@2616:             }
ingo@2616: 
ingo@2616:             Clazz      c = new Clazz(expr);
ingo@2616:             Style      s = new Style();
ingo@2616:             s.setColor(color.replace(",", ""));
ingo@2616:             s.setSize(5);
ingo@2616: 
ingo@2616:             c.addItem(new Expression("(" + expr + ")"));
ingo@2616:             c.addItem(s);
ingo@2616: 
ingo@2616:             ms.addClazz(c);
ingo@2616:         }
ingo@2616: 
ingo@2616:         return ms;
ingo@2616:     }
ingo@2616: 
ingo@2616: 
ingo@2616:     protected static String createWSPLGENExpression(int idx) {
ingo@2616:         if (idx < 5) {
ingo@2616:             int lower = idx - 1;
ingo@2616:             return "[DIFF] >= " + lower + " AND  [DIFF] < " + idx;
ingo@2616:         }
ingo@2616:         else {
ingo@2616:             return "[DIFF] >= 4";
ingo@2616:         }
ingo@2616:     }
ingo@2616: 
ingo@2616: 
ingo@1793:     public static String createMapserverStyle(Document theme) {
ingo@1816:         String symbol    = getSymbol(theme);
ingo@1878:         String backcolor = getBackgroundColorString(theme);
ingo@1793:         String linecolor = getLineColorString(theme);
ingo@1816: 
ingo@1816:         int linewidth = parseLineWidth(theme);
ingo@1793: 
ingo@1793:         MapserverStyle ms = new MapserverStyle();
ingo@1793: 
ingo@1793:         Clazz c = new Clazz(" ");
ingo@1876: 
ingo@1876:         Style s = new Style();
ingo@1878:         s.setOutlineColor(linecolor.replace(",", ""));
ingo@1878: 
ingo@1878:         if (backcolor != null && backcolor.length() > 0) {
ingo@1878:             s.setColor(backcolor.replace(",", ""));
ingo@1878:         }
ingo@1878: 
ingo@1876:         s.setSize(linewidth);
ingo@1876:         s.setSymbol(symbol);
ingo@1876:         c.addItem(s);
ingo@1876: 
ingo@1876:         String textcolor = getTextColorString(theme);
ingo@1876:         int    textsize  = parseTextSize(theme);
ingo@1876: 
ingo@1876:         if (textcolor != null && textcolor.length() > 0 && textsize > 0) {
ingo@1876:             Label l = new Label();
ingo@1876:             l.setColor(textcolor.replace(",", ""));
ingo@1876:             l.setSize(textsize);
ingo@1876:             c.addItem(l);
ingo@1876:         }
ingo@1793: 
ingo@1793:         ms.addClazz(c);
ingo@1793: 
ingo@1793:         return ms.toString();
felix@1090:     }
felix@1090: }
felix@2016: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :