root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / writer / GPEKmlWriterHandlerImplementor.java @ 19686
History | View | Annotate | Download (14 KB)
1 |
package org.gvsig.gpe.kml.writer; |
---|---|
2 |
|
3 |
import java.io.FileWriter; |
4 |
import java.io.IOException; |
5 |
import java.io.StringWriter; |
6 |
import java.util.Stack; |
7 |
|
8 |
import org.gvsig.gpe.kml.utils.KmlTags; |
9 |
import org.gvsig.gpe.kml.writer.profiles.IWriterProfile; |
10 |
import org.gvsig.gpe.warnings.FeatureNotSupportedWarning; |
11 |
import org.gvsig.gpe.xml.exceptions.GPEXmlToZipFileException; |
12 |
import org.gvsig.gpe.xml.utils.XMLTags; |
13 |
import org.gvsig.gpe.xml.writer.GPEXmlWriterHandlerImplementor; |
14 |
import org.gvsig.gpe.xml.writer.IWriter; |
15 |
import org.gvsig.gpe.xml.writer.Writer; |
16 |
|
17 |
|
18 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
19 |
*
|
20 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
21 |
*
|
22 |
* This program is free software; you can redistribute it and/or
|
23 |
* modify it under the terms of the GNU General Public License
|
24 |
* as published by the Free Software Foundation; either version 2
|
25 |
* of the License, or (at your option) any later version.
|
26 |
*
|
27 |
* This program is distributed in the hope that it will be useful,
|
28 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
29 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
30 |
* GNU General Public License for more details.
|
31 |
*
|
32 |
* You should have received a copy of the GNU General Public License
|
33 |
* along with this program; if not, write to the Free Software
|
34 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
35 |
*
|
36 |
* For more information, contact:
|
37 |
*
|
38 |
* Generalitat Valenciana
|
39 |
* Conselleria d'Infraestructures i Transport
|
40 |
* Av. Blasco Ib??ez, 50
|
41 |
* 46010 VALENCIA
|
42 |
* SPAIN
|
43 |
*
|
44 |
* +34 963862235
|
45 |
* gvsig@gva.es
|
46 |
* www.gvsig.gva.es
|
47 |
*
|
48 |
* or
|
49 |
*
|
50 |
* IVER T.I. S.A
|
51 |
* Salamanca 50
|
52 |
* 46005 Valencia
|
53 |
* Spain
|
54 |
*
|
55 |
* +34 963163400
|
56 |
* dac@iver.es
|
57 |
*/
|
58 |
/* CVS MESSAGES:
|
59 |
*
|
60 |
* $Id: GPEKmlWriterHandlerImplementor.java 362 2008-01-10 08:41:41Z jpiera $
|
61 |
* $Log$
|
62 |
* Revision 1.1 2007/06/29 12:19:48 jorpiell
|
63 |
* The schema validation is made independently of the concrete writer
|
64 |
*
|
65 |
* Revision 1.17 2007/06/11 06:41:41 jorpiell
|
66 |
* Add a new Exception
|
67 |
*
|
68 |
* Revision 1.16 2007/06/07 14:53:59 jorpiell
|
69 |
* Add the schema support
|
70 |
*
|
71 |
* Revision 1.15 2007/05/16 15:54:20 jorpiell
|
72 |
* Add elements support
|
73 |
*
|
74 |
* Revision 1.14 2007/05/16 12:08:14 jorpiell
|
75 |
* A multipoint layer is not supported
|
76 |
*
|
77 |
* Revision 1.13 2007/05/16 09:30:09 jorpiell
|
78 |
* the writting methods has to have the errorHandler argument
|
79 |
*
|
80 |
* Revision 1.12 2007/05/15 12:37:45 jorpiell
|
81 |
* Add multyGeometries
|
82 |
*
|
83 |
* Revision 1.11 2007/05/09 08:36:24 jorpiell
|
84 |
* Add the bbox to the layer
|
85 |
*
|
86 |
* Revision 1.10 2007/05/08 09:28:17 jorpiell
|
87 |
* Add comments to create javadocs
|
88 |
*
|
89 |
* Revision 1.9 2007/05/08 08:22:37 jorpiell
|
90 |
* Add comments to create javadocs
|
91 |
*
|
92 |
* Revision 1.8 2007/05/07 07:07:18 jorpiell
|
93 |
* Add a constructor with the name and the description fields
|
94 |
*
|
95 |
* Revision 1.7 2007/05/02 11:46:50 jorpiell
|
96 |
* Writing tests updated
|
97 |
*
|
98 |
* Revision 1.6 2007/04/20 08:38:59 jorpiell
|
99 |
* Tests updating
|
100 |
*
|
101 |
* Revision 1.5 2007/04/17 10:30:41 jorpiell
|
102 |
* Add a method to compress a file
|
103 |
*
|
104 |
* Revision 1.4 2007/04/14 16:08:07 jorpiell
|
105 |
* Kml writing support added
|
106 |
*
|
107 |
* Revision 1.3 2007/04/13 13:16:21 jorpiell
|
108 |
* Add KML reading support
|
109 |
*
|
110 |
* Revision 1.2 2007/04/12 17:06:43 jorpiell
|
111 |
* First GML writing tests
|
112 |
*
|
113 |
* Revision 1.1 2007/04/12 10:21:52 jorpiell
|
114 |
* Add the writers
|
115 |
*
|
116 |
*
|
117 |
*/
|
118 |
/**
|
119 |
* KML writer handler used to write KML/KMZ files
|
120 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
121 |
*/
|
122 |
public abstract class GPEKmlWriterHandlerImplementor extends GPEXmlWriterHandlerImplementor{ |
123 |
private int layerLevel = 0; |
124 |
private Stack currentElementStream = null; |
125 |
private Stack currentElementName = null; |
126 |
private IWriterProfile profile = null; |
127 |
|
128 |
public GPEKmlWriterHandlerImplementor() {
|
129 |
super();
|
130 |
currentElementStream = new Stack(); |
131 |
currentElementName = new Stack(); |
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
* @see org.gvsig.gpe.xml.writer.GPEXmlWriterHandler#createOutputStream()
|
137 |
*/
|
138 |
protected IWriter createWriter() throws IOException { |
139 |
return new Writer(new FileWriter(getOutputFile())); |
140 |
} |
141 |
|
142 |
/*
|
143 |
* (non-Javadoc)
|
144 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#getDefaultFormat()
|
145 |
*/
|
146 |
public String getDefaultFormat() { |
147 |
return "KML"; |
148 |
} |
149 |
|
150 |
/*
|
151 |
* (non-Javadoc)
|
152 |
* @see org.gvsig.gpe.writers.IGPEWriterHandlerImplementor#getFormats()
|
153 |
*/
|
154 |
public String[] getFormats() { |
155 |
String[] formats = new String[2]; |
156 |
formats[0] = "KML"; |
157 |
formats[1] = "KMZ"; |
158 |
return formats;
|
159 |
} |
160 |
|
161 |
/*
|
162 |
* (non-Javadoc)
|
163 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#initialize()
|
164 |
*/
|
165 |
public void initialize(){ |
166 |
try {
|
167 |
initXmlHeader(); |
168 |
writer.write("\n");
|
169 |
writer.write("<" + KmlTags.ROOT);
|
170 |
writer.write(" " + XMLTags.XML_NAMESPACE + "=\"" + KmlTags.NAMESPACE_21 + "\""); |
171 |
writer.write(">");
|
172 |
} catch (IOException e) { |
173 |
getErrorHandler().addError(e); |
174 |
} |
175 |
} |
176 |
|
177 |
/*
|
178 |
* (non-Javadoc)
|
179 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#close()
|
180 |
*/
|
181 |
public void close(){ |
182 |
try {
|
183 |
writer.write("\n");
|
184 |
writer.write("</" + KmlTags.ROOT + ">"); |
185 |
writer.flush(); |
186 |
writer.close(); |
187 |
} catch (IOException e) { |
188 |
getErrorHandler().addError(e); |
189 |
} |
190 |
if (getFormat().toUpperCase().equals("KMZ")){ |
191 |
try {
|
192 |
compressFile(); |
193 |
} catch (GPEXmlToZipFileException e) {
|
194 |
getErrorHandler().addError(e); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
/*
|
200 |
* (non-Javadoc)
|
201 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#startLayer(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
202 |
*/
|
203 |
public void startLayer(String id, String name, String description, String srs, String xsElementName) { |
204 |
try{
|
205 |
if (layerLevel == 0){ |
206 |
getProfile().getDocumentWriter().start(writer, this, id, name, description);
|
207 |
}else{
|
208 |
getProfile().getFolderWriter().start(writer, this, id, name, description);
|
209 |
} |
210 |
} catch (IOException e) { |
211 |
getErrorHandler().addError(e); |
212 |
} |
213 |
layerLevel++; |
214 |
} |
215 |
|
216 |
/*
|
217 |
* (non-Javadoc)
|
218 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endLayer()
|
219 |
*/
|
220 |
public void endLayer() { |
221 |
layerLevel--; |
222 |
try{
|
223 |
if (layerLevel == 0){ |
224 |
getProfile().getDocumentWriter().end(writer, this);
|
225 |
}else{
|
226 |
getProfile().getFolderWriter().end(writer, this);
|
227 |
} |
228 |
} catch (IOException e) { |
229 |
getErrorHandler().addError(e); |
230 |
} |
231 |
} |
232 |
|
233 |
/*
|
234 |
* (non-Javadoc)
|
235 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startFeature(java.lang.String, java.lang.String)
|
236 |
*/
|
237 |
public void startFeature(String id, String name, String xsElementName) { |
238 |
currentElementStream.push(new Writer(new StringWriter())); |
239 |
currentElementName.push(new Stack()); |
240 |
try{
|
241 |
getProfile().getPlaceMarkWriter().start(writer, this, id, name);
|
242 |
} catch (IOException e) { |
243 |
getErrorHandler().addError(e); |
244 |
} |
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endFeature()
|
250 |
*/
|
251 |
public void endFeature() { |
252 |
IWriter elementWriter = (IWriter)currentElementStream.pop(); |
253 |
currentElementName.pop(); |
254 |
try{
|
255 |
getProfile().getMetadataWriter().start(writer, this);
|
256 |
writer.write(elementWriter.toString()); |
257 |
getProfile().getMetadataWriter().end(writer, this);
|
258 |
getProfile().getPlaceMarkWriter().end(writer, this);
|
259 |
} catch (IOException e) { |
260 |
getErrorHandler().addError(e); |
261 |
} |
262 |
} |
263 |
|
264 |
/*
|
265 |
* (non-Javadoc)
|
266 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#startElement(java.lang.String, java.lang.Object, java.lang.Object)
|
267 |
*/
|
268 |
public void startElement(String name, Object value, String type) { |
269 |
IWriter buffer = ((IWriter)currentElementStream.lastElement()); |
270 |
((Stack)currentElementName.lastElement()).push(name);
|
271 |
try {
|
272 |
getProfile().getElementWriter().start(buffer, this, name, value, type);
|
273 |
} catch (IOException e) { |
274 |
getErrorHandler().addError(e); |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
/*
|
280 |
* (non-Javadoc)
|
281 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#endElement()
|
282 |
*/
|
283 |
public void endElement() { |
284 |
IWriter buffer = ((IWriter)currentElementStream.lastElement()); |
285 |
String elementName = (String)((Stack)currentElementName.lastElement()).pop(); |
286 |
try {
|
287 |
getProfile().getElementWriter().end(buffer, this, elementName);
|
288 |
} catch (IOException e) { |
289 |
getErrorHandler().addError(e); |
290 |
} |
291 |
} |
292 |
|
293 |
/*
|
294 |
* (non-Javadoc)
|
295 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#startPoint(java.lang.String, double, double, double, java.lang.String)
|
296 |
*/
|
297 |
public void startPoint(String id, double x, double y, double z, String srs) { |
298 |
try {
|
299 |
getProfile().getPointTypeWriter().start(writer, this, id, x, y, z);
|
300 |
} catch (IOException e) { |
301 |
getErrorHandler().addError(e); |
302 |
} |
303 |
} |
304 |
|
305 |
/*
|
306 |
* (non-Javadoc)
|
307 |
* @see org.gvsig.gpe.writer.GPEWriterHandler#endPoint()
|
308 |
*/
|
309 |
public void endPoint() { |
310 |
try {
|
311 |
getProfile().getPointTypeWriter().end(writer, this);
|
312 |
} catch (IOException e) { |
313 |
getErrorHandler().addError(e); |
314 |
} |
315 |
} |
316 |
|
317 |
/*
|
318 |
* (non-Javadoc)
|
319 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startLineString(java.lang.String, double[], double[], double[], java.lang.String)
|
320 |
*/
|
321 |
public void startLineString(String id, double[] x, double[] y, double[] z, String srs) { |
322 |
try {
|
323 |
getProfile().getLineStringTypeWriter().start(writer, this, id, x, y, z);
|
324 |
} catch (IOException e) { |
325 |
getErrorHandler().addError(e); |
326 |
} |
327 |
} |
328 |
|
329 |
/*
|
330 |
* (non-Javadoc)
|
331 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endLineString()
|
332 |
*/
|
333 |
public void endLineString() { |
334 |
try {
|
335 |
getProfile().getLineStringTypeWriter().end(writer, this);
|
336 |
} catch (IOException e) { |
337 |
getErrorHandler().addError(e); |
338 |
} |
339 |
} |
340 |
|
341 |
/*
|
342 |
* (non-Javadoc)
|
343 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startLinearRing(java.lang.String, double[], double[], double[], java.lang.String)
|
344 |
*/
|
345 |
public void startLinearRing(String id, double[] x, double[] y, double[] z, String srs) { |
346 |
try {
|
347 |
getProfile().getLinearRingWriter().start(writer, this, x, y, z);
|
348 |
} catch (IOException e) { |
349 |
getErrorHandler().addError(e); |
350 |
} |
351 |
} |
352 |
|
353 |
/*
|
354 |
* (non-Javadoc)
|
355 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endLinearRing()
|
356 |
*/
|
357 |
public void endLinearRing() { |
358 |
try {
|
359 |
getProfile().getLinearRingWriter().end(writer, this);
|
360 |
} catch (IOException e) { |
361 |
getErrorHandler().addError(e); |
362 |
} |
363 |
} |
364 |
|
365 |
/*
|
366 |
* (non-Javadoc)
|
367 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startPolygon(java.lang.String, double[], double[], double[], java.lang.String)
|
368 |
*/
|
369 |
public void startPolygon(String id, double[] x, double[] y, double[] z, String srs) { |
370 |
try {
|
371 |
getProfile().getPolygonTypeWriter().start(writer, this, id, x, y, z);
|
372 |
} catch (IOException e) { |
373 |
getErrorHandler().addError(e); |
374 |
} |
375 |
} |
376 |
|
377 |
/*
|
378 |
* (non-Javadoc)
|
379 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endPolygon()
|
380 |
*/
|
381 |
public void endPolygon() { |
382 |
try {
|
383 |
getProfile().getPolygonTypeWriter().end(writer, this);
|
384 |
} catch (IOException e) { |
385 |
getErrorHandler().addError(e); |
386 |
} |
387 |
} |
388 |
|
389 |
/*
|
390 |
* (non-Javadoc)
|
391 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startInnerBoundary(java.lang.String, double[], double[], double[], java.lang.String)
|
392 |
*/
|
393 |
public void startInnerBoundary(String id, double[] x, double[] y, double[] z, String srs) { |
394 |
try {
|
395 |
getProfile().getInnerBoundaryIsWriter().start(writer, this, x, y, z);
|
396 |
} catch (IOException e) { |
397 |
getErrorHandler().addError(e); |
398 |
} |
399 |
} |
400 |
|
401 |
/*
|
402 |
* (non-Javadoc)
|
403 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endInnerBoundary()
|
404 |
*/
|
405 |
public void endInnerBoundary() { |
406 |
try {
|
407 |
getProfile().getInnerBoundaryIsWriter().end(writer, this);
|
408 |
} catch (IOException e) { |
409 |
getErrorHandler().addError(e); |
410 |
} |
411 |
} |
412 |
|
413 |
/*
|
414 |
* (non-Javadoc)
|
415 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startBbox(java.lang.String, double[], double[], double[], java.lang.String)
|
416 |
*/
|
417 |
public void startBbox(String id, double[] x, double[] y, double[] z, String srs) { |
418 |
try {
|
419 |
getProfile().getRegionWriter().start(writer, this, y[1], y[0], x[1], x[0], z[0], z[1]); |
420 |
} catch (IOException e) { |
421 |
getErrorHandler().addError(e); |
422 |
} |
423 |
} |
424 |
|
425 |
/*
|
426 |
* (non-Javadoc)
|
427 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endBbox()
|
428 |
*/
|
429 |
public void endBbox() { |
430 |
try {
|
431 |
getProfile().getRegionWriter().end(writer, this);
|
432 |
} catch (IOException e) { |
433 |
getErrorHandler().addError(e); |
434 |
} |
435 |
} |
436 |
|
437 |
/*
|
438 |
* (non-Javadoc)
|
439 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
|
440 |
*/
|
441 |
public void startMultiGeometry(String id, String srs) { |
442 |
try {
|
443 |
getProfile().getMultiGeometryWriter().start(writer, this, id);
|
444 |
} catch (IOException e) { |
445 |
getErrorHandler().addError(e); |
446 |
} |
447 |
} |
448 |
|
449 |
/*
|
450 |
* (non-Javadoc)
|
451 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
|
452 |
*/
|
453 |
public void endMultiGeometry() { |
454 |
try {
|
455 |
getProfile().getMultiGeometryWriter().end(writer, this);
|
456 |
} catch (IOException e) { |
457 |
getErrorHandler().addError(e); |
458 |
} |
459 |
} |
460 |
|
461 |
/*
|
462 |
* (non-Javadoc)
|
463 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
|
464 |
*/
|
465 |
public void startMultiPoint(String id, String srs) { |
466 |
getErrorHandler().addWarning( |
467 |
new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOINTCREATION,getName()));
|
468 |
startMultiGeometry(id, srs); |
469 |
} |
470 |
|
471 |
/*
|
472 |
* (non-Javadoc)
|
473 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
|
474 |
*/
|
475 |
public void endMultiPoint() { |
476 |
super.endMultiPoint();
|
477 |
endMultiGeometry(); |
478 |
} |
479 |
|
480 |
/*
|
481 |
* (non-Javadoc)
|
482 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
|
483 |
*/
|
484 |
public void startMultiLineString(String id, String srs) { |
485 |
getErrorHandler().addWarning( |
486 |
new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTILINESTRINGCREATION,getName()));
|
487 |
startMultiGeometry(id, srs); |
488 |
} |
489 |
|
490 |
/*
|
491 |
* (non-Javadoc)
|
492 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
|
493 |
*/
|
494 |
public void endMultiLineString() { |
495 |
endMultiGeometry(); |
496 |
} |
497 |
|
498 |
/*
|
499 |
* (non-Javadoc)
|
500 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#startMultiPoint(java.lang.String, java.lang.String)
|
501 |
*/
|
502 |
public void startMultiPolygon(String id, String srs) { |
503 |
getErrorHandler().addWarning( |
504 |
new FeatureNotSupportedWarning(FeatureNotSupportedWarning.MULTIPOLYGONCREATION,getName()));
|
505 |
startMultiGeometry(id, srs); |
506 |
} |
507 |
|
508 |
/*
|
509 |
* (non-Javadoc)
|
510 |
* @see org.gvsig.gpe.writers.GPEWriterHandler#endMuliPoint()
|
511 |
*/
|
512 |
public void endMultiPolygon() { |
513 |
endMultiGeometry(); |
514 |
} |
515 |
|
516 |
/**
|
517 |
* @return the profile
|
518 |
*/
|
519 |
public IWriterProfile getProfile() {
|
520 |
return profile;
|
521 |
} |
522 |
|
523 |
/**
|
524 |
* @param profile the profile to set
|
525 |
*/
|
526 |
public void setProfile(IWriterProfile profile) { |
527 |
this.profile = profile;
|
528 |
} |
529 |
} |