MATSIM
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | Static Private Attributes | List of all members
org.matsim.counts.MatsimCountsReader Class Reference
Inheritance diagram for org.matsim.counts.MatsimCountsReader:
Inheritance graph
[legend]

Public Member Functions

 MatsimCountsReader (final Counts counts)
 
 MatsimCountsReader (String inputCRS, String targetCRS, final Counts counts)
 
 MatsimCountsReader (String inputCRS, String targetCRS, final Counts counts, Class<? extends Identifiable<?>> idClass)
 
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)
 
final void setValidating (final boolean validateXml)
 
final void setNamespaceAware (final boolean awareness)
 
final void setLocalDtdDirectory (final String localDtdDirectory)
 
final void readFile (final String filename) throws UncheckedIOException
 
final void readURL (final URL url) throws UncheckedIOException
 
final void readStream (InputStream stream) throws UncheckedIOException
 
final void parse (final URL url) throws UncheckedIOException
 
final void parse (final InputStream stream) throws UncheckedIOException
 
final void parse (final InputSource input) throws UncheckedIOException
 
final String getDoctype ()
 
final InputSource resolveEntity (final String publicId, final String systemId)
 
void characters (final char[] ch, final int start, final int length) throws SAXException
 
final void startElement (final String uri, final String localName, final String qName, Attributes atts)
 
final void endElement (final String uri, final String localName, final String qName) throws SAXException
 
final void error (final SAXParseException ex) throws SAXException
 
final void fatalError (final SAXParseException ex) throws SAXException
 
final void warning (final SAXParseException ex) throws SAXException
 

Static Public Member Functions

static double parseDouble (String value) throws NumberFormatException
 

Protected Member Functions

void setDoctype (final String doctype)
 

Private Attributes

final Counts counts
 
final String inputCRS
 
final String targetCRS
 
MatsimXmlParser delegate = null
 
final Class<? extends Identifiable<?> > idClass
 

Static Private Attributes

static final Logger log = LogManager.getLogger(MatsimCountsReader.class)
 
static final String COUNTS_V1 = "counts_v1.xsd"
 
static final String COUNTS_V2 = "counts_v2.xsd"
 

Detailed Description

A reader for counts-files of MATSim. This reader recognizes the format of the counts-file and uses the correct reader for the specific counts-version, without manual setting.

Author
mrieser

Definition at line 42 of file MatsimCountsReader.java.

Constructor & Destructor Documentation

◆ MatsimCountsReader() [1/3]

org.matsim.counts.MatsimCountsReader.MatsimCountsReader ( final Counts  counts)

Creates a new reader for MATSim counts files.

Parameters
countsThe Counts-object to store the configuration settings in.

Definition at line 59 of file MatsimCountsReader.java.

References org.matsim.counts.MatsimCountsReader.counts.

59  {
60  this( null, null, counts, Link.class );
61  }

◆ MatsimCountsReader() [2/3]

org.matsim.counts.MatsimCountsReader.MatsimCountsReader ( String  inputCRS,
String  targetCRS,
final Counts  counts 
)

Creates a new reader for MATSim counts files.

Parameters
inputCRS
targetCRS
countsThe Counts-object to store the configuration settings in.

Definition at line 70 of file MatsimCountsReader.java.

References org.matsim.counts.MatsimCountsReader.counts, org.matsim.counts.MatsimCountsReader.inputCRS, and org.matsim.counts.MatsimCountsReader.targetCRS.

◆ MatsimCountsReader() [3/3]

org.matsim.counts.MatsimCountsReader.MatsimCountsReader ( String  inputCRS,
String  targetCRS,
final Counts  counts,
Class<? extends Identifiable<?>>  idClass 
)

Creates a new reader for MATSim counts files.

Parameters
inputCRS
targetCRS
countsthe counts object to store the configuration settings in
idClassid class of locations

Definition at line 83 of file MatsimCountsReader.java.

References org.matsim.counts.MatsimCountsReader.counts, org.matsim.counts.MatsimCountsReader.idClass, org.matsim.counts.MatsimCountsReader.inputCRS, org.matsim.counts.MatsimCountsReader.targetCRS, and org.matsim.core.utils.io.MatsimXmlParser.ValidationType.XSD_ONLY.

85  {
86  super(ValidationType.XSD_ONLY);
87  this.inputCRS = inputCRS;
88  this.targetCRS = targetCRS;
89  this.counts = counts;
90  this.idClass = idClass;
91  }
final Class<? extends Identifiable<?> > idClass

Member Function Documentation

◆ startTag() [1/2]

void org.matsim.counts.MatsimCountsReader.startTag ( final String  name,
final Attributes  atts,
final Stack< String >  context 
)

Definition at line 94 of file MatsimCountsReader.java.

References org.matsim.core.utils.io.MatsimXmlParser.startTag().

94  {
95  this.delegate.startTag(name, atts, context);
96  }
abstract void startTag(String name, Attributes atts, Stack< String > context)
Here is the call graph for this function:

◆ endTag()

void org.matsim.counts.MatsimCountsReader.endTag ( final String  name,
final String  content,
final Stack< String >  context 
)

Definition at line 99 of file MatsimCountsReader.java.

References org.matsim.core.utils.io.MatsimXmlParser.endTag().

99  {
100  this.delegate.endTag(name, content, context);
101  }
abstract void endTag(String name, String content, Stack< String > context)
Here is the call graph for this function:

◆ setDoctype()

void org.matsim.counts.MatsimCountsReader.setDoctype ( final String  doctype)
protected

Definition at line 104 of file MatsimCountsReader.java.

References org.matsim.core.utils.geometry.transformations.TransformationFactory.getCoordinateTransformation().

104  {
105  super.setDoctype(doctype);
106  // Currently the only counts-type is v1
107  if (COUNTS_V1.equals(doctype)) {
108  CoordinateTransformation coordinateTransformation = new IdentityTransformation();
109  if (inputCRS != null && targetCRS != null) {
110  coordinateTransformation = TransformationFactory.getCoordinateTransformation(inputCRS, targetCRS );
111  }
112  this.delegate = new CountsReaderMatsimV1( coordinateTransformation, this.counts);
113  log.info("using counts_v1-reader.");
114  } else if (COUNTS_V2.equals(doctype)) {
115  this.delegate = new CountsReaderMatsimV2( inputCRS, targetCRS, this.counts, idClass);
116  log.info("using counts_v2-reader.");
117 
118  } else {
119  throw new IllegalArgumentException("Doctype \"" + doctype + "\" not known.");
120  }
121  }
final Class<? extends Identifiable<?> > idClass
Here is the call graph for this function:

◆ startTag() [2/2]

abstract void org.matsim.core.utils.io.MatsimXmlParser.startTag ( String  name,
Attributes  atts,
Stack< String >  context 
)
abstractinherited

◆ setValidating()

final void org.matsim.core.utils.io.MatsimXmlParser.setValidating ( final boolean  validateXml)
inherited

Sets, if this parser should validate the read XML or not. Not validating is sometimes useful during development or during some tests with format-extensions that are not yet part of the DTD, but it is strongly discouraged not to validate during production use.

Parameters
validateXmlWhether the parsed XML should be validated or not.

Definition at line 117 of file MatsimXmlParser.java.

Referenced by org.matsim.core.events.EventsConverterXML.EventsConverterXML(), and org.matsim.core.events.EventsReaderXMLv1.EventsReaderXMLv1().

117  {
118  this.isValidating = validateXml;
119  }

◆ setNamespaceAware()

final void org.matsim.core.utils.io.MatsimXmlParser.setNamespaceAware ( final boolean  awareness)
inherited

Specifies that the parser produced by this code will provide support for XML namespaces. By default the value of this is set to false.

Parameters
awarenesstrue if the parser produced by this code will provide support for XML namespaces; false otherwise.
See also
javax.xml.parsers.SAXParserFactory::setNamespaceAware(boolean)

Definition at line 128 of file MatsimXmlParser.java.

128  {
129  this.isNamespaceAware = awareness;
130  }

◆ setLocalDtdDirectory()

final void org.matsim.core.utils.io.MatsimXmlParser.setLocalDtdDirectory ( final String  localDtdDirectory)
inherited

Sets the directory where to look for DTD and XSD files if they are not found at the location specified in the XML.

Parameters
localDtdDirectory

Definition at line 138 of file MatsimXmlParser.java.

138  {
139  this.localDtdBase = localDtdDirectory;
140  }

◆ readFile()

final void org.matsim.core.utils.io.MatsimXmlParser.readFile ( final String  filename) throws UncheckedIOException
inherited

Parses the specified file. The file can be gzip-compressed and is decompressed on-the-fly while parsing. A gzip-compressed file must have the ending ".gz" to be correctly recognized. The passed filename may or may not contain the ending ".gz". If no uncompressed file is found with the specified name, the ending ".gz" will be added to the filename and a compressed file will be searched for and read if found.

Parameters
filenameThe filename of the file to read, optionally ending with ".gz" to force reading a gzip-compressed file.
Exceptions
UncheckedIOException

Implements org.matsim.core.api.internal.MatsimReader.

Definition at line 152 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.IOUtils.getBufferedReader(), and org.matsim.core.utils.io.MatsimXmlParser.parse().

Referenced by org.matsim.utils.gis.matsim2esri.plans.SelectedPlans2ESRIShape.main(), org.matsim.core.utils.io.OsmNetworkReader.parse(), org.matsim.vehicles.MatsimVehicleReader.readFile(), org.matsim.pt.transitSchedule.api.TransitScheduleReader.readFile(), org.matsim.core.events.MatsimEventsReader.readFile(), org.matsim.core.population.io.StreamingPopulationReader.readFile(), org.matsim.core.network.NetworkUtils.readNetwork(), org.matsim.core.population.PopulationUtils.readPopulation(), org.matsim.core.network.algorithms.intersectionSimplifier.RunIntersectionSimplifier.run(), and org.matsim.run.gui.ScheduleValidatorWindow.run().

152  {
153  log.info("starting to parse xml from file " + filename + " ...");
154  this.theSource = filename;
155  parse(new InputSource(IOUtils.getBufferedReader(filename)));
156  }
Here is the call graph for this function:

◆ readURL()

final void org.matsim.core.utils.io.MatsimXmlParser.readURL ( final URL  url) throws UncheckedIOException
inherited

Implements org.matsim.core.api.internal.MatsimReader.

Definition at line 159 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.parse().

Referenced by org.matsim.vehicles.MatsimVehicleReader.readURL(), and org.matsim.core.events.MatsimEventsReader.readURL().

159  {
160  parse( url ) ;
161  }
Here is the call graph for this function:

◆ readStream()

final void org.matsim.core.utils.io.MatsimXmlParser.readStream ( InputStream  stream) throws UncheckedIOException
inherited

Definition at line 163 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.parse().

163  {
164  parse(stream);
165  }
Here is the call graph for this function:

◆ parse() [1/3]

final void org.matsim.core.utils.io.MatsimXmlParser.parse ( final URL  url) throws UncheckedIOException
inherited

◆ parse() [2/3]

final void org.matsim.core.utils.io.MatsimXmlParser.parse ( final InputStream  stream) throws UncheckedIOException
inherited

Definition at line 175 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.parse().

175  {
176  this.theSource = "stream";
177  parse(new InputSource(stream));
178  }
Here is the call graph for this function:

◆ parse() [3/3]

final void org.matsim.core.utils.io.MatsimXmlParser.parse ( final InputSource  input) throws UncheckedIOException
inherited

Definition at line 180 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.ValidationType.DTD_ONLY, and org.matsim.core.utils.io.MatsimXmlParser.ValidationType.NO_VALIDATION.

180  {
181  try {
182  boolean validating = this.isValidating && this.validationType != ValidationType.NO_VALIDATION;
183  boolean useWstxParser = !validating || this.validationType == ValidationType.DTD_ONLY;
184 
185  if (useWstxParser) {
186  // use Woodstox-library as XML parser when no validation or only DTD-validation is required, as it is much faster than the default (xerces)
187 
188  WstxSAXParserFactory factory = new WstxSAXParserFactory();
189  factory.setValidating(validating);
190  factory.setNamespaceAware(this.isNamespaceAware);
191  factory.setFeature("http://xml.org/sax/features/external-general-entities", false); // prevent XEE attack: https://en.wikipedia.org/wiki/XML_external_entity_attack
192 
193  if (validating) {
194  factory.setFeature("http://xml.org/sax/features/validation", true); // required to enable DTD validation in Woodstox
195  SAXParser parser = factory.newSAXParser();
196  XMLReader reader = parser.getXMLReader();
197  reader.setContentHandler(this);
198  reader.setErrorHandler(this);
199  reader.setEntityResolver(this);
200  reader.parse(input);
201  } else {
202  SAXParser parser = factory.newSAXParser();
203  parser.parse(input, this);
204  }
205 
206  } else {
207  // use the default (Xerces) SAX parser, it is slower than Woodstox, but supports XSD validation
208 
209  SAXParserFactory factory = SAXParserFactory.newInstance();
210  factory.setValidating(validating);
211  factory.setNamespaceAware(this.isNamespaceAware);
212  factory.setFeature("http://xml.org/sax/features/external-general-entities", false); // prevent XEE attack: https://en.wikipedia.org/wiki/XML_external_entity_attack
213 
214  if (validating) {
215  // enable optional support for XML Schemas
216  factory.setFeature("http://apache.org/xml/features/validation/schema", true);
217  SAXParser parser = factory.newSAXParser();
218  XMLReader reader = parser.getXMLReader();
219  reader.setContentHandler(this);
220  reader.setErrorHandler(this);
221  reader.setEntityResolver(this);
222  reader.parse(input);
223  } else {
224  SAXParser parser = factory.newSAXParser();
225  parser.parse(input, this);
226  }
227  }
228  } catch (IOException e) {
229  throw new UncheckedIOException(e);
230  } catch (SAXException | ParserConfigurationException e) {
231  throw new UncheckedIOException(new IOException(e));
232  }
233  }

◆ getDoctype()

final String org.matsim.core.utils.io.MatsimXmlParser.getDoctype ( )
inherited

Definition at line 235 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.doctype.

235  {
236  return this.doctype;
237  }

◆ resolveEntity()

final InputSource org.matsim.core.utils.io.MatsimXmlParser.resolveEntity ( final String  publicId,
final String  systemId 
)
inherited

Definition at line 248 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.findDtdInClasspath(), org.matsim.core.utils.io.MatsimXmlParser.findDtdInDefaultLocation(), org.matsim.core.utils.io.MatsimXmlParser.findDtdInLocalFilesystem(), org.matsim.core.utils.io.MatsimXmlParser.findDtdInRemoteLocation(), and org.matsim.core.utils.io.MatsimXmlParser.setDoctype().

248  {
249  // ConfigReader* did override this. Not sure if it did that for good reasons. kai, jul'16
250 
251  // extract the last part of the systemId
252  int index = systemId.replace('\\', '/').lastIndexOf('/');
253  String shortSystemId = systemId.substring(index + 1);
254 
255  if (this.doctype == null) {
256  // this is the first systemId we have to resolve, assume it's the doctype
257  // I haven't found any other way to determine the doctype of the currently read file
258  setDoctype(shortSystemId);
259  }
260 
261  InputSource source;
262  if (this.preferLocalDtds) {
263  source = findDtdInLocalFilesystem(shortSystemId);
264  if (source == null) {
265  source = findDtdInClasspath(shortSystemId);
266  }
267  if (source == null) {
268  source = findDtdInDefaultLocation(shortSystemId);
269  }
270  if (source == null) {
271  source = findDtdInRemoteLocation(systemId);
272  }
273  } else {
274  source = findDtdInRemoteLocation(systemId);
275  if (source == null) {
276  source = findDtdInLocalFilesystem(shortSystemId);
277  }
278  if (source == null) {
279  source = findDtdInClasspath(shortSystemId);
280  }
281  if (source == null) {
282  source = findDtdInDefaultLocation(shortSystemId);
283  }
284  }
285 
286  if (source == null) {
287  // We could neither get the remote nor the local version of the dtd, show a warning
288  log.warn("Could neither get the DTD from the web nor a local one. " + systemId);
289  } else {
290  source.setSystemId(systemId);
291  }
292  return source;
293  }
static InputSource findDtdInDefaultLocation(final String shortSystemId)
static InputSource findDtdInRemoteLocation(final String fullSystemId)
InputSource findDtdInLocalFilesystem(final String shortSystemId)
InputSource findDtdInClasspath(final String shortSystemId)
Here is the call graph for this function:

◆ characters()

void org.matsim.core.utils.io.MatsimXmlParser.characters ( final char []  ch,
final int  start,
final int  length 
) throws SAXException
inherited

Definition at line 352 of file MatsimXmlParser.java.

352  {
353  // has to be non-final since otherwise the events parser does not work. Probably ok (this here is just a default implementation). kai, jul'16
354 
355  StringBuffer buffer = this.buffers.peek();
356  if (buffer != null) {
357  buffer.append(ch, start, length);
358  }
359  }

◆ startElement()

final void org.matsim.core.utils.io.MatsimXmlParser.startElement ( final String  uri,
final String  localName,
final String  qName,
Attributes  atts 
)
inherited

Definition at line 362 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.startTag().

362  {
363  // I have not good intuition if making this one non-final might be ok. kai, jul'16
364 
365  String tag = (uri.length() == 0) ? qName : localName;
366  this.buffers.push(new StringBuffer());
367  this.startTag(tag, atts, this.theContext);
368  this.theContext.push(tag);
369  }
abstract void startTag(String name, Attributes atts, Stack< String > context)
Here is the call graph for this function:

◆ endElement()

final void org.matsim.core.utils.io.MatsimXmlParser.endElement ( final String  uri,
final String  localName,
final String  qName 
) throws SAXException
inherited

Definition at line 372 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.endTag(), and org.matsim.core.utils.io.MatsimXmlParser.theContext.

372  {
373  // I have not good intuition if making this one non-final might be ok. kai, jul'16
374 
375  String tag = (uri.length() == 0) ? qName : localName;
376  this.theContext.pop();
377  StringBuffer buffer = this.buffers.pop();
378  this.endTag(tag, buffer.toString(), this.theContext);
379  }
abstract void endTag(String name, String content, Stack< String > context)
Here is the call graph for this function:

◆ error()

final void org.matsim.core.utils.io.MatsimXmlParser.error ( final SAXParseException  ex) throws SAXException
inherited

Definition at line 384 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.getInputSource().

384  {
385  if (this.theContext.isEmpty()) {
386  System.err.println("Missing DOCTYPE.");
387  }
388  System.err.println("XML-ERROR: " + getInputSource(ex) + ", line " + ex.getLineNumber() + ", column " + ex.getColumnNumber() + ":");
389  System.err.println(ex.toString());
390  throw ex;
391  }
String getInputSource(final SAXParseException ex)
Here is the call graph for this function:

◆ fatalError()

final void org.matsim.core.utils.io.MatsimXmlParser.fatalError ( final SAXParseException  ex) throws SAXException
inherited

Definition at line 394 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.getInputSource().

394  {
395  System.err.println("XML-FATAL: " + getInputSource(ex) + ", line " + ex.getLineNumber() + ", column " + ex.getColumnNumber() + ":");
396  System.err.println(ex.toString());
397  throw ex;
398  }
String getInputSource(final SAXParseException ex)
Here is the call graph for this function:

◆ warning()

final void org.matsim.core.utils.io.MatsimXmlParser.warning ( final SAXParseException  ex) throws SAXException
inherited

Definition at line 401 of file MatsimXmlParser.java.

References org.matsim.core.utils.io.MatsimXmlParser.getInputSource().

401  {
402  System.err.println("XML-WARNING: " + getInputSource(ex) + ", line " + ex.getLineNumber() + ", column " + ex.getColumnNumber() + ":");
403  System.err.println(ex.getMessage());
404  }
String getInputSource(final SAXParseException ex)
Here is the call graph for this function:

◆ parseDouble()

static double org.matsim.core.utils.io.MatsimXmlParser.parseDouble ( String  value) throws NumberFormatException
staticinherited

Parses a String into a double, taking into account the special encoding for Infinity according to the xsd-specifications for the xs:double data type

Definition at line 426 of file MatsimXmlParser.java.

426  {
427  if ("INF".equals(value)) {
428  return Double.POSITIVE_INFINITY;
429  }
430  if ("-INF".equals(value)) {
431  return Double.NEGATIVE_INFINITY;
432  }
433  if ("NaN".equals(value)) {
434  return Double.NaN;
435  }
436  return Double.parseDouble(value);
437  }

Member Data Documentation

◆ log

final Logger org.matsim.counts.MatsimCountsReader.log = LogManager.getLogger(MatsimCountsReader.class)
staticprivate

Definition at line 44 of file MatsimCountsReader.java.

◆ COUNTS_V1

final String org.matsim.counts.MatsimCountsReader.COUNTS_V1 = "counts_v1.xsd"
staticprivate

Definition at line 45 of file MatsimCountsReader.java.

◆ COUNTS_V2

final String org.matsim.counts.MatsimCountsReader.COUNTS_V2 = "counts_v2.xsd"
staticprivate

Definition at line 46 of file MatsimCountsReader.java.

◆ counts

final Counts org.matsim.counts.MatsimCountsReader.counts
private

◆ inputCRS

final String org.matsim.counts.MatsimCountsReader.inputCRS
private

◆ targetCRS

final String org.matsim.counts.MatsimCountsReader.targetCRS
private

◆ delegate

MatsimXmlParser org.matsim.counts.MatsimCountsReader.delegate = null
private

Definition at line 50 of file MatsimCountsReader.java.

◆ idClass

final Class<? extends Identifiable<?> > org.matsim.counts.MatsimCountsReader.idClass
private

The documentation for this class was generated from the following file: