annotate 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
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.Document;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 import org.w3c.dom.Element;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14 import org.w3c.dom.NodeList;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
15
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 * 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
18 *
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
19 * @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
20 */
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
21 public class MapToXMLUtils
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
22 {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 private Document root;
38
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
24 private Element rootElement;
31
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, "", "");
38
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
30 rootElement = creator.create("mxd");
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
31 root.appendChild(rootElement);
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
32 }
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
33
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
34 public boolean addFilename(String path) {
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
35 Element file = creator.create("file");
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
36 file.setAttribute("name", path);
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
37 rootElement.appendChild(file);
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
38 return true;
31
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 /**
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 * Create the top level map element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 *
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 * @return The new map element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45 */
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 public Element createMap()
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 throws Exception {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 Element map;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 NodeList list = root.getElementsByTagName("map");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
50 if(list == null || list.getLength() == 0){
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51 map = creator.create("map");
38
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
52 rootElement.appendChild(map);
31
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 else if(list.getLength() == 1){
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55 map = (Element)list.item(0);
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
56 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
57 else{
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 throw new Exception("Error while creating map node.");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
59 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60 return map;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
62
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63 /**
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 * Add a layer element to the map.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 * @param name The layer name.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 * @return The layer element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67 */
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68 public Element addLayer()
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 throws Exception {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
70 Element node = creator.create("layer");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
71 NodeList list = root.getElementsByTagName("map");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
72 if(list == null || list.getLength() == 0){
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
73 throw new Exception("No map node found!");
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 else if(list.getLength() > 1) {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76 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
77 " This should never happen!");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 else {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
80 list.item(0).appendChild(node);
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
81 return node;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
83 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
84
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 * Add a renderer element to the map.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
87 * @param layer The parent layer element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
88 * @return The renderer element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
89 */
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
90 public Element addRenderer(Element layer) {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
91 Element node = creator.create("renderer");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
92 layer.appendChild(node);
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
93 return node;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
94 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
95
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 * Add a symbol element to the map.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
98 * @param renderer The parent renderer element.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
99 * @return The symbol element.
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 Element addSymbol(Element renderer) {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
102 Element node = creator.create("symbol");
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
103 renderer.appendChild(node);
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
104 return node;
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
105 }
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
106
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
107 /**
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
108 * Print out the XML document.
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
109 */
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
110 public void print() {
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
111 XMLUtils.toStream(root, System.out);
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
112 }
38
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
113
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
114 public Document getDocument() {
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
115 return root;
27577294f9ca Added root element to document and save the filename in the XML tree.
Raimund Renkert <rrenkert@intevation.de>
parents: 34
diff changeset
116 }
31
40c0b4e5f91a Added utility class to store map attributes.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
117 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)