view flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java @ 1586:dbdf954dbe94

Cosmetics. flys-client/trunk@3873 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 02 Feb 2012 07:40:44 +0000
parents 0f2b94408bd1
children e53d773e6992
line wrap: on
line source
package de.intevation.flys.client.server;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import org.apache.log4j.Logger;

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

import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DefaultData;
import de.intevation.flys.client.shared.model.DefaultDataItem;
import de.intevation.flys.client.shared.model.DoubleArrayData;
import de.intevation.flys.client.shared.model.IntegerArrayData;
import de.intevation.flys.client.shared.model.IntegerData;
import de.intevation.flys.client.shared.model.IntegerOptionsData;
import de.intevation.flys.client.shared.model.IntegerRangeData;
import de.intevation.flys.client.shared.model.StringData;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DataFactory {

    private static final Logger logger = Logger.getLogger(DataFactory.class);

    public static final String NS_URI = ArtifactNamespaceContext.NAMESPACE_URI;


    /**
     * Creates a new Data instance based on the <i>art:type</i> attribute of
     * <i>element</i>.
     *
     * @param element The Data element.
     *
     * @return a Data instance.
     */
    public static Data createDataFromElement(Element element) {
        String name = element.getAttributeNS(NS_URI, "name");
        String type = element.getAttributeNS(NS_URI, "type");

        try {
            logger.debug("Create Data instance for: " + name + " | " + type);

            if (type == null || type.length() == 0) {
                return createDefaultData(element, name);
            }

            type = type.toLowerCase();

            if (type.equals(StringData.TYPE)) {
                return createStringData(element, name);
            }
            else if (type.equals(IntegerData.TYPE)) {
                return createIntegerData(element, name);
            }
            else if (type.equals(IntegerOptionsData.TYPE)) {
                return createIntegerOptionsData(element, name);
            }
            else if (type.equals(IntegerRangeData.TYPE)) {
                return createIntegerRangeData(element, name);
            }
            else if (type.equals(IntegerArrayData.TYPE)) {
                return createIntegerArrayData(element, name);
            }
            else if (type.equals(DoubleArrayData.TYPE)) {
                return createDoubleArrayData(element, name);
            }
            else {
                return createDefaultData(element, name);
            }
        }
        catch (Exception e) {
            logger.error("Error while data creation for: " + name);
        }

        return null;
    }


    /**
     * This method creates a new instance of DefaultData which has no real type
     * set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of DefaultData.
     */
    protected static Data createDefaultData(Element ele, String name) {
        logger.debug("Create new DefaultData");
        return new DefaultData(name, name, "default", extractDataItems(ele));
    }


    /**
     * This method creates a new instance of StringData which has a type
     * "string" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of StringData.
     */
    protected static Data createStringData(Element ele, String name) {
        return new StringData(name, name, extractDataItems(ele));
    }


    /**
     * This method creates a new instance of DefaultData which has a type
     * "integer" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of IntegerData.
     */
    protected static Data createIntegerData(Element ele, String name) {
        return new IntegerData(name, name, extractDataItems(ele));
    }


    /**
     * This method creates a new instance of DefaultData which has a type
     * "intoptions" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of IntegerOptionsData.
     */
    protected static Data createIntegerOptionsData(Element ele, String name) {
        return new IntegerOptionsData(name, name, extractDataItems(ele));
    }


    /**
     * This method creates a new instance of DefaultData which has a type
     * "intrange" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of IntegerRangeData.
     */
    protected static Data createIntegerRangeData(Element ele, String name) {
        DataItem[] items    = extractDataItems(ele);
        String     rawValue = items[0].getStringValue();

        String[] minmax = rawValue.split(";");

        return new IntegerRangeData(
            name,
            name,
            Integer.valueOf(minmax[0]),
            Integer.valueOf(minmax[1]));
    }


    /**
     * This method creates a new instance of DefaultData which has a type
     * "integerarray" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of IntegerArrayData.
     */
    protected static Data createIntegerArrayData(Element ele, String name) {
        DataItem[] items    = extractDataItems(ele);
        String     rawValue = items[0].getStringValue();

        String[] values   = rawValue.split(";");
        int[]    integers = new int[values.length];

        for (int i = 0; i < values.length; i++) {
            try {
                integers[i] = Integer.valueOf(values[i]);
            }
            catch (NumberFormatException nfe) {
                logger.warn("Error while parsing IntegerArrayData: " + nfe);
            }
        }

        return new IntegerArrayData(name, name, integers);
    }


    /**
     * This method creates a new instance of DefaultData which has a type
     * "doublearray" set.
     *
     * @param ele The Data element.
     * @param name The name of the Data instance.
     *
     * @return an instance of DoubleArrayData.
     */
    protected static Data createDoubleArrayData(Element ele, String name) {
        return new DoubleArrayData(name, name, extractDataItems(ele));
    }


    /**
     * This method extracts the art:item elements placed under <i>elements</i>.
     *
     * @param element A data node that contains items.
     *
     * @return a list of DataItems.
     */
    protected static DataItem[] extractDataItems(Element element) {
        NodeList itemList = (NodeList) XMLUtils.xpath(
            element,
            "art:item",
            XPathConstants.NODESET,
            ArtifactNamespaceContext.INSTANCE);

        if (itemList == null || itemList.getLength() == 0) {
            logger.debug("No old data items found.");
            return null;
        }

        int count = itemList.getLength();

        DataItem[] items = new DataItem[count];

        for (int i = 0; i < count; i++) {
            Element tmp = (Element) itemList.item(i);

            String value = tmp.getAttributeNS(NS_URI, "value");
            String label = tmp.getAttributeNS(NS_URI, "label");

            items[i] = new DefaultDataItem(label, label, value);
        }

        return items;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org