view flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 1724:0349dd524f9c

Add convenience method to add default data as string. flys-artifacts/trunk@3006 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 18 Oct 2011 13:26:15 +0000
parents f708120cb7bc
children 6cdc7a77d3d4
line wrap: on
line source
package de.intevation.flys.utils;

import java.awt.Color;

import org.w3c.dom.Document;

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


/**
 * Utility to deal with themes and their representations.
 */
public class ThemeUtil {
    public final static String XPATH_LINE_COLOR =
        "/theme/field[@name='linecolor']/@default";

    public static final String XPATH_LINE_SIZE =
        "/theme/field[@name='linesize']/@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;
    }


    /**
     * 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;
    }

    /**
     * Gets color from color field.
     * @param theme    the theme document.
     * @return color.
     */
    public static Color parseLineColorField(Document theme) {
        String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
        return parseRGB(color);
    }
}

http://dive4elements.wald.intevation.org