felix@1090: package de.intevation.flys.utils;
felix@1090: 
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: 
christian@3738: import de.intevation.artifacts.common.utils.XMLUtils;
christian@3738: import de.intevation.flys.artifacts.model.MapserverStyle;
christian@3738: import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
christian@3738: import de.intevation.flys.artifacts.model.MapserverStyle.Expression;
christian@3738: import de.intevation.flys.artifacts.model.MapserverStyle.Label;
christian@3738: import de.intevation.flys.artifacts.model.MapserverStyle.Style;
christian@3738: 
felix@1090: 
felix@1090: /**
felix@1090:  * Utility to deal with themes and their representations.
felix@1090:  */
felix@1090: public class ThemeUtil {
raimund@1738: 
felix@3624:     /** Private logger. */
raimund@1738:     private static Logger logger =
christian@3464:             Logger.getLogger(ThemeUtil.class);
raimund@1738: 
felix@2016:     public final static String XPATH_FILL_COLOR =
christian@3464:             "/theme/field[@name='fillcolor']/@default";
felix@2016: 
felix@1711:     public final static String XPATH_LINE_COLOR =
christian@3464:             "/theme/field[@name='linecolor']/@default";
felix@1711: 
felix@1711:     public static final String XPATH_LINE_SIZE =
christian@3464:             "/theme/field[@name='linesize']/@default";
felix@1711: 
raimund@1753:     public static final String XPATH_LINE_STYLE =
christian@3464:             "/theme/field[@name='linetype']/@default";
raimund@1753: 
ingo@2076:     public static final String XPATH_POINT_SIZE =
christian@3464:             "/theme/field[@name='pointsize']/@default";
ingo@2076: 
ingo@3090:     public static final String XPATH_POINT_COLOR =
christian@3464:             "/theme/field[@name='pointcolor']/@default";
ingo@3090: 
felix@2016:     public final static String XPATH_SHOW_BORDER =
christian@3464:             "/theme/field[@name='showborder']/@default";
felix@2016: 
raimund@1748:     public final static String XPATH_SHOW_POINTS =
christian@3464:             "/theme/field[@name='showpoints']/@default";
raimund@1748: 
raimund@1753:     public final static String XPATH_SHOW_LINE =
christian@3464:             "/theme/field[@name='showlines']/@default";
raimund@1753: 
felix@2996:     public final static String XPATH_SHOW_VERTICAL_LINE =
christian@3464:             "/theme/field[@name='showverticalline']/@default";
felix@2996: 
felix@2996:     public final static String XPATH_SHOW_HORIZONTAL_LINE =
christian@3464:             "/theme/field[@name='showhorizontalline']/@default";
felix@2996: 
felix@2648:     public final static String XPATH_SHOW_LINE_LABEL =
christian@3464:             "/theme/field[@name='showlinelabel']/@default";
felix@2648: 
christian@3286:     public final static String XPATH_SHOW_POINT_LABEL =
christian@3286:             "/theme/field[@name='showpointlabel']/@default";
christian@3286: 
felix@2663:     public final static String XPATH_SHOW_WIDTH =
christian@3464:             "/theme/field[@name='showwidth']/@default";
felix@2663: 
felix@2663:     public final static String XPATH_SHOW_LEVEL =
felix@3624:             "/theme/field[@name='showlevel']/@default";
felix@2663: 
christian@3738:     public final static String XPATH_TRANSPARENCY =
christian@3738:             "/theme/field[@name='transparency']/@default";
felix@2032: 
felix@2666:     public final static String XPATH_SHOW_AREA =
christian@3464:             "/theme/field[@name='showarea']/@default";
felix@2666: 
felix@2674:     public final static String XPATH_SHOW_MIDDLE_HEIGHT =
christian@3464:             "/theme/field[@name='showmiddleheight']/@default";
raimund@1738: 
christian@3464:     public final static String XPATH_LABEL_FONT_COLOR =
christian@3464:             "/theme/field[@name='labelfontcolor']/@default";
raimund@1738: 
christian@3464:     public final static String XPATH_LABEL_FONT_SIZE =
christian@3464:             "/theme/field[@name='labelfontsize']/@default";
felix@1711: 
christian@3464:     public final static String XPATH_LABEL_FONT_FACE =
christian@3464:             "/theme/field[@name='labelfontface']/@default";
christian@3464: 
christian@3464:     public final static String XPATH_LABEL_FONT_STYLE =
christian@3464:             "/theme/field[@name='labelfontstyle']/@default";
raimund@1748: 
raimund@1748:     public final static String XPATH_TEXT_ORIENTATION =
christian@3464:             "/theme/field[@name='textorientation']/@default";
raimund@1748: 
christian@3464:     public final static String XPATH_LABEL_BGCOLOR =
christian@3464:             "/theme/field[@name='labelbgcolor']/@default";
raimund@1748: 
christian@3464:     public final static String XPATH_LABEL_SHOW_BACKGROUND =
christian@3464:             "/theme/field[@name='labelshowbg']/@default";
christian@3464: 
christian@3464:     public final static String XPATH_BACKGROUND_COLOR =
christian@3464:             "/theme/field[@name='backgroundcolor']/@default";
raimund@1748: 
ingo@1816:     public final static String XPATH_SYMBOL =
christian@3464:             "/theme/field[@name='symbol']/@default";
ingo@1816: 
ingo@2072:     public final static String XPATH_SHOW_MINIMUM =
christian@3464:             "/theme/field[@name='showminimum']/@default";
ingo@2072: 
ingo@2072:     public final static String XPATH_SHOW_MAXIMUM =
christian@3464:             "/theme/field[@name='showmaximum']/@default";
ingo@2072: 
ingo@2616:     public final static String XPATH_WSPLGEN_FIELDS =
christian@3464:             "/theme[@name='WSPLGEN']/field";
ingo@2616: 
felix@3395:     /** XPATH to bandwidth field. */
felix@3394:     public final static String XPATH_BANDWIDTH =
christian@3464:             "/theme/field[@name='bandwidth']/@default";
felix@3394: 
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: 
felix@3399:     /**
felix@3399:      * Attempt converting \param value to an integer, in failing cases,
felix@3399:      * return \param defaultsTo.
felix@3399:      * @param value String to be converted to integer.
felix@3399:      * @param defaultsTo Default to return if conversion failed.
felix@3399:      * @return \param value as integer or defaultsto if conversion failed.
felix@3399:      */
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 {
sascha@3405:             return Integer.parseInt(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@3399:      * Attempt converting \param value to a double, in failing cases,
felix@3399:      * return \param defaultsTo.
felix@3399:      * @param value String to be converted to double.
felix@3399:      * @param defaultsTo Default to return if conversion failed.
felix@3399:      * @return \param value as integer or defaultsto if conversion failed.
felix@3399:      */
felix@3399:     public static double parseDouble(String value, double defaultsTo) {
felix@3399:         if (value == null || value.length() == 0) {
felix@3399:             return defaultsTo;
felix@3399:         }
felix@3399: 
felix@3399:         try {
sascha@3561:             return Double.parseDouble(value);
felix@3399:         }
felix@3399:         catch (NumberFormatException nfe) {
felix@3399:             // do nothing
felix@3399:         }
felix@3399: 
felix@3399:         return defaultsTo;
felix@3399:     }
felix@3399: 
felix@3399: 
felix@3399:     /**
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 {
sascha@3405:             return Integer.parseInt(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: 
felix@3395:     /**
felix@3395:      * Parse band width, defaulting to 0.
felix@3395:      * @param theme the theme.
felix@3395:      */
felix@3399:     public static double parseBandWidth(Document theme) {
felix@3394:         String bandWidth = XMLUtils.xpathString(theme, XPATH_BANDWIDTH, null);
felix@3394: 
felix@3399:         return parseDouble(bandWidth, 0);
felix@3394:     }
felix@3394: 
felix@3395: 
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 {
sascha@3405:             return Integer.parseInt(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) {
christian@3464:         return parseTextSize(theme, XPATH_LABEL_FONT_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: 
christian@3286:     public static boolean parseShowPointLabel(Document theme) {
christian@3286:         String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINT_LABEL, null);
christian@3286:         return parseBoolean(show, false);
christian@3286:     }
christian@3286: 
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: 
raimund@1738:     /**
raimund@1738:      * Parses the font.
raimund@1738:      * @param theme The theme.
raimund@1738:      */
raimund@1738:     public static Font parseTextFont(Document theme) {
christian@3464:         String font = XMLUtils.xpathString(theme, XPATH_LABEL_FONT_FACE, 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: 
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) {
christian@3464:         return parseTextStyle(theme, XPATH_LABEL_FONT_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: 
christian@3464: 
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) {
christian@3464:         String color = getLabelBackgroundColorString(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:      */
christian@3464:     public static boolean parseLabelShowBackground(Document theme) {
christian@3464:         String show = XMLUtils.xpathString(theme, XPATH_LABEL_SHOW_BACKGROUND, 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(
sascha@3405:                     Integer.parseInt(rgb[0].trim()),
sascha@3405:                     Integer.parseInt(rgb[1].trim()),
sascha@3405:                     Integer.parseInt(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: 
christian@3464: 
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: 
christian@3464: 
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: 
christian@3464: 
christian@3464:     public static String getLabelBackgroundColorString(Document theme) {
christian@3464:         return XMLUtils.xpathString(theme, XPATH_LABEL_BGCOLOR, null);
christian@3464:     }
christian@3464: 
christian@3464: 
ingo@1878:     public static String getBackgroundColorString(Document theme) {
christian@3464:         return XMLUtils.xpathString(theme, XPATH_BACKGROUND_COLOR, null);
ingo@1878:     }
ingo@1878: 
ingo@1878: 
ingo@1876:     public static String getTextColorString(Document theme) {
christian@3464:         String textColor = XMLUtils.xpathString(theme, XPATH_LABEL_FONT_COLOR, null);
christian@3464:         return textColor;
ingo@1876:     }
ingo@1876: 
ingo@1876: 
ingo@1816:     public static String getSymbol(Document theme) {
ingo@1816:         return XMLUtils.xpathString(theme, XPATH_SYMBOL, null);
ingo@1816:     }
ingo@1816: 
christian@3464: 
felix@2032:     public static String getTransparencyString(Document theme) {
felix@2032:         return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY, null);
felix@2032:     }
sascha@3256: 
sascha@3256: 
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: 
christian@3464: 
felix@2016:     public static boolean parseShowBorder(Document theme) {
felix@2016:         return parseBoolean(getShowBorderString(theme), false);
felix@2016:     }
felix@2016: 
christian@3464: 
christian@3889:     public static int parseTransparency(Document theme) {
christian@3889:         return parseInteger(getTransparencyString(theme), 50);
felix@2032:     }
felix@2016: 
christian@3464: 
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: 
christian@3464: 
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(
christian@3464:                 theme,
christian@3464:                 XPATH_WSPLGEN_FIELDS,
christian@3464:                 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();
sascha@3405:                 int idx    = Integer.parseInt(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);
christian@3464:         String backcolor = getLabelBackgroundColorString(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 :