rrenkert@31: /* rrenkert@31: * Copyright (c) 2010 by Intevation GmbH rrenkert@31: * rrenkert@31: * This program is free software under the LGPL (>=v2.1) rrenkert@31: * Read the file LGPL.txt coming with the software for details rrenkert@31: * or visit http://www.gnu.org/licenses/ if it does not exist. rrenkert@31: */ rrenkert@31: package de.intevation.mxd.utils; rrenkert@31: rrenkert@31: import org.apache.log4j.Logger; rrenkert@31: rrenkert@31: import org.w3c.dom.Document; rrenkert@31: import org.w3c.dom.Element; rrenkert@31: import org.w3c.dom.NodeList; rrenkert@31: rrenkert@31: /** rrenkert@31: * Utility class to store map information in a XML structure. rrenkert@31: * rrenkert@31: * @author Raimund Renkert rrenkert@31: */ rrenkert@31: public class MapToXMLUtils rrenkert@31: { rrenkert@31: private Document root; rrenkert@31: private XMLUtils.ElementCreator creator; rrenkert@31: rrenkert@31: public MapToXMLUtils() { rrenkert@31: this.root = XMLUtils.newDocument(); rrenkert@31: creator = new XMLUtils.ElementCreator(root, "", ""); rrenkert@31: } rrenkert@31: rrenkert@31: /** rrenkert@31: * Create the top level map element. rrenkert@31: * rrenkert@31: * @return The new map element. rrenkert@31: */ rrenkert@31: public Element createMap() rrenkert@31: throws Exception { rrenkert@31: Element map; rrenkert@31: NodeList list = root.getElementsByTagName("map"); rrenkert@31: if(list == null || list.getLength() == 0){ rrenkert@31: map = creator.create("map"); rrenkert@31: root.appendChild(map); rrenkert@31: } rrenkert@31: else if(list.getLength() == 1){ rrenkert@31: map = (Element)list.item(0); rrenkert@31: } rrenkert@31: else{ rrenkert@31: throw new Exception("Error while creating map node."); rrenkert@31: } rrenkert@31: return map; rrenkert@31: } rrenkert@31: rrenkert@31: /** rrenkert@31: * Add a layer element to the map. rrenkert@31: * @param name The layer name. rrenkert@31: * @return The layer element. rrenkert@31: */ rrenkert@31: public Element addLayer() rrenkert@31: throws Exception { rrenkert@31: Element node = creator.create("layer"); rrenkert@31: NodeList list = root.getElementsByTagName("map"); rrenkert@31: if(list == null || list.getLength() == 0){ rrenkert@31: throw new Exception("No map node found!"); rrenkert@31: } rrenkert@31: else if(list.getLength() > 1) { rrenkert@31: throw new Exception("Found more than one map node." + rrenkert@31: " This should never happen!"); rrenkert@31: } rrenkert@31: else { rrenkert@31: list.item(0).appendChild(node); rrenkert@31: return node; rrenkert@31: } rrenkert@31: } rrenkert@31: rrenkert@31: /** rrenkert@31: * Add a renderer element to the map. rrenkert@31: * @param layer The parent layer element. rrenkert@31: * @return The renderer element. rrenkert@31: */ rrenkert@31: public Element addRenderer(Element layer) { rrenkert@31: Element node = creator.create("renderer"); rrenkert@31: layer.appendChild(node); rrenkert@31: return node; rrenkert@31: } rrenkert@31: rrenkert@31: /** rrenkert@31: * Add a symbol element to the map. rrenkert@31: * @param renderer The parent renderer element. rrenkert@31: * @return The symbol element. rrenkert@31: */ rrenkert@31: public Element addSymbol(Element renderer) { rrenkert@31: Element node = creator.create("symbol"); rrenkert@31: renderer.appendChild(node); rrenkert@31: return node; rrenkert@31: } rrenkert@31: rrenkert@31: /** rrenkert@31: * Print out the XML document. rrenkert@31: */ rrenkert@31: public void print() { rrenkert@31: XMLUtils.toStream(root, System.out); rrenkert@31: } rrenkert@31: }