MATSIM
MatsimFacilitiesReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatsimFacilitiesReader.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.facilities;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.Scenario;
29 import org.xml.sax.Attributes;
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Stack;
34 
42  /* Why is this suddenly a "Matsim"FacilitiesReader and not just a Facilities reader to be consistent with all other
43  * naming conventions? kai, jan09
44  * because all other readers in Matsim are also called Matsim*Reader,
45  * e.g. MatsimPopulationReader, MatsimNetworkReader, MatsimWorldReader, ...
46  * marcel, feb09
47  * The logic seems to be:
48  * - there is a "basic" MatsimXmlParser
49  * - there are implementations AbcReaderMatsimVx
50  * - there is a meta-class MatsimReaderAbc, which calls the Vx-Readers depending on the version
51  * - yy there is usually also an interface AbcReader, which is, however, not consistent:
52  * () sometimes, it is there, and sometimes not
53  * () sometimes, it is read(), sometimes it is readFile( file), sometimes ...
54  * () sometimes it throws an i/o exception, sometimes not
55  * Oh well.
56  * At least it seems indeed that the MatsimReader is indeed usually there. kai, jul09
57  */
58 
59 
60  private final static String FACILITIES_V1 = "facilities_v1.dtd";
61  private final static String FACILITIES_V2 = "facilities_v2.dtd";
62 
63  private final static Logger log = LogManager.getLogger(MatsimFacilitiesReader.class);
64 
65  private final String externalInputCRS;
66  private final String targetCRS;
68 
70  private MatsimXmlParser delegate = null;
71  private Map<Class<?>, AttributeConverter<?>> attributeConverters = new HashMap<>();
72 
79  public MatsimFacilitiesReader(final Scenario scenario) {
80  this(null, scenario);
81  }
82 
92  final String targetCRS,
93  final Scenario scenario) {
94  this(scenario.getConfig().facilities().getInputCRS(), targetCRS, scenario.getActivityFacilities());
95  }
96 
106  final String externalInputCRS,
107  final String targetCRS,
108  final ActivityFacilities facilities) {
109  super(ValidationType.DTD_ONLY);
110  this.externalInputCRS = externalInputCRS;
111  this.targetCRS = targetCRS;
112  this.facilities = facilities;
113  }
114 
115  public void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter) {
116  this.attributeConverters.put(clazz, converter);
117  }
118 
119  public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
120  this.attributeConverters.putAll(converters);
121  }
122 
123  @Override
124  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
125  this.delegate.startTag(name, atts, context);
126  }
127 
128  @Override
129  public void endTag(final String name, final String content, final Stack<String> context) {
130  this.delegate.endTag(name, content, context);
131  }
132 
133  @Override
134  protected void setDoctype(final String doctype) {
135  super.setDoctype(doctype);
136  if (FACILITIES_V1.equals(doctype)) {
137  this.delegate = new FacilitiesReaderMatsimV1(this.externalInputCRS, this.targetCRS, this.facilities);
138  ((FacilitiesReaderMatsimV1)this.delegate).putAttributeConverters(this.attributeConverters);
139  log.info("using facilities_v1-reader.");
140  } else if (FACILITIES_V2.equals(doctype)) { // v2 added support for 3D coordinates
141  this.delegate = new FacilitiesReaderMatsimV2(this.externalInputCRS, this.targetCRS, this.facilities);
142  ((FacilitiesReaderMatsimV2)this.delegate).putAttributeConverters(this.attributeConverters);
143  log.info("using facilities_v2-reader.");
144  } else {
145  throw new IllegalArgumentException("Doctype \"" + doctype + "\" not known.");
146  }
147  }
148 
149 }
abstract void startTag(String name, Attributes atts, Stack< String > context)
Map< Class<?>, AttributeConverter<?> > attributeConverters
MatsimFacilitiesReader(final String externalInputCRS, final String targetCRS, final ActivityFacilities facilities)
final FacilitiesConfigGroup facilities()
Definition: Config.java:423
abstract void endTag(String name, String content, Stack< String > context)
void endTag(final String name, final String content, final Stack< String > context)
ActivityFacilities getActivityFacilities()
MatsimFacilitiesReader(final String targetCRS, final Scenario scenario)
void startTag(final String name, final Attributes atts, final Stack< String > context)
void putAttributeConverters(Map< Class<?>, AttributeConverter<?>> converters)
void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter)