All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.entur.netex.loader.NetexXmlParser Maven / Gradle / Ivy

There is a newer version: 3.1.29
Show newest version
package org.entur.netex.loader;

import org.rutebanken.netex.model.PublicationDeliveryStructure;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import java.io.InputStream;

/** Simple wrapper to perform typesafe xml parsing and simple error handling. */
public class NetexXmlParser {
    /** used to parse the XML. */
    private final Unmarshaller unmarshaller;

    public NetexXmlParser() {
        this.unmarshaller = createUnmarshaller();
    }

    /**
     * Parse an input stream and return the root document type for the given xml file (stream).
     */
    public PublicationDeliveryStructure parseXmlDoc(InputStream stream) throws JAXBException {

        @SuppressWarnings("unchecked")
        JAXBElement root = (JAXBElement) unmarshaller.unmarshal(stream);

        return root.getValue();
    }

    /** factory method for unmarshaller */
    private static Unmarshaller createUnmarshaller() {
        try {
            return JAXBContext
                    .newInstance(PublicationDeliveryStructure.class)
                    .createUnmarshaller();
        } catch (JAXBException e) {
            // This is a programming error - not expected!
            // We abort early and also allow for this to happen in the constructor;
            // Which in other cases would be considered bad practice.
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy