view src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 89:475ee3e4bc8b

Added "type" as XML attribute to symbols.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 31 May 2011 18:33:54 +0200
parents ef7ca23c4233
children 6b80d2c7536e
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.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
{
    /**
     * 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.
     * @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);
    }

    /**
     * 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)