MATSIM
MatsimNetworkReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MatsimNetworkReader.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.network.io;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Stack;
26 
27 import org.apache.logging.log4j.LogManager;
28 import org.apache.logging.log4j.Logger;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.SAXException;
37 
44 public final class MatsimNetworkReader extends MatsimXmlParser {
45 
46  private final static Logger log = LogManager.getLogger(MatsimNetworkReader.class);
47  private final static String NETWORK_V1 = "network_v1.dtd";
48  private final static String NETWORK_V2 = "network_v2.dtd";
49 
50  private MatsimXmlParser delegate = null;
51  private final String inputCRS;
52  private final String targetCRS;
53 
54  private final Network network;
55  private Map<Class<?>, AttributeConverter<?>> converters = new HashMap<>();
56 
62  public MatsimNetworkReader(Network network) {
63  this( null , network );
64  }
65 
72  public MatsimNetworkReader(String targetCRS, Network network) {
73  this(null, targetCRS, network);
74  }
75 
76  public MatsimNetworkReader(String inputCRS, String targetCRS, Network network) {
77  super(ValidationType.DTD_ONLY);
78  this.inputCRS = inputCRS;
79  this.targetCRS = targetCRS;
80  this.network = network;
81  }
82 
83  @Override
84  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
85  this.delegate.startTag(name, atts, context);
86  }
87 
88  @Override
89  public void endTag(final String name, final String content, final Stack<String> context) {
90  this.delegate.endTag(name, content, context);
91  }
92 
93  @Override
94  public void endDocument() {
95  try {
96  this.delegate.endDocument();
97  } catch (SAXException e) {
98  throw new RuntimeException(e);
99  }
100  if (targetCRS != null) {
101  ProjectionUtils.putCRS(network, targetCRS);
102  }
103  }
104 
105  @Override
106  protected void setDoctype(final String doctype) {
107  super.setDoctype(doctype);
108 
109  switch ( doctype ) {
110  case NETWORK_V1:
111  this.delegate =
112  new NetworkReaderMatsimV1(
113  inputCRS != null ?
116  this.network);
117  log.info("using network_v1-reader.");
118  break;
119  case NETWORK_V2:
120  this.delegate = new NetworkReaderMatsimV2(inputCRS, targetCRS, this.network);
121  ((NetworkReaderMatsimV2) delegate).putAttributeConverters( converters );
122  log.info("using network_v2-reader.");
123  break;
124  default:
125  throw new IllegalArgumentException("Doctype \"" + doctype + "\" not known.");
126  }
127  }
128 
129  public void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter) {
130  this.converters.put( clazz, converter );
131  }
132 
133  public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> attributeConverters) {
134  this.converters.putAll( attributeConverters );
135  }
136 }
void putAttributeConverters(Map< Class<?>, AttributeConverter<?>> attributeConverters)
abstract void startTag(String name, Attributes atts, Stack< String > context)
MatsimNetworkReader(String inputCRS, String targetCRS, Network network)
static CoordinateTransformation getCoordinateTransformation(final String fromSystem, final String toSystem)
void startTag(final String name, final Attributes atts, final Stack< String > context)
static< T extends MatsimToplevelContainer &Attributable > void putCRS(T container, String CRS)
void putAttributeConverter(Class<?> clazz, AttributeConverter<?> converter)
void endTag(final String name, final String content, final Stack< String > context)
Map< Class<?>, AttributeConverter<?> > converters
abstract void endTag(String name, String content, Stack< String > context)
MatsimNetworkReader(String targetCRS, Network network)