XmlReportParser.java

package com.reallifedeveloper.maven.jdepend.xml;

import java.io.File;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.helpers.DefaultValidationEventHandler;

/**
 * A parser for XML files created by JDepend.
 *
 * @author RealLifeDeveloper
 */
public final class XmlReportParser {

    private final Unmarshaller unmarshaller;

    /**
     * Creates a new {@code XmlReportParser}.
     *
     * @throws JAXBException if creating the underlying JAXB unmarshaller fails
     */
    public XmlReportParser() throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(XmlReport.class);
        this.unmarshaller = jaxbContext.createUnmarshaller();
        this.unmarshaller.setEventHandler(new DefaultValidationEventHandler());
    }

    /**
     * Parses a file with XML generated by JDepend and creates the corresponding {@link XmlReport}.
     *
     * @param file the location of the JDepend XML file to parse
     *
     * @return an {@link XmlReport} representing the information in the XML file
     *
     * @throws JAXBException if any unepxected errors occur while parsing
     */
    public XmlReport parse(File file) throws JAXBException {
        return (XmlReport) unmarshaller.unmarshal(file);
    }
}