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

Public Member Functions

 HouseholdsStreamingReaderV10 (HouseholdsAlgorithmRunner algoRunner)
 
void endTag (String name, String content, Stack< String > context)
 
void readFileRunAlgorithmsAndWriteFile (String inputFile, String outputFile)
 
void putAttributeConverter (final Class<?> clazz, AttributeConverter<?> converter)
 
void putAttributeConverters (final Map< Class<?>, AttributeConverter<?>> converters)
 
void startTag (String name, Attributes atts, 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

HouseholdsWriterV10 hhWriter
 
HouseholdsAlgorithmRunner algoRunner
 
final Counter counter = new Counter (" household # ")
 

Static Private Attributes

static final Logger log = LogManager.getLogger(HouseholdsStreamingReaderV10.class)
 

Detailed Description

Author
dgrether

Definition at line 34 of file HouseholdsStreamingReaderV10.java.

Constructor & Destructor Documentation

◆ HouseholdsStreamingReaderV10()

org.matsim.households.HouseholdsStreamingReaderV10.HouseholdsStreamingReaderV10 ( HouseholdsAlgorithmRunner  algoRunner)

Definition at line 44 of file HouseholdsStreamingReaderV10.java.

References org.matsim.households.HouseholdsStreamingReaderV10.algoRunner.

44  {
45  super(new HouseholdsImpl());
46  this.algoRunner = algoRunner;
47  }

Member Function Documentation

◆ endTag()

void org.matsim.households.HouseholdsStreamingReaderV10.endTag ( String  name,
String  content,
Stack< String >  context 
)

Definition at line 50 of file HouseholdsStreamingReaderV10.java.

References org.matsim.households.HouseholdsSchemaV10Names.HOUSEHOLD, org.matsim.core.utils.misc.Counter.incCounter(), and org.matsim.households.HouseholdsAlgorithmRunner.runAlgorithms().

50  {
51  if (HouseholdsSchemaV10Names.HOUSEHOLD.equalsIgnoreCase(name)) {
52  Household household = createHousehold();
53  this.algoRunner.runAlgorithms(household);
54  this.hhWriter.writeHousehold(household);
56  }
57  }
Here is the call graph for this function:

◆ readFileRunAlgorithmsAndWriteFile()

void org.matsim.households.HouseholdsStreamingReaderV10.readFileRunAlgorithmsAndWriteFile ( String  inputFile,
String  outputFile 
)

Definition at line 59 of file HouseholdsStreamingReaderV10.java.

References org.matsim.core.utils.misc.Counter.printCounter().

59  {
60  if (inputFile.equalsIgnoreCase(outputFile)){
61  throw new IllegalArgumentException("Inputfile and outputfile must not refer to the same filename!");
62  }
63  hhWriter = new HouseholdsWriterV10(super.getHouseholds());
64  hhWriter.openFileAndWritePreamble(outputFile);
65  super.readFile(inputFile);
67  hhWriter.writeEndAndCloseFile();
68  log.info("Done reading households, running algorithms, and writing file");
69  }
Here is the call graph for this function:

◆ putAttributeConverter()

void org.matsim.households.AbstractHouseholdsReaderV10.putAttributeConverter ( final Class<?>  clazz,
AttributeConverter<?>  converter 
)
inherited

Definition at line 74 of file AbstractHouseholdsReaderV10.java.

74  {
75  attributesReader.putAttributeConverter( clazz , converter );
76  }

◆ putAttributeConverters()

void org.matsim.households.AbstractHouseholdsReaderV10.putAttributeConverters ( final Map< Class<?>, AttributeConverter<?>>  converters)
inherited

Definition at line 79 of file AbstractHouseholdsReaderV10.java.

79  {
80  attributesReader.putAttributeConverters( converters );
81  }

◆ startTag() [1/2]

void org.matsim.households.AbstractHouseholdsReaderV10.startTag ( String  name,
Attributes  atts,
Stack< String >  context 
)
inherited
See also
org.matsim.core.utils.io.MatsimXmlParser::startTag(java.lang.String, org.xml.sax.Attributes, java.util.Stack)

Definition at line 136 of file AbstractHouseholdsReaderV10.java.

136  {
137  if (HouseholdsSchemaV10Names.HOUSEHOLD.equalsIgnoreCase(name)) {
138  this.currentHhId = Id.create(atts.getValue(HouseholdsSchemaV10Names.ID), Household.class);
139  this.currentHousehold = this.builder.createHousehold(this.currentHhId);
140  this.currentmembers = new ArrayList<>();
141  this.currentVehicleIds = new ArrayList<>();
142  }
143  else if (HouseholdsSchemaV10Names.MEMBERS.equalsIgnoreCase(name)) {
144 // this.currentmembers = new ArrayList<Id>();
145  }
146  else if (HouseholdsSchemaV10Names.PERSONID.equalsIgnoreCase(name)) {
147  Id<Person> personId = Id.create(atts.getValue(HouseholdsSchemaV10Names.REFID), Person.class);
148  this.currentmembers.add(personId);
149  }
150  else if (HouseholdsSchemaV10Names.INCOME.equalsIgnoreCase(name)) {
151  this.currentIncomePeriod = getIncomePeriod(atts.getValue(HouseholdsSchemaV10Names.PERIOD));
152  this.currentincomeCurrency = atts.getValue(HouseholdsSchemaV10Names.CURRENCY);
153  }
154  else if (HouseholdsSchemaV10Names.VEHICLES.equalsIgnoreCase(name)){
155 // this.currentVehicleIds = new ArrayList<Id>();
156  }
157  else if (HouseholdsSchemaV10Names.VEHICLEDEFINITIONID.equalsIgnoreCase(name)) {
158  Id<Vehicle> vehicleId = Id.create(atts.getValue(HouseholdsSchemaV10Names.REFID), Vehicle.class);
159  this.currentVehicleIds.add(vehicleId);
160  }
161  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTES)) {
162  if (context.peek().equalsIgnoreCase(HouseholdsSchemaV10Names.HOUSEHOLD)) {
163  currAttributes = this.currentHousehold.getAttributes();
164  attributesReader.startTag( name , atts , context, currAttributes );
165  }
166  }
167  else if (name.equalsIgnoreCase(HouseholdsSchemaV10Names.ATTRIBUTE)) {
168  attributesReader.startTag( name , atts , context, currAttributes );
169  }
170  }

◆ 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  }

◆ setDoctype()

void org.matsim.core.utils.io.MatsimXmlParser.setDoctype ( final String  doctype)
protectedinherited

Definition at line 239 of file MatsimXmlParser.java.

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

Referenced by org.matsim.core.utils.io.MatsimXmlParser.resolveEntity().

239  {
240  // implementation of this method is what reacts to the different version of the file formats, so we cannot make it final. kai, jul'16
241 
242  this.doctype = doctype;
243  }

◆ 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.households.HouseholdsStreamingReaderV10.log = LogManager.getLogger(HouseholdsStreamingReaderV10.class)
staticprivate

Definition at line 37 of file HouseholdsStreamingReaderV10.java.

◆ hhWriter

HouseholdsWriterV10 org.matsim.households.HouseholdsStreamingReaderV10.hhWriter
private

Definition at line 39 of file HouseholdsStreamingReaderV10.java.

◆ algoRunner

HouseholdsAlgorithmRunner org.matsim.households.HouseholdsStreamingReaderV10.algoRunner
private

◆ counter

final Counter org.matsim.households.HouseholdsStreamingReaderV10.counter = new Counter (" household # ")
private

Definition at line 42 of file HouseholdsStreamingReaderV10.java.


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