Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java @ 1119:7c4f81f74c47
merged gnv-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:00 +0200 |
parents | dec4257ad570 |
children |
comparison
equal
deleted
inserted
replaced
1027:fca4b5eb8d2f | 1119:7c4f81f74c47 |
---|---|
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 | |
9 package de.intevation.gnv.utils; | |
10 | |
11 import java.io.File; | |
12 import java.io.FileNotFoundException; | |
13 import java.io.FileOutputStream; | |
14 import java.io.IOException; | |
15 import java.io.OutputStream; | |
16 import java.text.DecimalFormat; | |
17 import java.util.Date; | |
18 | |
19 import javax.xml.xpath.XPathConstants; | |
20 | |
21 import org.apache.log4j.Logger; | |
22 import org.w3c.dom.Document; | |
23 import org.w3c.dom.Element; | |
24 import org.w3c.dom.Node; | |
25 | |
26 import com.vividsolutions.jts.geom.Envelope; | |
27 | |
28 import de.intevation.artifacts.common.utils.XMLUtils; | |
29 import de.intevation.artifacts.ArtifactNamespaceContext; | |
30 import de.intevation.artifacts.CallContext; | |
31 import de.intevation.gnv.artifacts.context.GNVArtifactContext; | |
32 import de.intevation.gnv.wms.LayerInfo; | |
33 | |
34 /** | |
35 * This class provides some methods to create files storing meta information | |
36 * about wms layers and a map service which serves these layers. | |
37 * | |
38 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
39 */ | |
40 public class MetaWriter { | |
41 | |
42 private static Logger logger = Logger.getLogger(MetaWriter.class); | |
43 | |
44 public static final String NODE_MAPSERVER = "mapserver"; | |
45 public static final String NODE_SERVER = "server"; | |
46 public static final String NODE_MAP = "map"; | |
47 public static final String NODE_TTL = "ttl"; | |
48 | |
49 public static final String META_FILE_NAME = "meta.xml"; | |
50 | |
51 public static final String XPATH_META = "/art:meta"; | |
52 public static final String XPATH_MAPSERVER = "art:meta/art:"+NODE_MAPSERVER; | |
53 | |
54 /** | |
55 * Constructor. | |
56 */ | |
57 private MetaWriter() { | |
58 } | |
59 | |
60 | |
61 /** | |
62 * Just create a new document and insert the root node. | |
63 * | |
64 * @return the meta document with necessary root node. | |
65 */ | |
66 public static Document initMeta() { | |
67 Document meta = XMLUtils.newDocument(); | |
68 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
69 meta, | |
70 ArtifactNamespaceContext.NAMESPACE_URI, | |
71 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
72 | |
73 Element root = creator.create("meta"); | |
74 meta.appendChild(root); | |
75 | |
76 return meta; | |
77 } | |
78 | |
79 public static boolean insertLayer( | |
80 CallContext context, | |
81 Document document, | |
82 String name, | |
83 String title, | |
84 String data, | |
85 String model, | |
86 String type, | |
87 String status | |
88 ) { | |
89 if (document == null) { | |
90 document = initMeta(); | |
91 } | |
92 | |
93 Node meta = (Node) XMLUtils.xpath( | |
94 document, | |
95 XPATH_META, | |
96 XPathConstants.NODE, | |
97 ArtifactNamespaceContext.INSTANCE); | |
98 | |
99 if (meta == null) { | |
100 logger.error("No meta node was found in the given document. " | |
101 + "Cannot insert layer!"); | |
102 return false; | |
103 } | |
104 | |
105 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
106 document, | |
107 ArtifactNamespaceContext.NAMESPACE_URI, | |
108 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
109 | |
110 Element layerEl = creator.create(LayerInfo.LAYER); | |
111 Element nameEl = creator.create(LayerInfo.LAYER_NAME); | |
112 Element titleEl = creator.create(LayerInfo.LAYER_TITLE); | |
113 Element dataEl = creator.create(LayerInfo.LAYER_DATA); | |
114 Element modelEl = creator.create(LayerInfo.LAYER_MODEL); | |
115 Element typeEl = creator.create(LayerInfo.LAYER_TYPE); | |
116 Element statusEl = creator.create(LayerInfo.LAYER_STATUS); | |
117 | |
118 modelEl.setTextContent(model); | |
119 nameEl.setTextContent(name); | |
120 titleEl.setTextContent(title); | |
121 typeEl.setTextContent(type); | |
122 statusEl.setTextContent(status); | |
123 dataEl.setTextContent(data); | |
124 | |
125 layerEl.appendChild(modelEl); | |
126 layerEl.appendChild(nameEl); | |
127 layerEl.appendChild(titleEl); | |
128 layerEl.appendChild(dataEl); | |
129 layerEl.appendChild(typeEl); | |
130 layerEl.appendChild(statusEl); | |
131 | |
132 if (logger.isDebugEnabled()) { | |
133 logger.debug("--------------- WMS LAYER PARAMS ---------------"); | |
134 logger.debug("Name : " + name); | |
135 logger.debug("Title : " + title); | |
136 logger.debug("Data : " + data); | |
137 logger.debug("Type : " + type); | |
138 logger.debug("Model : " + model); | |
139 logger.debug("Status: " + status); | |
140 } | |
141 | |
142 meta.appendChild(layerEl); | |
143 | |
144 return true; | |
145 } | |
146 | |
147 /** | |
148 * Method to write the <i>meta</i> document down to a file. | |
149 * | |
150 * @param path The destination of the file. | |
151 * @param meta The xml document storing the meta information. | |
152 */ | |
153 public static boolean insertMbr(Envelope mbr, String srs, Document meta) { | |
154 Node mapserverNode = (Node) XMLUtils.xpath( | |
155 meta, | |
156 XPATH_MAPSERVER, | |
157 XPathConstants.NODE, | |
158 ArtifactNamespaceContext.INSTANCE); | |
159 if (mapserverNode != null){ | |
160 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
161 meta, | |
162 ArtifactNamespaceContext.NAMESPACE_URI, | |
163 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
164 Element bboxNode = creator.create("Box"); | |
165 bboxNode.setAttribute("srsName", srs); | |
166 Element coordinateNode = creator.create("coordinates"); | |
167 | |
168 DecimalFormat formatter = new DecimalFormat("###.############"); | |
169 coordinateNode.setTextContent(formatter.format(mbr.getMinX()).replace(',', '.') +","+ | |
170 formatter.format(mbr.getMinY()).replace(',', '.')+" "+ | |
171 formatter.format(mbr.getMaxX()).replace(',', '.') +","+ | |
172 formatter.format(mbr.getMaxY()).replace(',', '.')); | |
173 mapserverNode.appendChild(bboxNode); | |
174 bboxNode.appendChild(coordinateNode); | |
175 } | |
176 return true; | |
177 } | |
178 /** | |
179 * Method to write the <i>meta</i> document down to a file. | |
180 * | |
181 * @param path The destination of the file. | |
182 * @param meta The xml document storing the meta information. | |
183 */ | |
184 public static boolean writeMetaFile(String path, Document meta) { | |
185 try { | |
186 File metaFile = new File(path, META_FILE_NAME); | |
187 | |
188 if (metaFile.exists()) { | |
189 logger.info("Delete old meta information file."); | |
190 metaFile.delete(); | |
191 } | |
192 | |
193 if (!metaFile.createNewFile() || !metaFile.canWrite()) { | |
194 logger.error("Error while writing meta file: "+metaFile.toString()); | |
195 return false; | |
196 } | |
197 | |
198 OutputStream out = null; | |
199 boolean success = false; | |
200 try { | |
201 out = new FileOutputStream(metaFile); | |
202 success = XMLUtils.toStream(meta, out); | |
203 } | |
204 finally { | |
205 if (out != null) { | |
206 try { out.close(); } | |
207 catch (IOException ioe) {} | |
208 } | |
209 } | |
210 | |
211 if (!success && metaFile.exists()) { | |
212 metaFile.delete(); | |
213 } | |
214 | |
215 return success; | |
216 } | |
217 catch (FileNotFoundException fnfe) { | |
218 logger.error(fnfe); | |
219 return false; | |
220 } | |
221 catch (IOException ioe) { | |
222 logger.error(ioe, ioe); | |
223 return false; | |
224 } | |
225 } | |
226 | |
227 | |
228 /** | |
229 * Append meta information about the mapservice itself. | |
230 * | |
231 * @param callContext The CallContext object. | |
232 * @param document The meta information document. | |
233 * @param meta The element where the new information need to be appended to. | |
234 */ | |
235 public static void insertAbstractMeta( | |
236 CallContext callContext, | |
237 Document document | |
238 ) { | |
239 if (document == null) { | |
240 document = initMeta(); | |
241 } | |
242 | |
243 Node meta = (Node) XMLUtils.xpath( | |
244 document, | |
245 XPATH_META, | |
246 XPathConstants.NODE, | |
247 ArtifactNamespaceContext.INSTANCE); | |
248 | |
249 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
250 document, | |
251 ArtifactNamespaceContext.NAMESPACE_URI, | |
252 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
253 | |
254 GNVArtifactContext context = | |
255 (GNVArtifactContext) callContext.globalContext(); | |
256 | |
257 String server = (String) | |
258 context.get(GNVArtifactContext.MAPSERVER_SERVER_PATH_KEY); | |
259 | |
260 String map = (String) | |
261 context.get(GNVArtifactContext.MAPSERVER_MAP_PATH_KEY); | |
262 | |
263 Long time = callContext.getTimeToLive(); | |
264 time = time != null ? time + new Date().getTime() : null; | |
265 String ttl = time != null ? time.toString() : null; | |
266 | |
267 if (logger.isDebugEnabled()) { | |
268 logger.debug("MAPSERVER PATH: " + server); | |
269 logger.debug("MAP PATH: " + map); | |
270 logger.debug("TTL: " + ttl); | |
271 } | |
272 | |
273 Element mapserver = creator.create(NODE_MAPSERVER); | |
274 Element serverPath = creator.create(NODE_SERVER); | |
275 Element mapPath = creator.create(NODE_MAP); | |
276 Element timetolive = creator.create(NODE_TTL); | |
277 | |
278 mapPath.setTextContent(map); | |
279 serverPath.setTextContent(server); | |
280 timetolive.setTextContent(ttl); | |
281 | |
282 mapserver.appendChild(serverPath); | |
283 mapserver.appendChild(mapPath); | |
284 mapserver.appendChild(timetolive); | |
285 meta.appendChild(mapserver); | |
286 } | |
287 } | |
288 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |