<commit_before>package uk.ac.ebi.pride.utilities.data.io.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import uk.ac.ebi.jmzidml.xml.jaxb.unmarshaller.filters.MzIdentMLNamespaceFilter;
import uk.ac.ebi.pride.utilities.data.lightModel.MzIdentML;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * This class converts mzIdentML XML elements into Java objects(so-called unmarshalling) using Java Architecture for XML Binding (JAXB).
 * JAXB can be used with two approaches, which are namely DOM and SAX parsers. Unlike DOM, SAX does not construct a tree of objects
 * in the memory and SAX is faster. For this implementation, SAX parser has been used.
 * <p>
 * This class designed according to Singleton design pattern. Therefore, mzIdentML object cannot be directly created
 * from the constructor, but with the getInstance public method.
 *
 * @author <NAME>
 */
public class FastMzIdentMLUnmarshaller {

    private static final Logger logger = LoggerFactory.getLogger(FastMzIdentMLUnmarshaller.class);

    private MzIdentML mzIdentML = null;
    private JAXBContext jaxbContext;
    private static volatile FastMzIdentMLUnmarshaller fastMzIdentMLUnmarshaller = null;

    @SuppressWarnings("unchecked")
    private FastMzIdentMLUnmarshaller(File mzIdentMLFile) {

        try {
            if (mzIdentMLFile.exists()) {
                if (mzIdentML == null) {

                    //Prepare the input, in this case a java.io.File (output)
                    InputSource inputSource = new InputSource(new FileInputStream(mzIdentMLFile));

                    //required for the addition of namespaces to top-level objects
                    MzIdentMLNamespaceFilter xmlFilter = new MzIdentMLNamespaceFilter();

                    // Lazy caching of the JAXB Context.
                    if (jaxbContext == null) {
                        jaxbContext = JAXBContext.newInstance(MzIdentML.class.getPackage().getName());
                    }

                    //create unmarshaller(convert XML to Java objects)
                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

                    //Create an XMLReader to use with our filter
                    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                    xmlFilter.setParent(xmlReader);

                    //Create a SAXSource specifying the filter
                    SAXSource source = new SAXSource(xmlFilter, inputSource);

                    //Do unmarshalling
                    JAXBElement<MzIdentML> mzIdentMLJAXBElement = (JAXBElement<MzIdentML>) unmarshaller.unmarshal(source);

                    // get mzIdentML Java object
                    this.mzIdentML = mzIdentMLJAXBElement.getValue();
<commit_msg>code formatting<commit_after>package uk.ac.ebi.pride.utilities.data.io.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import uk.ac.ebi.jmzidml.xml.jaxb.unmarshaller.filters.MzIdentMLNamespaceFilter;
import uk.ac.ebi.pride.utilities.data.lightModel.MzIdentML;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * This class converts mzIdentML XML elements into Java objects(so-called unmarshalling) using Java Architecture for XML Binding (JAXB).
 * JAXB can be used with two approaches, which are namely DOM and SAX parsers. Unlike DOM, SAX does not construct a tree of objects
 * in the memory and SAX is faster. For this implementation, SAX parser has been used.
 * <p>
 * This class designed according to Singleton design pattern. Therefore, mzIdentML object cannot be directly created
 * from the constructor, but with the getInstance public method.
 *
 * @author <NAME>
 */
public class FastMzIdentMLUnmarshaller {

    private static final Logger logger = LoggerFactory.getLogger(FastMzIdentMLUnmarshaller.class);

    private MzIdentML mzIdentML = null;
    private JAXBContext jaxbContext;
    private static volatile FastMzIdentMLUnmarshaller fastMzIdentMLUnmarshaller = null;

    @SuppressWarnings("unchecked")
    private FastMzIdentMLUnmarshaller(File mzIdentMLFile) {
        try {
            if (mzIdentMLFile.exists()) {
                if (mzIdentML == null) {
                    InputSource inputSource = new InputSource(new FileInputStream(mzIdentMLFile));
                    //required for the addition of namespaces to top-level objects
                    MzIdentMLNamespaceFilter xmlFilter = new MzIdentMLNamespaceFilter();
                    // Lazy caching of the JAXB Context.
                    if (jaxbContext == null) {
                        jaxbContext = JAXBContext.newInstance(MzIdentML.class.getPackage().getName());
                    }
                    //create unmarshaller(convert XML to Java objects)
                    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                    //Create an XMLReader to use with our filter
                    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                    xmlFilter.setParent(xmlReader);
                    //Create a SAXSource specifying the filter
                    SAXSource source = new SAXSource(xmlFilter, inputSource);
                    //Do unmarshalling
                    JAXBElement<MzIdentML> mzIdentMLJAXBElement = (JAXBElement<MzIdentML>) unmarshaller.unmarshal(source);
                    // get mzIdentML Java object
                    this.mzIdentML = mzIdentMLJAXBElement.getValue();
{'fraction_non_alphanumeric': 0.07548701298701299, 'fraction_numerical': 0.0006493506493506494, 'mean_word_length': 2.786724031960664, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262911', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1708, 'n_words': 456}