view flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 1793:1636686070f7

Initial commit to support styles in maps. flys-artifacts/trunk@3116 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 14:42:24 +0000
parents 741ba9e34c7d
children 5364b86a0880
line wrap: on
line source
package de.intevation.flys.utils;

import org.apache.log4j.Logger;

import java.awt.Color;
import java.awt.Font;

import org.w3c.dom.Document;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.artifacts.model.MapserverStyle;
import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;


/**
 * Utility to deal with themes and their representations.
 */
public class ThemeUtil {

    private static Logger logger =
        Logger.getLogger(ThemeUtil.class);

    public final static String XPATH_LINE_COLOR =
        "/theme/field[@name='linecolor']/@default";

    public static final String XPATH_LINE_SIZE =
        "/theme/field[@name='linesize']/@default";

    public static final String XPATH_LINE_STYLE =
        "/theme/field[@name='linetype']/@default";

    public final static String XPATH_SHOW_POINTS =
        "/theme/field[@name='showpoints']/@default";

    public final static String XPATH_SHOW_LINE =
        "/theme/field[@name='showlines']/@default";

    public final static String XPATH_TEXT_COLOR =
        "/theme/field[@name='textcolor']/@default";

    public final static String XPATH_TEXT_SIZE =
        "/theme/field[@name='textsize']/@default";

    public final static String XPATH_TEXT_FONT =
        "/theme/field[@name='font']/@default";

    public final static String XPATH_TEXT_STYLE =
        "/theme/field[@name='textstyle']/@default";

    public final static String XPATH_TEXT_ORIENTATION =
        "/theme/field[@name='textorientation']/@default";

    public final static String XPATH_TEXT_BACKGROUND =
        "/theme/field[@name='backgroundcolor']/@default";

    public final static String XPATH_SHOW_BACKGROUND =
        "/theme/field[@name='showbackground']/@default";

    /**
     * Parses line width, defaulting to 0.
     * @param theme the theme
     */
    public static int parseLineWidth(Document theme) {
        String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null);
        if (size == null || size.length() == 0) {
            return 0;
        }

        try {
            return Integer.valueOf(size);
        }
        catch (NumberFormatException nfe) {
            logger.warn("Unable to set line size from string: '" + size + "'");
        }
        return 0;
    }


    /**
     * Parses the line style, defaulting to '10'.
     * @param theme The theme.
     */
    public static float[] parseLineStyle(Document theme) {
        String dash = XMLUtils.xpathString(theme, XPATH_LINE_STYLE, null);

        float[] def = {10};
        if (dash == null || dash.length() == 0) {
            return def;
        }

        String[] pattern = dash.split(",");
        if(pattern.length == 1) {
            return def;
        }

        try {
            float[] dashes = new float[pattern.length];
            for (int i = 0; i < pattern.length; i++) {
                dashes[i] = Float.parseFloat(pattern[i]);
            }
            return dashes;
        }
        catch(NumberFormatException nfe) {
            logger.warn("Unable to set dash from string: '" + dash + "'");
            return def;
        }
    }


    /**
     * Parses text size, defaulting to 10.
     * @param theme The theme.
     */
    public static int parseTextSize(Document theme) {
        String size = XMLUtils.xpathString(theme, XPATH_TEXT_SIZE, null);
        if (size == null || size.length() == 0) {
            return 10;
        }

        try {
            return Integer.valueOf(size);
        }
        catch (NumberFormatException nfe) {
        }
        return 10;
    }


    /**
     * Parses the attribute 'showpoints', defaults to false.
     * @param theme The theme.
     */
    public static boolean parseShowPoints(Document theme) {
        String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINTS, null);
        if (show == null || show.length() == 0) {
            return false;
        }
        if (show.equals("true")) {
            return true;
        }
        else {
            return false;
        }
    }


    /**
     * Parses the attribute 'showlines', defaults to true.
     * @param theme The theme.
     */
    public static boolean parseShowLine(Document theme) {
        String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE, null);
        if (show == null || show.length() == 0) {
            return true;
        }
        if (show.equals("false")) {
            return false;
        }
        else {
            return true;
        }
    }


    /**
     * Parses text color.
     * @param theme The theme.
     */
    public static Color parseTextColor(Document theme) {
        String color = XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null);
        return parseRGB(color);
    }


    /**
     * Parses the font.
     * @param theme The theme.
     */
    public static Font parseTextFont(Document theme) {
        String font = XMLUtils.xpathString(theme, XPATH_TEXT_FONT, null);
        if (font == null || font.length() == 0) {
            return null;
        }

        int size = parseTextSize(theme);
        int style = parseTextStyle(theme);
        Font f = new Font (font, style, size);
        return f;
    }


    /**
     * Parses the text style, defaults to 'Font.PLAIN'.
     * @param theme The theme.
     */
    public static int parseTextStyle(Document theme) {
        String style = XMLUtils.xpathString(theme, XPATH_TEXT_STYLE, null);
        if (style == null || style.length() == 0) {
            return Font.PLAIN;
        }

        if (style.equals("italic")) {
            return Font.ITALIC;
        }
        else if (style.equals("bold")) {
            return Font.BOLD;
        }
        else {
            return Font.PLAIN;
        }
    }


    /**
     * Parses the textorientation, defaults to 'vertical'.
     * @param theme The theme.
     */
    public static String parseTextOrientation(Document theme) {
        String o = XMLUtils.xpathString(theme, XPATH_TEXT_ORIENTATION, null);
        if (o == null || o.length() == 0) {
            return "vertical";
        }
        if(o.equals("true")) {
            return "horizontal";
        }
        else {
            return "vertical";
        }
    }


    /**
     * Parses the text background color, defaults to white.
     * @param theme The theme.
     */
    public static Color parseTextBackground(Document theme) {
        String color = XMLUtils.xpathString(theme, XPATH_TEXT_BACKGROUND, null);
        if (color == null || color.length() == 0) {
            return Color.WHITE;
        }
        return parseRGB(color);
    }


    /**
     * Parses the attribute whether to show background or not, defaults to
     * false.
     * @param theme The theme.
     */
    public static boolean parseShowTextBackground(Document theme) {
        String show = XMLUtils.xpathString(theme, XPATH_SHOW_BACKGROUND, null);
        if(show == null || show.length() == 0) {
            return false;
        }

        if(show.equals("true")) {
            return true;
        }
        else {
            return false;
        }
    }


    /**
     * Parse a string like "103, 100, 0" and return a corresping color.
     * @param rgbtext Color as string representation, e.g. "255,0,20".
     * @return Color, null in case of issues.
     */
    public static Color parseRGB(String rgbtext) {
        if (rgbtext == null) {
            return null;
        }
        String rgb[] = rgbtext.split(",");
        Color c = null;
        try {
            c = new Color(
                    Integer.valueOf(rgb[0].trim()),
                    Integer.valueOf(rgb[1].trim()),
                    Integer.valueOf(rgb[2].trim()));
        }
        catch (NumberFormatException nfe) {
            c = null;
        }
        return c;
    }


    public static String getLineColorString(Document theme) {
        return XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
    }


    /**
     * Gets color from color field.
     * @param theme    the theme document.
     * @return color.
     */
    public static Color parseLineColorField(Document theme) {
        return parseRGB(getLineColorString(theme));
    }


    public static String createMapserverStyle(Document theme) {
        String linecolor = getLineColorString(theme);
        int    linewidth = parseLineWidth(theme);

        MapserverStyle ms = new MapserverStyle();

        Clazz c = new Clazz(" ");
        c.setOutlineColor(linecolor);
        c.setSize(linewidth);

        ms.addClazz(c);

        return ms.toString();
    }
}

http://dive4elements.wald.intevation.org