MATSIM
NetworkWriterHandlerImplV1.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;
30 import org.matsim.core.utils.misc.Time;
31 
32 import java.io.BufferedWriter;
33 import java.io.IOException;
34 import java.util.Set;
35 
37 
38 /*package*/ class NetworkWriterHandlerImplV1 implements NetworkWriterHandler {
39  private final CoordinateTransformation transformation;
40 
41  NetworkWriterHandlerImplV1() {
42  this( new IdentityTransformation() );
43  }
44 
45  NetworkWriterHandlerImplV1(CoordinateTransformation transformation) {
46  this.transformation = transformation;
47  }
48 
50  //
51  // interface implementation
52  //
54 
56  // <network ... > ... </network>
58 
59  @Override
60  public void startNetwork(final Network network, final BufferedWriter out) throws IOException {
61  out.write("<network");
62  if ((network.getName() != null)) {
63  out.write(" name=\"" + encodeAttributeValue(network.getName()) + "\"");
64  }
65  out.write(">\n\n");
66  }
67 
68  @Override
69  public void endNetwork(final BufferedWriter out) throws IOException {
70  out.write("</network>\n");
71  }
72 
74  // <nodes ... > ... </nodes>
76 
77  @Override
78  public void startNodes(final Network network, final BufferedWriter out) throws IOException {
79  out.write("\t<nodes>\n");
80  }
81 
82  @Override
83  public void endNodes(final BufferedWriter out) throws IOException {
84  out.write("\t</nodes>\n\n");
85  }
86 
88  // <links ... > ... </links>
90 
91  @Override
92  public void startLinks(final Network network, final BufferedWriter out) throws IOException {
93  out.write("\t<links");
94  if (network.getCapacityPeriod() != Integer.MIN_VALUE) {
95  out.write(" capperiod=\"" + Time.writeTime(network.getCapacityPeriod()) + "\"");
96  }
97 
98  out.write(" effectivecellsize=\"" + network.getEffectiveCellSize() + "\"");
99  out.write(" effectivelanewidth=\"" + network.getEffectiveLaneWidth() + "\"");
100 
101  out.write(">\n");
102  }
103 
104  @Override
105  public void endLinks(final BufferedWriter out) throws IOException {
106  out.write("\t</links>\n\n");
107  }
108 
110  // <node ... > ... </node>
112 
113  @Override
114  public void startNode(final Node node, final BufferedWriter out) throws IOException {
115  out.write("\t\t<node");
116  out.write(" id=\"" + encodeAttributeValue(node.getId().toString()) + "\"");
117  final Coord coord = transformation.transform( node.getCoord() );
118  out.write(" x=\"" + coord.getX() + "\"");
119  out.write(" y=\"" + coord.getY() + "\"");
120  if( coord.hasZ() ) out.write(" z=\"" + coord.getZ() + "\"");
121  if (NetworkUtils.getType( node ) != null) {
122  out.write(" type=\"" + encodeAttributeValue(NetworkUtils.getType( node )) + "\"");
123  }
124  if (NetworkUtils.getOrigId( node ) != null) {
125  out.write(" origid=\"" + encodeAttributeValue(NetworkUtils.getOrigId( node )) + "\"");
126  }
127  out.write(" />\n");
128  }
129 
130  @Override
131  public void endNode(final BufferedWriter out) throws IOException {
132  }
133 
135  // <link ... > ... </link>
137 
138  private Set<String> lastSet = null;
139  private String lastModes = null;
140 
141  @Override
142  public void startLink(final Link link, final BufferedWriter out) throws IOException {
143  out.write("\t\t<link");
144  out.write(" id=\"" + encodeAttributeValue(link.getId().toString()) + "\"");
145  out.write(" from=\"" + encodeAttributeValue(link.getFromNode().getId().toString()) + "\"");
146  out.write(" to=\"" + encodeAttributeValue(link.getToNode().getId().toString()) + "\"");
147  out.write(" length=\"" + link.getLength() + "\"");
148  out.write(" freespeed=\"" + link.getFreespeed() + "\"");
149  out.write(" capacity=\"" + link.getCapacity() + "\"");
150  out.write(" permlanes=\"" + link.getNumberOfLanes() + "\"");
151  out.write(" oneway=\"1\"");
152 
153  Set<String> modes = link.getAllowedModes();
154  if (modes != null) {
155  if (modes != this.lastSet) { // default LinkImpl internally caches the modes-set, thus the != operator works indeed
156  StringBuilder buffer = new StringBuilder();
157  int counter = 0;
158  for (String mode : modes) {
159  if (counter > 0) {
160  buffer.append(',');
161  }
162  buffer.append(mode);
163  counter++;
164  }
165  this.lastModes = encodeAttributeValue(buffer.toString());
166  this.lastSet = modes;
167  }
168  out.write(" modes=\"" + this.lastModes + "\"");
169  }
170 
171  Link li = link;
172  if (NetworkUtils.getOrigId( li ) != null) {
173  out.write(" origid=\"" + encodeAttributeValue(NetworkUtils.getOrigId(li)) + "\"");
174  }
175  if (NetworkUtils.getType(li) != null) {
176  out.write(" type=\"" + encodeAttributeValue(NetworkUtils.getType(li)) + "\"");
177  }
178  out.write(" />\n");
179  }
180 
181  @Override
182  public void endLink(final BufferedWriter out) throws IOException {
183  }
184 
186  // <!-- ============ ... ========== -->
188 
189  @Override
190  public void writeSeparator(final BufferedWriter out) throws IOException {
191  out.write("<!-- =================================================" +
192  "===================== -->\n\n");
193  }
194 }
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41