Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/MapfileGenerator.java @ 806:2cea76f1112e
Added Javadoc in utils package.
gnv-artifacts/trunk@888 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 08 Apr 2010 13:10:39 +0000 |
parents | 6cff63d0c434 |
children | a645bd23c1c8 |
comparison
equal
deleted
inserted
replaced
805:bb7afd783321 | 806:2cea76f1112e |
---|---|
30 import org.w3c.dom.Document; | 30 import org.w3c.dom.Document; |
31 import org.w3c.dom.Node; | 31 import org.w3c.dom.Node; |
32 import org.w3c.dom.NodeList; | 32 import org.w3c.dom.NodeList; |
33 | 33 |
34 /** | 34 /** |
35 * This class iterates over a bunch of directories, searches for meta | |
36 * information coresponding to shapefiles and creates a mapfile which is used by | |
37 * a <i>MapServer</i>. | |
38 * | |
35 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 39 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
36 */ | 40 */ |
37 public class MapfileGenerator | 41 public class MapfileGenerator |
38 extends Thread | 42 extends Thread |
39 { | 43 { |
44 /** | |
45 * The configured template path. | |
46 */ | |
40 public static final String TEMPLATE_PATH = | 47 public static final String TEMPLATE_PATH = |
41 "/artifact-database/gnv/map-generator/templates/path/text()"; | 48 "/artifact-database/gnv/map-generator/templates/path/text()"; |
42 | 49 |
50 /** | |
51 * The configured template mapfile. | |
52 */ | |
43 public static final String TEMPLATE_MAPFILE = | 53 public static final String TEMPLATE_MAPFILE = |
44 "/artifact-database/gnv/map-generator/templates/maptemplate/text()"; | 54 "/artifact-database/gnv/map-generator/templates/maptemplate/text()"; |
45 | 55 |
56 /** | |
57 * The configured mapfile path. | |
58 */ | |
46 public static final String MAPFILE_PATH = | 59 public static final String MAPFILE_PATH = |
47 "/artifact-database/gnv/map-generator/mapfile/@path"; | 60 "/artifact-database/gnv/map-generator/mapfile/@path"; |
48 | 61 |
62 /** | |
63 * The configured shapefile base directory where the subdirectories for each | |
64 * artifact are stored. | |
65 */ | |
49 public static final String SHAPEFILE_BASE_DIR = | 66 public static final String SHAPEFILE_BASE_DIR = |
50 "/artifact-database/gnv/shapefile-directory/@path"; | 67 "/artifact-database/gnv/shapefile-directory/@path"; |
51 | 68 |
69 /** | |
70 * The configured velocity logfile. | |
71 */ | |
52 public static final String VELOCITY_LOGFILE = | 72 public static final String VELOCITY_LOGFILE = |
53 "/artifact-database/velocity/logfile/@path"; | 73 "/artifact-database/velocity/logfile/@path"; |
54 | 74 |
75 /** | |
76 * The name of the file storing meta information coresponding to shapefiles. | |
77 */ | |
55 public static final String META_FILE_NAME = "meta.xml"; | 78 public static final String META_FILE_NAME = "meta.xml"; |
56 | 79 |
80 /** | |
81 * The XPath to layer nodes in the meta information xml file. | |
82 */ | |
57 public static final String XPATH_LAYER = "/art:meta/art:layer"; | 83 public static final String XPATH_LAYER = "/art:meta/art:layer"; |
84 | |
58 public static final String XPATH_LAYER_NAME = "art:name"; | 85 public static final String XPATH_LAYER_NAME = "art:name"; |
59 public static final String XPATH_LAYER_TITLE = "art:title"; | 86 public static final String XPATH_LAYER_TITLE = "art:title"; |
60 public static final String XPATH_LAYER_TYPE = "art:type"; | 87 public static final String XPATH_LAYER_TYPE = "art:type"; |
61 public static final String XPATH_LAYER_DATA = "art:data"; | 88 public static final String XPATH_LAYER_DATA = "art:data"; |
62 public static final String XPATH_LAYER_STATUS = "art:status"; | 89 public static final String XPATH_LAYER_STATUS = "art:status"; |
81 private MapfileGenerator() { | 108 private MapfileGenerator() { |
82 lock = new boolean[1]; | 109 lock = new boolean[1]; |
83 } | 110 } |
84 | 111 |
85 | 112 |
113 /** | |
114 * A main method which can be used to create a mapfile without a running | |
115 * artifact server.<br> | |
116 * <b>NOTE:</b> This method is not implemented yet! | |
117 * | |
118 * @param args Arguments. | |
119 */ | |
86 public static void main(String[] args) { | 120 public static void main(String[] args) { |
87 // TODO IMPLEMENT ME | 121 // TODO IMPLEMENT ME |
88 } | 122 } |
89 | 123 |
90 | 124 |
125 /** | |
126 * Returns the instance of this generator. | |
127 * | |
128 * @return the instance. | |
129 */ | |
91 public static synchronized MapfileGenerator getInstance() { | 130 public static synchronized MapfileGenerator getInstance() { |
92 if (instance == null) { | 131 if (instance == null) { |
93 instance = new MapfileGenerator(); | 132 instance = new MapfileGenerator(); |
94 instance.setDaemon(true); | 133 instance.setDaemon(true); |
95 instance.start(); | 134 instance.start(); |
97 | 136 |
98 return instance; | 137 return instance; |
99 } | 138 } |
100 | 139 |
101 | 140 |
141 /** | |
142 * Triggers the mapfile generation process. | |
143 */ | |
102 public void update() { | 144 public void update() { |
103 synchronized (lock) { | 145 synchronized (lock) { |
104 logger.debug("update"); | 146 logger.debug("update"); |
105 lock[0] = true; | 147 lock[0] = true; |
106 lock.notify(); | 148 lock.notify(); |
107 } | 149 } |
108 } | 150 } |
109 | 151 |
110 | 152 |
153 /** | |
154 * Thread to generate a mapfile. | |
155 */ | |
156 @Override | |
111 public void run() { | 157 public void run() { |
112 logger.debug("Start MapfileGenerator thread..."); | 158 logger.debug("Start MapfileGenerator thread..."); |
113 try { | 159 try { |
114 for (;;) { | 160 for (;;) { |
115 synchronized (lock) { | 161 synchronized (lock) { |
129 finally { | 175 finally { |
130 logger.debug("THREAD END"); | 176 logger.debug("THREAD END"); |
131 } | 177 } |
132 } | 178 } |
133 | 179 |
180 /** | |
181 * Method to check the existance of a template file. | |
182 * | |
183 * @param templateID The name of a template. | |
184 * @return true, of the template exists - otherwise false. | |
185 */ | |
134 public boolean templateExists(String templateID){ | 186 public boolean templateExists(String templateID){ |
135 Template template = getTemplateByName(templateID); | 187 Template template = getTemplateByName(templateID); |
136 return template != null; | 188 return template != null; |
137 } | 189 } |
138 | 190 |
139 | 191 |
192 /** | |
193 * Method which starts searching for meta information file and mapfile | |
194 * generation. | |
195 */ | |
140 protected void generate() { | 196 protected void generate() { |
141 File basedir = new File(getShapefileBaseDir()); | 197 File basedir = new File(getShapefileBaseDir()); |
142 List layers = new ArrayList(); | 198 List layers = new ArrayList(); |
143 searchMetaInformation(basedir, layers); | 199 searchMetaInformation(basedir, layers); |
144 writeMapfile(layers); | 200 writeMapfile(layers); |
145 } | 201 } |
146 | 202 |
147 | 203 |
204 /** | |
205 * Returns the VelocityEngine used for the template mechanism. | |
206 * | |
207 * @return the velocity engine. | |
208 */ | |
148 protected VelocityEngine getVelocityEngine() { | 209 protected VelocityEngine getVelocityEngine() { |
149 if (velocityEngine == null) { | 210 if (velocityEngine == null) { |
150 velocityEngine = new VelocityEngine(); | 211 velocityEngine = new VelocityEngine(); |
151 try { | 212 try { |
152 setupVelocity(velocityEngine); | 213 setupVelocity(velocityEngine); |
158 } | 219 } |
159 return velocityEngine; | 220 return velocityEngine; |
160 } | 221 } |
161 | 222 |
162 | 223 |
224 /** | |
225 * Initialize velocity. | |
226 * | |
227 * @param engine Velocity engine. | |
228 * @throws Exception if an error occured while initializing velocity. | |
229 */ | |
163 protected void setupVelocity(VelocityEngine engine) | 230 protected void setupVelocity(VelocityEngine engine) |
164 throws Exception | 231 throws Exception |
165 { | 232 { |
166 engine.setProperty( | 233 engine.setProperty( |
167 "input.encoding", | 234 "input.encoding", |
181 | 248 |
182 engine.init(); | 249 engine.init(); |
183 } | 250 } |
184 | 251 |
185 | 252 |
253 /** | |
254 * Returns the path specifying the logfile for velocity. | |
255 * | |
256 * @return Velocity logfile path. | |
257 */ | |
186 protected String getVelocityLogfile() { | 258 protected String getVelocityLogfile() { |
187 if (velocityLogfile == null) | 259 if (velocityLogfile == null) |
188 velocityLogfile = Config.getStringXPath(VELOCITY_LOGFILE); | 260 velocityLogfile = Config.getStringXPath(VELOCITY_LOGFILE); |
189 | 261 |
190 return velocityLogfile; | 262 return velocityLogfile; |
191 } | 263 } |
192 | 264 |
193 | 265 |
266 /** | |
267 * Returns the base directory storing the templates. | |
268 * | |
269 * @return the template base directory. | |
270 */ | |
194 protected String getTemplateBaseDir() { | 271 protected String getTemplateBaseDir() { |
195 if (templatePath == null) { | 272 if (templatePath == null) { |
196 templatePath = Config.getStringXPath(TEMPLATE_PATH); | 273 templatePath = Config.getStringXPath(TEMPLATE_PATH); |
197 templatePath = Config.replaceConfigDir(templatePath); | 274 templatePath = Config.replaceConfigDir(templatePath); |
198 } | 275 } |
199 | 276 |
200 return templatePath; | 277 return templatePath; |
201 } | 278 } |
202 | 279 |
203 | 280 |
281 /** | |
282 * Returns a template specified by <i>model</i>. | |
283 * | |
284 * @param model The name of the template. | |
285 * @return a template. | |
286 */ | |
204 protected Template getTemplateByName(String model) { | 287 protected Template getTemplateByName(String model) { |
205 if (model.indexOf(".vm") < 0) { | 288 if (model.indexOf(".vm") < 0) { |
206 model = model.concat(".vm"); | 289 model = model.concat(".vm"); |
207 } | 290 } |
208 | 291 |
221 | 304 |
222 return null; | 305 return null; |
223 } | 306 } |
224 | 307 |
225 | 308 |
309 /** | |
310 * Returns the mapfile template. | |
311 * | |
312 * @return the mapfile template. | |
313 * @throws Exception if an error occured while reading the configuration. | |
314 */ | |
226 protected Template getMapfileTemplate() | 315 protected Template getMapfileTemplate() |
227 throws Exception | 316 throws Exception |
228 { | 317 { |
229 String mapfileName = Config.getStringXPath(TEMPLATE_MAPFILE); | 318 String mapfileName = Config.getStringXPath(TEMPLATE_MAPFILE); |
230 return getTemplateByName(mapfileName); | 319 return getTemplateByName(mapfileName); |
231 } | 320 } |
232 | 321 |
233 | 322 |
323 /** | |
324 * Returns the base directory storing the shapefiles. | |
325 * | |
326 * @return the shapefile base directory. | |
327 */ | |
234 protected String getShapefileBaseDir() { | 328 protected String getShapefileBaseDir() { |
235 if (shapefileDirectory == null) { | 329 if (shapefileDirectory == null) { |
236 shapefileDirectory = Config.getStringXPath(SHAPEFILE_BASE_DIR); | 330 shapefileDirectory = Config.getStringXPath(SHAPEFILE_BASE_DIR); |
237 shapefileDirectory = Config.replaceConfigDir(shapefileDirectory); | 331 shapefileDirectory = Config.replaceConfigDir(shapefileDirectory); |
238 } | 332 } |
239 | 333 |
240 return shapefileDirectory; | 334 return shapefileDirectory; |
241 } | 335 } |
242 | 336 |
243 | 337 |
338 /** | |
339 * Returns the mapfile. | |
340 * | |
341 * @return the mapfile. | |
342 */ | |
244 protected File getMapfile() { | 343 protected File getMapfile() { |
245 if (mapfile == null) { | 344 if (mapfile == null) { |
246 String tmp = Config.getStringXPath(MAPFILE_PATH); | 345 String tmp = Config.getStringXPath(MAPFILE_PATH); |
247 tmp = Config.replaceConfigDir(tmp); | 346 tmp = Config.replaceConfigDir(tmp); |
248 mapfile = new File(tmp); | 347 mapfile = new File(tmp); |
250 | 349 |
251 return mapfile; | 350 return mapfile; |
252 } | 351 } |
253 | 352 |
254 | 353 |
354 /** | |
355 * Search for meta information file in <i>file</i> and store the layer | |
356 * information in <i>store</i> if a meta file was found. | |
357 * | |
358 * @param file The root directory to be searched for meta files. | |
359 * @param store A list storing <code>LayerInfo</code> objects. | |
360 */ | |
255 protected void searchMetaInformation(File file, List store) { | 361 protected void searchMetaInformation(File file, List store) { |
256 if (file.isDirectory()) { | 362 if (file.isDirectory()) { |
257 File[] files = file.listFiles(); | 363 File[] files = file.listFiles(); |
258 | 364 |
259 if (files != null && files.length != 0) { | 365 if (files != null && files.length != 0) { |
274 } | 380 } |
275 } | 381 } |
276 } | 382 } |
277 | 383 |
278 | 384 |
385 /** | |
386 * Parses a meta information file and returns necessary information as | |
387 * <code>LayerInfo</code> array. | |
388 * | |
389 * @param file Meta information file. | |
390 * @return Array of LayerInfo objects. | |
391 */ | |
279 protected LayerInfo[] parseMeta(File file) { | 392 protected LayerInfo[] parseMeta(File file) { |
280 Document meta = XMLUtils.parseDocument(file); | 393 Document meta = XMLUtils.parseDocument(file); |
281 List layers = new ArrayList(); | 394 List layers = new ArrayList(); |
282 | 395 |
283 NodeList layerset = (NodeList) XMLUtils.xpath( | 396 NodeList layerset = (NodeList) XMLUtils.xpath( |
300 | 413 |
301 return (LayerInfo[]) layers.toArray(new LayerInfo[layers.size()]); | 414 return (LayerInfo[]) layers.toArray(new LayerInfo[layers.size()]); |
302 } | 415 } |
303 | 416 |
304 | 417 |
418 /** | |
419 * Parses a node storing layer information and returns them. | |
420 * | |
421 * @param layer Node storing information about a layer. | |
422 * @return a LayerInfo object. | |
423 */ | |
305 protected LayerInfo parseLayer(Node layer) { | 424 protected LayerInfo parseLayer(Node layer) { |
306 LayerInfo info = new LayerInfo(); | 425 LayerInfo info = new LayerInfo(); |
307 | 426 |
308 String name = parseLayerAttr(layer, XPATH_LAYER_NAME); | 427 String name = parseLayerAttr(layer, XPATH_LAYER_NAME); |
309 if (name != null && !name.equals("")) { | 428 if (name != null && !name.equals("")) { |
340 | 459 |
341 return info; | 460 return info; |
342 } | 461 } |
343 | 462 |
344 | 463 |
464 /** | |
465 * Parses attributes in layer nodes. | |
466 * | |
467 * @param node A node containing layer information. | |
468 * @param xpath XPath specifying an attribute. | |
469 * @return Attribute as string. | |
470 */ | |
345 protected String parseLayerAttr(Node node, String xpath) { | 471 protected String parseLayerAttr(Node node, String xpath) { |
346 return (String) XMLUtils.xpath( | 472 return (String) XMLUtils.xpath( |
347 node, | 473 node, |
348 xpath, | 474 xpath, |
349 XPathConstants.STRING, | 475 XPathConstants.STRING, |
350 ArtifactNamespaceContext.INSTANCE); | 476 ArtifactNamespaceContext.INSTANCE); |
351 } | 477 } |
352 | 478 |
353 | 479 |
480 /** | |
481 * Creates a mapfile with the layer information stored in <i>layers</i>. | |
482 * | |
483 * @param layers Layer information. | |
484 */ | |
354 protected void writeMapfile(List layers) { | 485 protected void writeMapfile(List layers) { |
355 String tmpMapName = "mapfile" + new Date().getTime(); | 486 String tmpMapName = "mapfile" + new Date().getTime(); |
356 | 487 |
357 int layersize = layers.size(); | 488 int layersize = layers.size(); |
358 StringBuilder sb = new StringBuilder(); | 489 StringBuilder sb = new StringBuilder(); |