MATSIM
EventWriterXML.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * EventWriterXML.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.events.algorithms;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
27 import org.matsim.core.utils.io.IOUtils;
28 
29 import java.io.BufferedWriter;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.UncheckedIOException;
34 import java.nio.charset.StandardCharsets;
35 
36 public class EventWriterXML implements EventWriter, BasicEventHandler {
37 
38  private static final Logger LOG = LogManager.getLogger(EventWriterXML.class);
39  private final BufferedWriter out;
40 
44  private final ThreadLocal<StringBuilder> stringBuilder = ThreadLocal.withInitial(StringBuilder::new);
45 
46  public EventWriterXML(final String outfilename) {
47  this.out = IOUtils.getBufferedWriter(outfilename);
48  this.writeHeader();
49  }
50 
56  public EventWriterXML(final OutputStream stream ) {
57  this.out = new BufferedWriter(new OutputStreamWriter(stream, StandardCharsets.UTF_8));
58  this.writeHeader();
59  }
60 
61  private void writeHeader() {
62  try {
63  this.out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<events version=\"1.0\">\n");
64  } catch (IOException e) {
65  throw new UncheckedIOException(e);
66  }
67  }
68 
69  @Override
70  public void closeFile() {
71  try {
72  this.out.write("</events>");
73  // I added a "\n" to make it look nicer on the console. Can't say if this may have unintended side
74  // effects anywhere else. kai, oct'12
75  // fails signalsystems test (and presumably other tests in contrib/playground) since they compare
76  // checksums of event files. Removed that change again. kai, oct'12
77  this.out.close();
78  } catch (IOException e) {
79  throw new UncheckedIOException(e);
80  }
81  }
82 
83  @Override
84  public void reset(final int iter) {
85  }
86 
87  @Override
88  public void handleEvent(final Event event) {
89  try {
90  StringBuilder b = stringBuilder.get();
91 
92  b.setLength(0);
93  event.writeAsXML(b);
94  this.out.append(b);
95 
96  } catch (IOException e) {
97  LOG.error(e.getMessage(), e);
98  }
99  }
100 }
final ThreadLocal< StringBuilder > stringBuilder
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390