comparison src/java/de/intevation/mxd/utils/MapToXMLUtils.java @ 31:40c0b4e5f91a

Added utility class to store map attributes.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 11 Apr 2011 16:04:03 +0200
parents
children 7a927921eb6c
comparison
equal deleted inserted replaced
30:c34c450edb06 31:40c0b4e5f91a
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8 package de.intevation.mxd.utils;
9
10 import org.apache.log4j.Logger;
11
12 import org.w3c.dom.Attr;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.NodeList;
16
17 /**
18 * Utility class to store map information in a XML structure.
19 *
20 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
21 */
22 public class MapToXMLUtils
23 {
24 private Document root;
25 private XMLUtils.ElementCreator creator;
26
27 public MapToXMLUtils() {
28 this.root = XMLUtils.newDocument();
29 creator = new XMLUtils.ElementCreator(root, "", "");
30 }
31
32 /**
33 * Create the top level map element.
34 *
35 * @return The new map element.
36 */
37 public Element createMap()
38 throws Exception {
39 Element map;
40 NodeList list = root.getElementsByTagName("map");
41 if(list == null || list.getLength() == 0){
42 map = creator.create("map");
43 root.appendChild(map);
44 }
45 else if(list.getLength() == 1){
46 map = (Element)list.item(0);
47 }
48 else{
49 throw new Exception("Error while creating map node.");
50 }
51 return map;
52 }
53
54 /**
55 * Add a layer element to the map.
56 * @param name The layer name.
57 * @return The layer element.
58 */
59 public Element addLayer()
60 throws Exception {
61 Element node = creator.create("layer");
62 NodeList list = root.getElementsByTagName("map");
63 if(list == null || list.getLength() == 0){
64 throw new Exception("No map node found!");
65 }
66 else if(list.getLength() > 1) {
67 throw new Exception("Found more than one map node." +
68 " This should never happen!");
69 }
70 else {
71 list.item(0).appendChild(node);
72 return node;
73 }
74 }
75
76 /**
77 * Add a renderer element to the map.
78 * @param layer The parent layer element.
79 * @return The renderer element.
80 */
81 public Element addRenderer(Element layer) {
82 Element node = creator.create("renderer");
83 layer.appendChild(node);
84 return node;
85 }
86
87 /**
88 * Add a symbol element to the map.
89 * @param renderer The parent renderer element.
90 * @return The symbol element.
91 */
92 public Element addSymbol(Element renderer) {
93 Element node = creator.create("symbol");
94 renderer.appendChild(node);
95 return node;
96 }
97
98 /**
99 * Print out the XML document.
100 */
101 public void print() {
102 XMLUtils.toStream(root, System.out);
103 }
104 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)