MATSIM
ConfigReaderMatsimV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ConfigReaderMatsimV1.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 org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
28 import org.xml.sax.Attributes;
29 
30 import java.util.ArrayDeque;
31 import java.util.Deque;
32 import java.util.Stack;
33 
34 import static org.matsim.core.config.ConfigV2XmlNames.NAME;
35 
41  class ConfigReaderMatsimV1 extends MatsimXmlParser {
42 
43  private final static Logger log = LogManager.getLogger(ConfigReaderMatsimV1.class);
44 // private final static String CONFIG = "config";
45  private final static String MODULE = "module";
46  private final static String INCLUDE = "include";
47  private final static String PARAM = "param";
48 
49  private static final String msg = "using deprecated config version; please switch to config v2; your output_config.xml " +
50  "will be in the correct version; v1 will fail eventually, since we want to reduce the " +
51  "workload on keeping everything between v1 and v2 consistent (look into " +
52  "ScoringConfigGroup or RoutingConfigGroup if you want to know what we mean).";
53 
54 
55  private final Config config;
56  private final ConfigAliases aliases;
57  private final Deque<String> pathStack = new ArrayDeque<>();
58  private ConfigGroup currmodule = null;
59 
60  private String localDtd;
61 
62  public ConfigReaderMatsimV1(final Config config) {
63  super(ValidationType.DTD_ONLY);
64  this.config = config;
65  this.aliases = new ConfigAliases();
66  log.warn(msg);
67  }
68 
69  public ConfigReaderMatsimV1(final Config config, final ConfigAliases aliases) {
70  super(ValidationType.DTD_ONLY);
71  this.config = config;
72  this.aliases = aliases;
73  log.warn(msg);
74  }
75 
76  @Override
77  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
78  if (PARAM.equals(name)) {
79  startParam(atts);
80  } else if (MODULE.equals(name)) {
81  startModule(atts);
82  } else if (INCLUDE.equals(name)) {
83  log.warn("<include> is currently not supported.");
84  }
85  }
86 
87  @Override
88  public void endTag(final String name, final String content, final Stack<String> context) {
89  if (MODULE.equals(name)) {
90 
91  if (GlobalConfigGroup.GROUP_NAME.equals(name) ) {
92  if (!config.global().isInsistingOnDeprecatedConfigVersion()) {
93  throw new RuntimeException(msg);
94  }
95  }
96  // the idea here was to wait until the global config group is read because only then we can
97  // decide if the user is insisting on it. However, this clearly does not work in full since the condition
98  // will never be triggered when the global config group is not used at all in the file.
99  // :-( kai, aug'18
100 
101  this.currmodule = null;
102  this.pathStack.removeFirst();
103  }
104  }
105 
106  private void startModule(final Attributes atts) {
107  String name = this.aliases.resolveAlias(atts.getValue(NAME), this.pathStack);
108  this.currmodule = this.config.getModule(name);
109 
110  if (this.currmodule == null) {
111  //if there are type safe optional modules they have to be added here
112  if (name.equals(QSimConfigGroup.GROUP_NAME)){
113  this.currmodule = this.config.qsim();
114  }
115  //it must be a not type safe generic module
116  else {
117  this.currmodule = this.config.createModule(atts.getValue("name"));
118  }
119  }
120  this.pathStack.addFirst(name);
121  }
122 
123  private void startParam(final Attributes atts) {
124  String name = this.aliases.resolveAlias(atts.getValue(NAME), this.pathStack);
125  this.currmodule.addParam(name, atts.getValue("value"));
126  }
127 
135  public void readFile(final String filename, final String dtdFilename) {
136  this.localDtd = dtdFilename;
137  readFile(filename);
138  this.localDtd = null;
139  }
140 
141  // The following did override the inherited resolveEntity method. But I have no idea why that may have made sense. kai, jul'16
142 // @Override
143 // public InputSource resolveEntity(final String publicId, final String systemId) {
144 //
145 // InputSource is = super.resolveEntity(publicId, systemId);
146 // if (is == null && this.localDtd != null) {
147 // File dtdFile = new File(this.localDtd);
148 // if (dtdFile.exists() && dtdFile.isFile() && dtdFile.canRead()) {
149 // log.info("Using the local DTD " + this.localDtd);
150 // return new InputSource(this.localDtd);
151 // }
152 // return null;
153 // }
154 // return is;
155 // }
156 
157 }
final void readFile(final String filename)