MATSIM
NetworkWriterHandlerImplV2.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkWriterHandlerImplV1.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.network.io;
22 
23 import org.matsim.api.core.v01.Coord;
29 import org.matsim.core.utils.misc.Time;
32 
33 import java.io.BufferedWriter;
34 import java.io.IOException;
35 import java.util.Map;
36 import java.util.Set;
37 
39 
40 /*package*/ class NetworkWriterHandlerImplV2 implements NetworkWriterHandler {
41  private final CoordinateTransformation transformation;
42  private final AttributesXmlWriterDelegate attributesWriter = new AttributesXmlWriterDelegate();
43 
44  NetworkWriterHandlerImplV2(CoordinateTransformation transformation) {
45  this.transformation = transformation;
46  }
47 
48  public void putAttributeConverters(final Map<Class<?>, AttributeConverter<?>> converters) {
49  attributesWriter.putAttributeConverters(converters);
50  }
51 
53  //
54  // interface implementation
55  //
57 
59  // <network ... > ... </network>
61 
62  @Override
63  public void startNetwork(final Network network, final BufferedWriter out) throws IOException {
64  out.write("<network");
65  if (network.getName() != null) {
66  out.write(" name=\"" + encodeAttributeValue(network.getName()) + "\"");
67  }
68  out.write(">\n\n");
69 
70  attributesWriter.writeAttributes( "\t" , out , network.getAttributes() );
71  }
72 
73  @Override
74  public void endNetwork(final BufferedWriter out) throws IOException {
75  out.write("</network>\n");
76  }
77 
79  // <nodes ... > ... </nodes>
81 
82  @Override
83  public void startNodes(final Network network, final BufferedWriter out) throws IOException {
84  out.write("\t<nodes>\n");
85  }
86 
87  @Override
88  public void endNodes(final BufferedWriter out) throws IOException {
89  out.write("\t</nodes>\n\n");
90  }
91 
93  // <links ... > ... </links>
95 
96  @Override
97  public void startLinks(final Network network, final BufferedWriter out) throws IOException {
98  out.write("\t<links");
99  if (network.getCapacityPeriod() != Integer.MIN_VALUE) {
100  out.write(" capperiod=\"" + Time.writeTime(network.getCapacityPeriod()) + "\"");
101  }
102 
103  out.write(" effectivecellsize=\"" + network.getEffectiveCellSize() + "\"");
104  out.write(" effectivelanewidth=\"" + network.getEffectiveLaneWidth() + "\"");
105 
106  out.write(">\n");
107  }
108 
109  @Override
110  public void endLinks(final BufferedWriter out) throws IOException {
111  out.write("\t</links>\n\n");
112  }
113 
115  // <node ... > ... </node>
117 
118  @Override
119  public void startNode(final Node node, final BufferedWriter out) throws IOException {
120  out.write("\t\t<node");
121  out.write(" id=\"" + encodeAttributeValue(node.getId().toString()) + "\"");
122  final Coord coord = transformation.transform( node.getCoord() );
123  out.write(" x=\"" + coord.getX() + "\"");
124  out.write(" y=\"" + coord.getY() + "\"");
125  if ( coord.hasZ() ) out.write(" z=\"" + coord.getZ() + "\"");
126  if (NetworkUtils.getType( node ) != null) {
127  out.write(" type=\"" + encodeAttributeValue(NetworkUtils.getType(node)) + "\"");
128  }
129  if (NetworkUtils.getOrigId( node ) != null) {
130  out.write(" origid=\"" + encodeAttributeValue(NetworkUtils.getOrigId(node)) + "\"");
131  }
132  out.write(" >\n");
133 
134  attributesWriter.writeAttributes( "\t\t\t" , out , node.getAttributes() );
135  }
136 
137  @Override
138  public void endNode(final BufferedWriter out) throws IOException {
139  out.write("\t\t</node>\n");
140  }
141 
143  // <link ... > ... </link>
145 
146  private Set<String> lastSet = null;
147  private String lastModes = null;
148 
149  @Override
150  public void startLink(final Link link, final BufferedWriter out) throws IOException {
151  out.write("\t\t<link");
152  out.write(" id=\"" + encodeAttributeValue(link.getId().toString()) + "\"");
153  out.write(" from=\"" + encodeAttributeValue(link.getFromNode().getId().toString()) + "\"");
154  out.write(" to=\"" + encodeAttributeValue(link.getToNode().getId().toString()) + "\"");
155  out.write(" length=\"" + link.getLength() + "\"");
156  out.write(" freespeed=\"" + link.getFreespeed() + "\"");
157  out.write(" capacity=\"" + link.getCapacity() + "\"");
158  out.write(" permlanes=\"" + link.getNumberOfLanes() + "\"");
159  out.write(" oneway=\"1\"");
160 
161  Set<String> modes = link.getAllowedModes();
162  if (modes != null) {
163  if (modes != this.lastSet) { // default LinkImpl internally caches the modes-set, thus the != operator works indeed
164  StringBuilder buffer = new StringBuilder();
165  int counter = 0;
166  for (String mode : modes) {
167  if (counter > 0) {
168  buffer.append(',');
169  }
170  buffer.append(mode);
171  counter++;
172  }
173  this.lastModes = encodeAttributeValue(buffer.toString());
174  this.lastSet = modes;
175  }
176  out.write(" modes=\"" + this.lastModes + "\"");
177  }
178 
179 // if (link instanceof Link) {
180 // Link link = link;
181 // if (NetworkUtils.getOrigId( link ) != null) {
182 // out.write(" origid=\"" + NetworkUtils.getOrigId( link ) + "\"");
183 // }
184 // if (NetworkUtils.getType(li) != null) {
185 // out.write(" type=\"" + NetworkUtils.getType(li) + "\"");
186 // }
187  // is now in the attributes, no special treatment. kai, dec'16
188 // }
189  out.write(" >\n");
190 
191  attributesWriter.writeAttributes( "\t\t\t" , out , link.getAttributes() );
192  }
193 
194  @Override
195  public void endLink(final BufferedWriter out) throws IOException {
196  out.write("\t\t</link>\n");
197  }
198 
200  // <!-- ============ ... ========== -->
202 
203  @Override
204  public void writeSeparator(final BufferedWriter out) throws IOException {
205  out.write("<!-- =================================================" +
206  "===================== -->\n\n");
207  }
208 }
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41