averbraeck / opentrafficsim

Open Source Multi-Level Traffic Simulator
BSD 3-Clause "New" or "Revised" License
30 stars 11 forks source link

Imports in XML not parsed #26

Closed WJSchakel closed 1 year ago

WJSchakel commented 1 year ago

When parsing from XML, the included definitions such as GTU types, are not parsed. Current simulations are running by the grace of OtsNetworks being created with default types, and all those simulations using default types. The created types happen to create a whole separate set of types, who's id's and parent structure matches with the default types. In overview:

Imported types (default) from XML: not generated Default types in code: used in simulations Default types in OtsNetwork: created with matching id's and parent structure

WJSchakel commented 1 year ago

I'm wondering whether the spf variable in the code below (from XmlNetworkLaneParser) does anything. There seems to be no relation with 'jc' or 'unmarshaller'.

        JAXBContext jc = JAXBContext.newInstance(OTS.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setXIncludeAware(true);
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        return (OTS) unmarshaller.unmarshal(xmlURL);
averbraeck commented 1 year ago

Good question. It might set a number of parameters that are used in the parsing, but the way it has been coded it looks like it is doing nothing indeed. I will check.

averbraeck commented 1 year ago

The way it is programmed right now, it does not do anything because it is indeed not connected. The SaxParserFactory part can be safely deleted as far as I can see. In case we want to use a SaxReader with the JAXB contexts at some point, we could use the tips in https://stackoverflow.com/questions/10890323/using-sax-with-jaxbcontext. Right now that does not seem needed.

WJSchakel commented 1 year ago

There is a parseXML(final URL xmlURL) method, in which the includes do not work. It has the above code. There is also parseXML(final InputStream xmlStream) in which the includes do work. It has very similar code, but explicitly uses the SAXParserFactory part. See TrafCodDemo2 on how the working method is used.

WJSchakel commented 1 year ago

SAXSource saxSource = new SAXSource(xmlReader, new InputSource(xmlURL.openStream())); seems to do the trick. It translates the URL in to an InputStream.

WJSchakel commented 1 year ago

Replacing the entire method with return parseXML(xmlURL.openStream()); fixed it.