view src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 272:f4539c905008

* website/download-de.htm4, website/download.htm4: Changed order of download-boxes
author Stephan Holl <stephan.holl@intevation.de>
date Fri, 09 Sep 2011 11:31:42 +0200
parents 10e0aa283217
children 1d77ea6a915d
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de>
 *
 * This file is part of MXD2map.
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LICENCE.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 *
 * MXD2map has been developed on behalf of the
 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg
 * by Intevation GmbH.
 *
 * Authors:
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Bjoern Schilberg <bjoern.schilberg@intevation.de>
 * Stephan Holl <stephan.holl@intevation.de>
 */

package de.intevation.mxd.utils;

import org.apache.log4j.Logger;

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

/**
 * Utility class to store map information in a XML structure.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class MapToXMLUtils
{
    /**
     * The logger.
     */
    private static final Logger logger = Logger.getLogger(MapToXMLUtils.class);

    /**
     * Private member.
     */
    private Document root;
    private Element rootElement;
    private XMLUtils.ElementCreator creator;


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

    /**
     * Add the mxd filename to the dom.
     * @param path Path to the mxdfile.
     * @return Currently always true.
     */
    public boolean addFilename(String path) {
        Element file = creator.create("file");
        file.setAttribute("name", path);
        rootElement.appendChild(file);
        return true;
    }

    /**
     * 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");
            rootElement.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.
     *
     * @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;
        }
    }

    /**
     * Remove a layer element from map.
     * @param layerElement The layer element.
     */
    public void removeLayer(Element layerElement) {
        NodeList list = root.getElementsByTagName("map");
        if(list == null || list.getLength() == 0){
            return;
        }
        else if(list.getLength() > 1) {
            return;
        }
        else {
            try {
                list.item(0).removeChild(layerElement);
            }
            catch(DOMException de) {
                return;
            }
        }
    }

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

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

    /**
     * Print out the XML document.
     */
    public void print() {
        XMLUtils.toStream(root, System.out);
    }

    /**
     * Get the DOM document.
     * @return The root document.
     */
    public Document getDocument() {
        return root;
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)