MATSIM
MatricesWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatricesWriter.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.matrices;
22 
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 
29 
30 public class MatricesWriter extends MatsimXmlWriter implements MatsimWriter {
31 
33  // member variables
35 
36  private MatricesWriterHandler handler = null;
37  private final Matrices matrices;
38  private String dtd;
39 
41  // constructors
43 
44  public MatricesWriter(final Matrices matrices) {
45  super();
46  this.matrices = matrices;
47  // always write out in newest version, currently v1
48  this.dtd = "http://matsim.org/files/dtd/matrices_v1.dtd";
49  this.handler = new MatricesWriterHandlerImplV1();
50  }
51 
53  // write methods
55 
56  @Override
57  public final void write(final String filename) {
58  try {
59  openFile(filename);
60  writeXmlHead();
61  writeDoctype("matrices", this.dtd);
62  this.handler.startMatrices(this.matrices, this.writer);
63  this.handler.writeSeparator(this.writer);
64  Iterator<Matrix> m_it = this.matrices.getMatrices().values().iterator();
65  while (m_it.hasNext()) {
66  Matrix m = m_it.next();
67  this.handler.startMatrix(m, this.writer);
68  Iterator<ArrayList<Entry>> eal_it = m.getFromLocations().values().iterator();
69  while (eal_it.hasNext()) {
70  ArrayList<Entry> eal = eal_it.next();
71  Iterator<Entry> e_it = eal.iterator();
72  while (e_it.hasNext()) {
73  Entry e = e_it.next();
74  this.handler.startEntry(e, this.writer);
75  this.handler.endEntry(this.writer);
76  }
77  }
78  this.handler.endMatrix(this.writer);
79  this.handler.writeSeparator(this.writer);
80  this.writer.flush();
81  }
82  this.handler.endMatrices(this.writer);
83  close();
84  }
85  catch (IOException e) {
86  throw new RuntimeException(e);
87  }
88  }
89 
91  // print methods
93 
94  @Override
95  public final String toString() {
96  return super.toString();
97  }
98 }
Definition: Entry.java:23
final Map< String, ArrayList< Entry > > getFromLocations()
Definition: Matrix.java:159
MatricesWriter(final Matrices matrices)
final TreeMap< String, Matrix > getMatrices()
Definition: Matrices.java:65
final void writeDoctype(String rootTag, String dtdUrl)
final void write(final String filename)