MATSIM
FacilitiesWriterV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2007 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.facilities;
21 
22 import org.matsim.api.core.v01.Coord;
27 import org.matsim.core.utils.misc.Time;
30 
31 import java.io.BufferedWriter;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.UncheckedIOException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.SortedSet;
39 
43 class FacilitiesWriterV1 extends MatsimXmlWriter implements MatsimWriter {
44 
45  private static final String DTD = "http://www.matsim.org/files/dtd/facilities_v1.dtd";
46 
47  private final ActivityFacilities facilities;
48 
49  private final CoordinateTransformation coordinateTransformation;
50 
51  private AttributesXmlWriterDelegate attributesWriter = new AttributesXmlWriterDelegate();
52 
53  FacilitiesWriterV1(
54  final CoordinateTransformation coordinateTransformation,
55  final ActivityFacilities facilities) {
56  this.coordinateTransformation = coordinateTransformation;
57  this.facilities = facilities;
58  }
59 
60  @Override
61  public void write(String filename) {
62  openFile(filename);
63  this.writeInit();
64  for (ActivityFacility f : FacilitiesUtils.getSortedFacilities(this.facilities).values()) {
65  this.writeFacility((ActivityFacilityImpl) f);
66  }
67  this.writeFinish();
68  }
69 
70  public void write(OutputStream stream) {
71  openOutputStream(stream);
72  this.writeInit();
73  for (ActivityFacility f : FacilitiesUtils.getSortedFacilities(this.facilities).values()) {
74  this.writeFacility((ActivityFacilityImpl) f);
75  }
76  this.writeFinish();
77  }
78 
79  private void writeInit() {
80  this.writeXmlHead();
81  this.writeDoctype("facilities", DTD);
82  this.startFacilities(this.facilities, this.writer);
83  }
84 
85  private void writeFacility(final ActivityFacilityImpl f) {
86  try {
87  this.startFacility(f);
88  for (ActivityOption a : f.getActivityOptions().values()) {
89  this.startActivity((ActivityOptionImpl) a);
90  this.writeCapacity((ActivityOptionImpl) a, this.writer);
91  SortedSet<OpeningTime> o_set = a.getOpeningTimes();
92  for (OpeningTime o : o_set) {
93  this.writeOpentime(o, this.writer);
94  }
95  this.endActivity();
96  }
97  if (!f.getAttributes().isEmpty()) {
98  this.writer.write(NL);
99  }
100  this.attributesWriter.writeAttributes("\t\t", this.writer, f.getAttributes(), false);
101  this.endFacility();
102  } catch (IOException e) {
103  throw new UncheckedIOException(e);
104  }
105  }
106 
107  private void writeFinish() {
108  try {
109  this.endFacilities();
110  this.writer.flush();
111  this.writer.close();
112  } catch (IOException e) {
113  throw new UncheckedIOException(e);
114  }
115  }
116 
118  // <facilities ... > ... </facilities>
120 
121  private void startFacilities(final ActivityFacilities facilities, final BufferedWriter out) {
122  List<Tuple<String, String>> attributes = new ArrayList<>();
123  if (facilities.getName() != null) {
124  attributes.add(new Tuple<>("name", facilities.getName()));
125  }
126  writeStartTag("facilities", attributes);
127  if (!facilities.getAttributes().isEmpty()) {
128  try {
129  this.writer.write(NL);
130  } catch (IOException e) {
131  throw new UncheckedIOException(e);
132  }
133  }
134  this.attributesWriter.writeAttributes("\t", out, facilities.getAttributes());
135  }
136 
137 
138  private void endFacilities() {
139  writeEndTag("facilities");
140  }
141 
143  // <facility ... > ... </facility>
145 
146  private void startFacility(final ActivityFacilityImpl facility) {
147  List<Tuple<String, String>> attributes = new ArrayList<>();
148  attributes.add(new Tuple<>("id", facility.getId().toString()));
149  if (facility.getLinkId() != null) {
150  attributes.add(new Tuple<>("linkId", facility.getLinkId().toString()));
151  }
152  if (facility.getCoord()!=null) {
153  final Coord coord = this.coordinateTransformation.transform(facility.getCoord());
154  attributes.add(new Tuple<>("x", Double.toString(coord.getX())));
155  attributes.add(new Tuple<>("y", Double.toString(coord.getY())));
156  }
157  if (facility.getDesc() != null) {
158  attributes.add(new Tuple<>("desc", facility.getDesc()));
159  }
160  writeStartTag("facility", attributes, false);
161  }
162 
163  private void endFacility() {
164  writeEndTag("facility");
165  }
166 
168  // <activity ... > ... </activity>
170 
171  public void startActivity(final ActivityOptionImpl activity) {
172  List<Tuple<String, String>> attributes = new ArrayList<>();
173  attributes.add(new Tuple<>("type", activity.getType()));
174  writeStartTag("activity", attributes);
175  }
176 
177  public void endActivity() {
178  writeEndTag("activity");
179  }
180 
182  // <capacity ... />
184 
185  private void writeCapacity(final ActivityOptionImpl activity, final BufferedWriter out) throws IOException {
186  if (activity.getCapacity() != Integer.MAX_VALUE) {
187  out.write("\t\t\t<capacity");
188  out.write(" value=\"" + activity.getCapacity() + "\"");
189  out.write(" />\n");
190  }
191  }
192 
194  // <opentime ... />
196 
197  private void writeOpentime(final OpeningTime opentime, final BufferedWriter out) throws IOException {
198  out.write("\t\t\t<opentime");
199  out.write(" day=\"wkday\"");
200  out.write(" start_time=\"" + Time.writeTime(opentime.getStartTime()) + "\"");
201  out.write(" end_time=\"" + Time.writeTime(opentime.getEndTime()) + "\"");
202  out.write(" />\n");
203  }
204 
205  public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
206  this.attributesWriter.putAttributeConverters(converters);
207  }
208 }
final void openOutputStream(OutputStream outputStream)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
final void writeDoctype(String rootTag, String dtdUrl)