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