Mercurial > mxd2map
view src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 181:0bde090506f9
Added comments.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 11 Jul 2011 14:28:38 +0200 |
parents | f4eb506499f5 |
children | df4e0946ef02 |
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; 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. * @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; } } /** * Remove a layer element from map. * @param name The layer name. */ 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; } }