view src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 38:27577294f9ca

Added root element to document and save the filename in the XML tree.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 14 Apr 2011 11:04:59 +0200
parents 7a927921eb6c
children ef7ca23c4233
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
{
    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);
    }

    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);
    }

    public Document getDocument() {
        return root;
    }
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)