view src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 31:40c0b4e5f91a

Added utility class to store map attributes.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 11 Apr 2011 16:04:03 +0200
parents
children 7a927921eb6c
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */
package de.intevation.mxd.utils;

import org.apache.log4j.Logger;

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

/**
 * Utility class to store map information in a XML structure.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class MapToXMLUtils
{
    private Document root;
    private XMLUtils.ElementCreator creator;

    public MapToXMLUtils() {
        this.root = XMLUtils.newDocument();
        creator = new XMLUtils.ElementCreator(root, "", "");
    }

    /**
     * Create the top level map element.
     *
     * @return The new map element.
     */
    public Element createMap()
    throws Exception {
        Element map;
        NodeList list = root.getElementsByTagName("map");
        if(list == null || list.getLength() == 0){
            map = creator.create("map");
            root.appendChild(map);
        }
        else if(list.getLength() == 1){
            map = (Element)list.item(0);
        }
        else{
            throw new Exception("Error while creating map node.");
        }
        return map;
    }

    /**
     * Add a layer element to the map.
     * @param name The layer name.
     * @return The layer element.
     */
    public Element addLayer()
    throws Exception {
        Element node = creator.create("layer");
        NodeList list = root.getElementsByTagName("map");
        if(list == null || list.getLength() == 0){
            throw new Exception("No map node found!");
        }
        else if(list.getLength() > 1) {
            throw new Exception("Found more than one map node." +
                                " This should never happen!");
        }
        else {
            list.item(0).appendChild(node);
            return node;
        }
    }

    /**
     * Add a renderer element to the map.
     * @param layer The parent layer element.
     * @return The renderer element.
     */
    public Element addRenderer(Element layer) {
        Element node = creator.create("renderer");
        layer.appendChild(node);
        return node;
    }

    /**
     * Add a symbol element to the map.
     * @param renderer The parent renderer element.
     * @return The symbol element.
     */
    public Element addSymbol(Element renderer) {
        Element node = creator.create("symbol");
        renderer.appendChild(node);
        return node;
    }

    /**
     * Print out the XML document.
     */
    public void print() {
        XMLUtils.toStream(root, System.out);
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)