<commit_before>/*  Copyright (C) 2003-2015 JabRef contributors.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.importer.fileformat;

import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.*;

import net.sf.jabref.*;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.model.database.KeyCollisionException;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class for importing BibTeX-files.
 * <p>
 * Use:
 * <p>
 * BibtexParser parser = new BibtexParser(reader);
 * <p>
 * ParserResult result = parser.parse();
 * <p>
 * or
 * <p>
 * ParserResult result = BibtexParser.parse(reader);
 * <p>
 * Can be used stand-alone.
 */
public class BibtexParser {
    private static final Log LOGGER = LogFactory.getLog(BibtexParser.class);

    private final PushbackReader pushbackReader;
    private BibDatabase database;
    private Map<String, EntryType> entryTypes;
    private boolean eof;
    private int line = 1;
    private final FieldContentParser fieldContentParser = new FieldContentParser();
    private ParserResult parserResult;
    private static final Integer LOOKAHEAD = 64;
    private final boolean autoDoubleBraces;
    private final Deque<Character> pureTextFromFile = new LinkedList<>();


    public BibtexParser(Reader in) {
        Objects.requireNonNull(in);

        if (Globals.prefs == null) {
            Globals.prefs = JabRefPreferences.getInstance();
        }
        autoDoubleBraces = Globals.prefs.getBoolean(JabRefPreferences.AUTO_DOUBLE_BRACES);
        pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
    }

    /**
     * Shortcut usage to create a Parser and read the input.<commit_msg>Remove preferences from the parser<commit_after>/*  Copyright (C) 2003-2015 JabRef contributors.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.importer.fileformat;

import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.*;

import net.sf.jabref.*;
import net.sf.jabref.logic.CustomEntryTypesManager;
import net.sf.jabref.model.database.KeyCollisionException;
import net.sf.jabref.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.strings.StringUtil;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class for importing BibTeX-files.
 * <p>
 * Use:
 * <p>
 * BibtexParser parser = new BibtexParser(reader);
 * <p>
 * ParserResult result = parser.parse();
 * <p>
 * or
 * <p>
 * ParserResult result = BibtexParser.parse(reader);
 * <p>
 * Can be used stand-alone.
 */
public class BibtexParser {
    private static final Log LOGGER = LogFactory.getLog(BibtexParser.class);

    private final PushbackReader pushbackReader;
    private BibDatabase database;
    private Map<String, EntryType> entryTypes;
    private boolean eof;
    private int line = 1;
    private final FieldContentParser fieldContentParser = new FieldContentParser();
    private ParserResult parserResult;
    private static final Integer LOOKAHEAD = 64;
    private final Deque<Character> pureTextFromFile = new LinkedList<>();


    public BibtexParser(Reader in) {
        Objects.requireNonNull(in);

        pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
    }

    /**
     * Shortcut usage to create a Parser and read the input.
{'fraction_non_alphanumeric': 0.08474903474903475, 'fraction_numerical': 0.009652509652509652, 'mean_word_length': 3.8647887323943664, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3227841', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1452, 'n_words': 500}