MATSIM
TransitScheduleReader.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TransitScheduleReader.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 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.pt.transitSchedule.api;
22 
23 import org.matsim.api.core.v01.Scenario;
31 import org.xml.sax.Attributes;
32 import org.xml.sax.SAXException;
33 
34 import java.io.InputStream;
35 import java.io.UncheckedIOException;
36 import java.net.URL;
37 import java.util.Stack;
38 
45 public class TransitScheduleReader implements MatsimReader {
46 
47  private final Scenario scenario;
48 
49  private final String externalInputCRS;
50  private final String targetCRS;
51 
53  final String targetCRS,
54  final Scenario scenario) {
55  this(null, targetCRS, scenario);
56  }
57 
59  final String externalInputCRS,
60  final String targetCRS,
61  final Scenario scenario) {
62  this.externalInputCRS = externalInputCRS;
63  this.targetCRS = targetCRS;
64  this.scenario = scenario;
65  }
66 
67  public TransitScheduleReader(final Scenario scenario) {
68  this(null, null, scenario);
69  }
70 
71  @Override
72  public void readFile(final String filename) throws UncheckedIOException {
73  new XmlScheduleReader(externalInputCRS, targetCRS, this.scenario).readFile(filename);
74  }
75  @Override
76  public void readURL( final URL url ) throws UncheckedIOException {
77  new XmlScheduleReader(externalInputCRS, targetCRS, this.scenario).parse(url);
78  }
79 
80  public void readStream(final InputStream stream) throws UncheckedIOException {
81  new XmlScheduleReader(externalInputCRS, targetCRS, this.scenario).parse(stream);
82  }
83 
84  private static class XmlScheduleReader extends MatsimXmlParser {
85 
86  private MatsimXmlParser delegate = null;
87  private final String externalInputCRS;
88  private final String targetCRS;
89  private final Scenario scenario;
90 
91  public XmlScheduleReader(String externalInputCRS, String targetCRS, Scenario scenario) {
92  super(ValidationType.DTD_ONLY);
93  this.externalInputCRS = externalInputCRS;
94  this.targetCRS = targetCRS;
95  this.scenario = scenario;
96  }
97 
98  @Override
99  public void startTag(String name, Attributes atts, Stack<String> context) {
100  this.delegate.startTag(name, atts, context);
101  }
102 
103  @Override
104  public void endTag(String name, String content, Stack<String> context) {
105  this.delegate.endTag(name, content, context);
106  }
107 
108  @Override
109  protected void setDoctype(String doctype) {
110  super.setDoctype(doctype);
111 
112  if ("transitSchedule_v2.dtd".equals(doctype)) {
113  this.delegate = new TransitScheduleReaderV2(externalInputCRS, targetCRS, this.scenario);
114  } else if ("transitSchedule_v1.dtd".equals(doctype)) {
115  this.delegate = new TransitScheduleReaderV1(
116  externalInputCRS != null ?
117  TransformationFactory.getCoordinateTransformation(externalInputCRS, targetCRS) :
119  this.scenario);
120  } else {
121  throw new IllegalArgumentException("Unsupported doctype: " + doctype);
122  }
123  }
124 
125  @Override
126  public void endDocument() {
127  try {
128  this.delegate.endDocument();
129  } catch (SAXException e) {
130  throw new RuntimeException(e);
131  }
132  if (targetCRS != null) {
134  }
135  }
136  }
137 
138 }
XmlScheduleReader(String externalInputCRS, String targetCRS, Scenario scenario)
TransitScheduleReader(final String targetCRS, final Scenario scenario)
TransitScheduleReader(final String externalInputCRS, final String targetCRS, final Scenario scenario)
abstract void startTag(String name, Attributes atts, Stack< String > context)
static CoordinateTransformation getCoordinateTransformation(final String fromSystem, final String toSystem)
static< T extends MatsimToplevelContainer &Attributable > void putCRS(T container, String CRS)
void endTag(String name, String content, Stack< String > context)
void startTag(String name, Attributes atts, Stack< String > context)
abstract void endTag(String name, String content, Stack< String > context)
final void readFile(final String filename)
TransitSchedule getTransitSchedule()