MATSIM
PopulationWriterHandlerImplV4.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansWriterHandlerImplV4.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 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.population.io;
22 
24 
25 import java.io.BufferedWriter;
26 import java.io.IOException;
27 
28 import org.matsim.api.core.v01.Coord;
44 import org.matsim.core.utils.misc.Time;
45 
46 /*package*/ class PopulationWriterHandlerImplV4 extends AbstractPopulationWriterHandler {
47 
48  private final CoordinateTransformation coordinateTransformation;
49  private final Network network;
50 
51  public PopulationWriterHandlerImplV4(
52  final Network network) {
53  this( new IdentityTransformation() , network );
54  }
55 
56  public PopulationWriterHandlerImplV4(
57  final CoordinateTransformation coordinateTransformation,
58  final Network network) {
59  this.coordinateTransformation = coordinateTransformation;
60  this.network = network;
61  }
62 
63  @Override
64  public void writeHeaderAndStartElement(final BufferedWriter out) throws IOException {
65  out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
66  out.write("<!DOCTYPE plans SYSTEM \"" + MatsimXmlWriter.DEFAULT_DTD_LOCATION + "plans_v4.dtd\">\n\n");
67  }
68 
70  // <plans ... > ... </plans>
72 
73  @Override
74  public void startPlans(final Population plans, final BufferedWriter out) throws IOException {
75  out.write("<plans");
76  if (plans.getName() != null) {
77  out.write(" name=\"" + encodeAttributeValue(plans.getName()) + "\"");
78  }
79  out.write(">\n\n");
80  }
81 
82  @Override
83  public void endPlans(final BufferedWriter out) throws IOException {
84  out.write("</plans>\n");
85  out.flush();
86  }
87 
89  // <person ... > ... </person>
91 
92  @Override
93  public void startPerson(final Person person, final BufferedWriter out) throws IOException {
94  out.write("\t<person id=\"");
95  out.write(encodeAttributeValue(person.getId().toString()));
96  out.write("\"");
97  if (PersonUtils.getSex(person) != null) {
98  out.write(" sex=\"");
99  out.write(PersonUtils.getSex(person));
100  out.write("\"");
101  }
102  if (PersonUtils.getAge(person) != null) {
103  out.write(" age=\"");
104  out.write(Integer.toString(PersonUtils.getAge(person)));
105  out.write("\"");
106  }
107  if (PersonUtils.getLicense(person) != null) {
108  out.write(" license=\"");
109  out.write(PersonUtils.getLicense(person));
110  out.write("\"");
111  }
112  if (PersonUtils.getCarAvail(person) != null) {
113  out.write(" car_avail=\"");
114  out.write(PersonUtils.getCarAvail(person));
115  out.write("\"");
116  }
117  if (PersonUtils.isEmployed(person) != null) {
118  out.write(" employed=\"");
119  out.write((PersonUtils.isEmployed(person) ? "yes" : "no"));
120  out.write("\"");
121  }
122  out.write(">\n");
123  }
124 
125  @Override
126  public void endPerson(final BufferedWriter out) throws IOException {
127  out.write("\t</person>\n\n");
128  }
129 
131  // <travelcard ... />
133 
134  @Override
135  public void startTravelCard(final String travelcard, final BufferedWriter out) throws IOException {
136  out.write("\t\t<travelcard type=\"");
137  out.write(travelcard);
138  out.write("\" />\n\n");
139  }
140 
141  @Override
142  public void endTravelCard(final BufferedWriter out) throws IOException {
143  }
144 
146  // <plan ... > ... </plan>
148 
149  @Override
150  public void startPlan(final Plan plan, final BufferedWriter out) throws IOException {
151  out.write("\t\t<plan");
152  if (plan.getScore() != null) {
153  out.write(" score=\"");
154  out.write(plan.getScore().toString());
155  out.write("\"");
156  }
157  if (PersonUtils.isSelected(plan))
158  out.write(" selected=\"yes\"");
159  else
160  out.write(" selected=\"no\"");
161  if ((plan.getType() != null)) {
162  out.write(" type=\"");
163  out.write(encodeAttributeValue(plan.getType()));
164  out.write("\"");
165  }
166  out.write(">\n");
167  }
168 
169  @Override
170  public void endPlan(final BufferedWriter out) throws IOException {
171  out.write("\t\t</plan>\n\n");
172  }
173 
175  // <act ... > ... </act>
177 
178  @Override
179  public void startAct(final Activity act, final BufferedWriter out) throws IOException {
180  out.write("\t\t\t<act type=\"");
181  out.write(encodeAttributeValue(act.getType()));
182  out.write("\"");
183  if (act.getLinkId() != null) {
184  out.write(" link=\"");
185  out.write(encodeAttributeValue(act.getLinkId().toString()));
186  out.write("\"");
187  }
188  if (act.getFacilityId() != null) {
189  out.write(" facility=\"");
190  out.write(encodeAttributeValue(act.getFacilityId().toString()));
191  out.write("\"");
192  }
193  if (act.getCoord() != null) {
194  final Coord coord = coordinateTransformation.transform( act.getCoord() );
195  out.write(" x=\"");
196  out.write(Double.toString( coord.getX() ));
197  out.write("\" y=\"");
198  out.write(Double.toString( coord.getY() ));
199  out.write("\"");
200  }
201  if (act.getStartTime().isDefined()) {
202  out.write(" start_time=\"");
203  out.write(Time.writeTime(act.getStartTime().seconds()));
204  out.write("\"");
205  }
206  if (act.getMaximumDuration().isDefined()) {
207  out.write(" dur=\"");
208  out.write(Time.writeTime(act.getMaximumDuration().seconds()));
209  out.write("\"");
210  }
211  if (act.getEndTime().isDefined()) {
212  out.write(" end_time=\"");
213  out.write(Time.writeTime(act.getEndTime().seconds()));
214  out.write("\"");
215  }
216  out.write(" />\n");
217  }
218 
219  @Override
220  public void endAct(final BufferedWriter out) throws IOException {
221  }
222 
224  // <leg ... > ... </leg>
226 
227  @Override
228  public void startLeg(final Leg leg, final BufferedWriter out) throws IOException {
229  out.write("\t\t\t<leg mode=\"");
230  out.write(leg.getMode());
231  out.write("\"");
232  if (leg.getDepartureTime().isDefined()) {
233  out.write(" dep_time=\"");
234  out.write(Time.writeTime(leg.getDepartureTime().seconds()));
235  out.write("\"");
236  }
237  if (leg.getTravelTime().isDefined()) {
238  out.write(" trav_time=\"");
239  out.write(Time.writeTime(leg.getTravelTime().seconds()));
240  out.write("\"");
241  }
242 // if (leg instanceof LegImpl) {
243 // LegImpl l = (LegImpl)leg;
244 // if (l.getDepartureTime() + l.getTravelTime() != Time.getUndefinedTime()) {
245 // out.write(" arr_time=\"");
246 // out.write(Time.writeTime(l.getDepartureTime() + l.getTravelTime()));
247 // out.write("\"");
248 // }
249 // }
250  // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16
251  out.write(">\n");
252  }
253 
254  @Override
255  public void endLeg(final BufferedWriter out) throws IOException {
256  out.write("\t\t\t</leg>\n");
257  }
258 
260  // <route ... > ... </route>
262 
263  @Override
264  public void startRoute(final Route route, final BufferedWriter out) throws IOException {
265  out.write("\t\t\t\t<route");
266  if (!Double.isNaN(route.getDistance())) {
267  out.write(" dist=\"");
268  out.write(Double.toString(route.getDistance()));
269  out.write("\"");
270  }
271  if (route.getTravelTime().isDefined()) {
272  out.write(" trav_time=\"");
273  out.write(Time.writeTime(route.getTravelTime().seconds()));
274  out.write("\"");
275  }
276  out.write(">\n");
277 
278  out.write("\t\t\t\t\t");
279  if (route instanceof NetworkRoute) {
280  for (Node n : RouteUtils.getNodes((NetworkRoute) route, this.network)) {
281  out.write(n.getId().toString());
282  out.write(" ");
283  }
284  } else {
285  String rd = route.getRouteDescription();
286  if (rd != null) {
287  out.write(XmlUtils.encodeContent(rd));
288  out.write(" "); // this is at the moment only to maintain binary compatibility
289  }
290  }
291 
292  out.write("\n");
293  }
294 
295  @Override
296  public void endRoute(final BufferedWriter out) throws IOException {
297  out.write("\t\t\t\t</route>\n");
298  }
299 
301  // <!-- ============ ... ========== -->
303 
304  @Override
305  public void writeSeparator(final BufferedWriter out) throws IOException {
306  out.write("<!-- ====================================================================== -->\n\n");
307  }
308 
309 }
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41