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