MATSIM
ConfigWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ConfigWriter.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.config;
22 
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.io.UncheckedIOException;
26 
29 
30 public final class ConfigWriter extends MatsimXmlWriter implements MatsimWriter {
31 
32  public static enum Verbosity {all, minimal }
33 
35  // member variables
37 
38  private final Config config;
39  private ConfigWriterHandler handler = null;
40  private String dtd = null;
41 
43  // constructors
45  public ConfigWriter(final Config config, final Verbosity verbosity ) {
46  this.config = config;
47  // always write the latest version, currently v2
48  this.dtd = "http://www.matsim.org/files/dtd/config_v2.dtd";
49  this.handler = new ConfigWriterHandlerImplV2(verbosity);
50  }
51  public ConfigWriter(final Config config) {
52  this( config, Verbosity.all ) ;
53  }
54 
56  // write methods
58 
59  public final void writeStream(final java.io.Writer writer) {
60  try {
61  this.writer = new BufferedWriter(writer);
62  write();
63  this.writer.flush();
64  this.writer = null;
65  } catch (IOException e) {
66  throw new UncheckedIOException(e);
67  }
68  }
69 
70  public final void writeStream(final java.io.Writer writer, final String newline) {
71  try {
72  final String formerNewLine = this.handler.setNewline(newline);
73  this.writer = new BufferedWriter(writer);
74  write();
75  this.writer.flush();
76  this.writer = null;
77  this.handler.setNewline( formerNewLine );
78  } catch (IOException e) {
79  throw new UncheckedIOException(e);
80  }
81  }
82 
83  @Override
84  public final void write(final String filename) throws UncheckedIOException {
85  openFile(filename);
86  write();
87  close();
88  }
89 
90  public final void writeFileV1(final String filename) {
91  this.dtd = "http://www.matsim.org/files/dtd/config_v1.dtd";
92  this.handler = new ConfigWriterHandlerImplV1();
93  write( filename );
94  }
95 
96  public final void writeFileV2(final String filename) {
97  this.dtd = "http://www.matsim.org/files/dtd/config_v2.dtd";
98  this.handler = new ConfigWriterHandlerImplV2(Verbosity.all);
99  write( filename );
100  }
101 
102  private void write() {
103  try {
104  writeXmlHead();
105  writeDoctype("config", this.dtd);
106 
107  this.handler.startConfig(this.config, this.writer);
108  this.handler.writeSeparator(this.writer);
109  for (ConfigGroup m : this.config.getModules().values()) {
110  this.handler.writeModule(m, this.writer);
111  this.handler.writeSeparator(this.writer);
112  }
113  this.handler.endConfig(this.writer);
114  this.writer.flush();
115  }
116  catch (IOException e) {
117  throw new UncheckedIOException(e);
118  }
119  }
120 
121 }
final void write(final String filename)
final void writeStream(final java.io.Writer writer)
ConfigWriter(final Config config, final Verbosity verbosity)
final void writeFileV1(final String filename)
final void writeFileV2(final String filename)
final TreeMap< String, ConfigGroup > getModules()
Definition: Config.java:288
final void writeStream(final java.io.Writer writer, final String newline)
final void writeDoctype(String rootTag, String dtdUrl)