MATSIM
ConfigReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatsimConfigReader.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.UncheckedIOException;
24 import java.util.Stack;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
29 import org.xml.sax.Attributes;
30 
37 public final class ConfigReader extends MatsimXmlParser {
38 
39  private final static Logger log = LogManager.getLogger(ConfigReader.class);
40 
41  private final static String CONFIG_V1 = "config_v1.dtd";
42  private final static String CONFIG_V2 = "config_v2.dtd";
43 
44  private final ConfigAliases aliases = new ConfigAliases();
45  private final Config config;
46  private MatsimXmlParser delegate = null;
47 
48  private String localDtd;
49 
59  public ConfigReader(final Config config) {
60  super(ValidationType.DTD_ONLY);
61  this.config = config;
62  }
63 
65  return this.aliases;
66  }
67 
68  @Override
69  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
70  this.delegate.startTag(name, atts, context);
71  }
72 
73  @Override
74  public void endTag(final String name, final String content, final Stack<String> context) {
75  this.delegate.endTag(name, content, context);
76  }
77 
86  public void readFile(final String filename, final String dtdFilename) throws UncheckedIOException {
87  // yyyyyy if this is a necessary/useful method, I would prefer it in the superclass. kai, jul'16
88 
89  log.info("trying to read config from " + filename);
90  this.localDtd = dtdFilename;
91  readFile(filename);
92  this.localDtd = null;
93  }
94 
95  @Override
96  protected void setDoctype(final String doctype) {
97  super.setDoctype(doctype);
98  // Currently the only config-type is v1
99  if (CONFIG_V1.equals(doctype)) {
100  this.delegate = new ConfigReaderMatsimV1(this.config);
101  log.info("using config_v1-reader.");
102  }
103  else if ( CONFIG_V2.equals( doctype ) ) {
104  this.delegate = new ConfigReaderMatsimV2( this.config );
105  log.info( "using config_v2-reader" );
106  }
107  else {
108  throw new IllegalArgumentException("Doctype \"" + doctype + "\" not known.");
109  }
110  }
111 
112  // The following did override the inherited resolveEntity method. But I have no idea why that may have made sense. kai, jul'16
113 // @Override
114 // public InputSource resolveEntity(final String publicId, final String systemId) {
115 // InputSource is = super.resolveEntity(publicId, systemId);
116 // if (is != null) {
117 // // everything is fine, we can access the dtd
118 // return is;
119 // }
120 // // okay, standard procedure failed... let's see if we have it locally
121 // if (this.localDtd != null) {
122 // File dtdFile = new File(this.localDtd);
123 // if (dtdFile.exists() && dtdFile.isFile() && dtdFile.canRead()) {
124 // log.info("Using the local DTD " + this.localDtd);
125 // return new InputSource(this.localDtd);
126 // }
127 // }
128 // // hmm, didn't find the local one either... maybe inside a jar somewhere?
129 // int index = systemId.replace('\\', '/').lastIndexOf('/');
130 // String shortSystemId = systemId.substring(index + 1);
131 // InputStream stream = this.getClass().getResourceAsStream("/dtd/" + shortSystemId);
132 // if (stream != null) {
133 // log.info("Using local DTD from jar-file " + shortSystemId);
134 // return new InputSource(stream);
135 // }
136 // // we fail...
137 // return null;
138 // }
139 
140 }
void readFile(final String filename, final String dtdFilename)
void startTag(final String name, final Attributes atts, final Stack< String > context)
void endTag(final String name, final String content, final Stack< String > context)
abstract void startTag(String name, Attributes atts, Stack< String > context)
void setDoctype(final String doctype)
abstract void endTag(String name, String content, Stack< String > context)