21 package org.matsim.vis.kml;
23 import java.io.BufferedWriter;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStreamWriter;
29 import java.util.HashMap;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipOutputStream;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Marshaller;
38 import org.apache.log4j.Level;
39 import org.apache.log4j.Logger;
42 import net.opengis.kml.v_2_2_0.KmlType;
43 import net.opengis.kml.v_2_2_0.LinkType;
44 import net.opengis.kml.v_2_2_0.NetworkLinkType;
45 import net.opengis.kml.v_2_2_0.ObjectFactory;
57 private static final Logger
log = Logger.getLogger(
KMZWriter.class);
59 private BufferedWriter
out = null;
61 private ZipOutputStream
zipOut = null;
63 private final Map<String, String>
nonKmlFiles =
new HashMap<String, String>();
71 JAXBContext jaxbContext = JAXBContext.newInstance(
"net.opengis.kml.v_2_2_0");
72 marshaller = jaxbContext.createMarshaller();
73 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
74 }
catch (JAXBException e) {
86 log.setLevel( Level.INFO ) ;
88 String filename = outFilename;
89 if (filename.endsWith(
".kml") || filename.endsWith(
".kmz")) {
90 filename = filename.substring(0, filename.length() - 4);
94 this.zipOut =
new ZipOutputStream(
new FileOutputStream(filename +
".kmz"));
95 this.out =
new BufferedWriter(
new OutputStreamWriter(this.zipOut,
"UTF8"));
96 }
catch (IOException e) {
104 KmlType docKML = kmlObjectFactory.createKmlType();
105 NetworkLinkType nl = kmlObjectFactory.createNetworkLinkType();
107 LinkType link = kmlObjectFactory.createLinkType();
108 link.setHref(
"main.kml");
110 docKML.setAbstractFeatureGroup(kmlObjectFactory.createNetworkLink(nl));
126 if (filename.equals(
"doc.kml")) {
127 throw new IllegalArgumentException(
128 "The filename 'doc.kml' is reserved for the primary kml.");
130 if (filename.equals(
"main.kml")) {
131 throw new IllegalArgumentException(
132 "The filename 'main.kml' is reserved for the main kml.");
156 }
catch (IOException e) {
168 public void addNonKMLFile(
final String filename,
final String inZipFilename)
throws IOException {
169 if (this.nonKmlFiles.containsKey(filename) && (inZipFilename.compareTo(this.nonKmlFiles.get(filename)) == 0)) {
170 log.warn(
"File: " + filename +
" is already included in the kmz as " + inZipFilename);
173 this.nonKmlFiles.put(filename, inZipFilename);
174 FileInputStream fis =
new FileInputStream(filename);
189 public void addNonKMLFile(
final InputStream data,
final String inZipFilename)
throws IOException {
192 byte[] buffer =
new byte[4096];
195 ZipEntry entry =
new ZipEntry(inZipFilename);
196 this.zipOut.putNextEntry(entry);
199 while ((bytesRead = data.read(buffer)) != -1) {
200 this.zipOut.write(buffer, 0, bytesRead);
202 log.debug(entry.getName() +
" added to kmz.");
214 public void addNonKMLFile(
final byte[] data,
final String inZipFilename)
throws IOException {
216 ZipEntry entry =
new ZipEntry(inZipFilename);
217 this.zipOut.putNextEntry(entry);
218 this.zipOut.write(data);
219 log.debug(entry.getName() +
" added to kmz.");
228 private void writeKml(
final String filename,
final KmlType kml) {
230 ZipEntry ze =
new ZipEntry(filename);
231 ze.setMethod(ZipEntry.DEFLATED);
232 this.zipOut.putNextEntry(ze);
235 marshaller.marshal(kmlObjectFactory.createKml(kml),
out);
236 }
catch (JAXBException e) {
242 }
catch (IOException e) {
final Map< String, String > nonKmlFiles
void addNonKMLFile(final InputStream data, final String inZipFilename)
void writeLinkedKml(final String filename, final KmlType kml)
void writeKml(final String filename, final KmlType kml)
void writeMainKml(final KmlType kml)
KMZWriter(final String outFilename)
static final ObjectFactory kmlObjectFactory
static final Marshaller marshaller
void addNonKMLFile(final byte[] data, final String inZipFilename)
void addNonKMLFile(final String filename, final String inZipFilename)