21 package org.matsim.core.utils.io;
23 import java.io.IOException;
24 import java.io.UncheckedIOException;
25 import java.util.Locale;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.parsers.SAXParser;
29 import javax.xml.parsers.SAXParserFactory;
31 import org.apache.logging.log4j.LogManager;
32 import org.apache.logging.log4j.Logger;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.XMLReader;
37 import org.xml.sax.helpers.DefaultHandler;
51 Counts, Events, Households,
TransimsVehicle, OTFVis, SignalSystems, LaneDefinitions, SignalGroups, SignalControl, AmberTimes,
55 "Make sure that you try to read a xml document with a valid header. " +
56 "If your header seems to be ok, make shure that there is no / at the " +
57 "end of the first part of the tuple used as value for xsi:schemaLocation.";
65 String name = fileName.toLowerCase(Locale.ROOT);
66 if (name.endsWith(
".xml.gz") || name.endsWith(
".xml")) {
69 String shortSystemId = null;
70 if (this.xmlSystemId != null) {
71 shortSystemId = this.xmlSystemId.substring(this.xmlSystemId.replace(
'\\',
'/').lastIndexOf(
'/') + 1);
73 if (shortSystemId != null) {
74 if (shortSystemId.startsWith(
"network_")) {
76 }
else if (shortSystemId.startsWith(
"world_")) {
78 }
else if (shortSystemId.startsWith(
"plans_")) {
80 }
else if (shortSystemId.startsWith(
"population_")) {
82 }
else if (shortSystemId.startsWith(
"facilities_")) {
84 }
else if (shortSystemId.startsWith(
"config_")) {
86 }
else if (shortSystemId.startsWith(
"counts_")) {
88 }
else if (shortSystemId.startsWith(
"vehicleDefinitions_")) {
90 }
else if (shortSystemId.startsWith(
"transitSchedule_")) {
92 }
else if (shortSystemId.startsWith(
"objectattributes_")) {
97 }
else if (name.endsWith(
".txt.gz") || name.endsWith(
".txt")) {
99 }
else if (name.endsWith(
".mvi.gz") || name.endsWith(
".mvi")) {
101 }
else if (name.endsWith(
".veh.gz") || name.endsWith(
".veh")) {
127 SAXParserFactory factory = SAXParserFactory.newInstance();
128 factory.setValidating(
false);
129 factory.setNamespaceAware(
true);
133 factory.setFeature(
"http://apache.org/xml/features/validation/schema",
true);
134 SAXParser parser = factory.newSAXParser();
135 XMLReader reader = parser.getXMLReader();
136 reader.setContentHandler(handler);
137 reader.setErrorHandler(handler);
138 reader.setEntityResolver(handler);
140 }
catch (IOException e) {
141 throw new UncheckedIOException(e);
142 }
catch (SAXException e) {
143 throw new UncheckedIOException(
new IOException(e));
144 }
catch (ParserConfigurationException e) {
145 throw new UncheckedIOException(
new IOException (e));
149 log.debug(
"Detected public id: " + this.xmlPublicId);
150 log.debug(
"Detected system Id: " + this.xmlSystemId);
152 log.debug(
"Detected root tag: " + e.
rootTag);
153 if (
"events".equals(e.
rootTag)) {
155 }
else if (
"signalGroups".equals(e.
rootTag)) {
157 }
else if (
"signalSystems".equals(e.
rootTag)) {
159 }
else if (
"signalControl".equals(e.
rootTag)) {
161 }
else if (
"laneDefinitions".equals(e.
rootTag)) {
163 }
else if (
"counts".equals(e.
rootTag)) {
165 }
else if (
"transitSchedule".equals(e.
rootTag)) {
167 }
else if (
"objectAttributes".equals(e.
rootTag)) {
170 log.warn(
"got unexpected rootTag: " + e.
rootTag);
176 private final static class XmlHandler extends DefaultHandler {
187 public InputSource
resolveEntity(
final String publicId,
final String systemId) {
192 if (! this.detectedFirstEntity){
194 this.detectedFirstEntity =
true;
196 if (systemId.endsWith(
".dtd")){
203 public void startElement(
final String uri,
final String localName,
final String qName,
final Attributes atts)
throws SAXException {
204 String tag = (uri.length() == 0) ? qName : localName;
205 if (this.exception == null) {
221 private static final long serialVersionUID = 1L;
228 this.publicId = publicId;
229 this.systemId = systemId;
XMLTypeDetectionException exception
void startElement(final String uri, final String localName, final String qName, final Attributes atts)
static BufferedReader getBufferedReader(URL url, Charset charset)
synchronized Throwable fillInStackTrace()
FileType getGuessedFileType()
MatsimFileTypeGuesser(final String fileName)
void guessFileTypeXml(final String fileName)
boolean detectedFirstEntity
XMLTypeDetectionException(final String publicId, final String systemId)
static final String SYSTEMIDNOTFOUNDMESSAGE
InputSource resolveEntity(final String publicId, final String systemId)