<commit_before>/*
 * XMLParser.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.xml;

import dr.evolution.util.Units;
import dr.util.Identifiable;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import java.io.Reader;
import java.util.*;

public class XMLParser {

    public static final String ID = "id";
    public static final String IDREF = "idref";
    public static final String CONCURRENT = "concurrent";

    private Vector<Thread> threads = new Vector<Thread>();
    private boolean strictXML;

    public XMLParser(boolean strictXML) {
        this.strictXML = strictXML;
        addXMLObjectParser(new ArrayParser());
        addXMLObjectParser(Report.PARSER);
    }

    public XMLParser(boolean verbose, boolean strictXML) {
        this(strictXML);
        this.verbose = verbose;
    }

    public void addXMLObjectParser(XMLObjectParser parser) {

        String[] parserNames = parser.getParserNames();

        for (String parserName : parserNames) {
            XMLObjectParser oldParser = parserStore.get(parserName);
            if (oldParser != null) {
                throw new IllegalArgumentException("New parser ( " + parser + " ) cannot replace existing parser (" + oldParser + ")");
            }

            parserStore.put(parserName, parser);
        }
    }

    public Iterator getParserNames() {
        return parserStore.keySet().iterator();
    }

    public XMLObjectParser getParser(String name) {
        return parserStore.get(name);
    }

    public Iterator getParsers() {
        return parserStore.values().iterator();
    }

    public Iterator getThreads() {
        return threads.iterator();
    }

    public void storeObject(String name, Object object) {

        XMLObject xo = new XMLObject(null /*, objectStore*/);
        xo.setNativeObject(object);
        store.put(name, xo);
    }

    public ObjectStore parse(Reader reader, boolean run)
            throws java.io.IOException,
            org.xml.sax.SAXException,
            dr.xml.XMLParseException,
            javax.xml.parsers.ParserConfigurationException {

        InputSource in = new InputSource(reader);
        javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();

        javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(in);

        Element e = document.getDocumentElement();
        if (e.getTagName().equals("beast")) {

            concurrent = false;
            root = (XMLObject) convert(e, run, true);

        } else {
            throw new dr.xml.XMLParseException("Unknown root document element, " + e.getTagName());
        }

        return objectStore;
    }

    public XMLObject getRoot() {
        return root;
    }

    private Object convert(Element e, boolean run, boolean doParse) throws XMLParseException {


        if (e.hasAttribute(IDREF)) {
            String idref = e.getAttribute(IDREF);
            XMLObject restoredXMLObject = (XMLObject) store.get(idref);
            if (restoredXMLObject == null) {
                throw new XMLParseException("Object with idref=" + idref + " has not been previously declared.");
            }
            if (verbose) System.out.println("  Restoring idref=" + idref);

            return new Reference(restoredXMLObject);
        } else {
            int repeats = 1;
            if (e.getTagName().equals(CONCURRENT)) {
                if (concurrent) throw new XMLParseException("Nested concurrent elements not allowed.");
                concurrent = true;

                threads = new Vector<Thread>();
            } else if (e.getTagName().equals("repeat")) {
                repeats = Integer.parseInt(e.getAttribute("count"));
            }
<commit_msg>remove empty line<commit_after>/*
 * XMLParser.java
 *
 * Copyright (C) 2002-2006 <NAME> and <NAME>
 *
 * This file is part of BEAST.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership and licensing.
 *
 * BEAST is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 *  BEAST 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BEAST; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package dr.xml;

import dr.evolution.util.Units;
import dr.util.Identifiable;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import java.io.Reader;
import java.util.*;

public class XMLParser {

    public static final String ID = "id";
    public static final String IDREF = "idref";
    public static final String CONCURRENT = "concurrent";

    private Vector<Thread> threads = new Vector<Thread>();
    private boolean strictXML;

    public XMLParser(boolean strictXML) {
        this.strictXML = strictXML;
        addXMLObjectParser(new ArrayParser());
        addXMLObjectParser(Report.PARSER);
    }

    public XMLParser(boolean verbose, boolean strictXML) {
        this(strictXML);
        this.verbose = verbose;
    }

    public void addXMLObjectParser(XMLObjectParser parser) {

        String[] parserNames = parser.getParserNames();

        for (String parserName : parserNames) {
            XMLObjectParser oldParser = parserStore.get(parserName);
            if (oldParser != null) {
                throw new IllegalArgumentException("New parser ( " + parser + " ) cannot replace existing parser (" + oldParser + ")");
            }

            parserStore.put(parserName, parser);
        }
    }

    public Iterator getParserNames() {
        return parserStore.keySet().iterator();
    }

    public XMLObjectParser getParser(String name) {
        return parserStore.get(name);
    }

    public Iterator getParsers() {
        return parserStore.values().iterator();
    }

    public Iterator getThreads() {
        return threads.iterator();
    }

    public void storeObject(String name, Object object) {

        XMLObject xo = new XMLObject(null /*, objectStore*/);
        xo.setNativeObject(object);
        store.put(name, xo);
    }

    public ObjectStore parse(Reader reader, boolean run)
            throws java.io.IOException,
            org.xml.sax.SAXException,
            dr.xml.XMLParseException,
            javax.xml.parsers.ParserConfigurationException {

        InputSource in = new InputSource(reader);
        javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();

        javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(in);

        Element e = document.getDocumentElement();
        if (e.getTagName().equals("beast")) {

            concurrent = false;
            root = (XMLObject) convert(e, run, true);

        } else {
            throw new dr.xml.XMLParseException("Unknown root document element, " + e.getTagName());
        }

        return objectStore;
    }

    public XMLObject getRoot() {
        return root;
    }

    private Object convert(Element e, boolean run, boolean doParse) throws XMLParseException {

        if (e.hasAttribute(IDREF)) {
            String idref = e.getAttribute(IDREF);
            XMLObject restoredXMLObject = (XMLObject) store.get(idref);
            if (restoredXMLObject == null) {
                throw new XMLParseException("Object with idref=" + idref + " has not been previously declared.");
            }
            if (verbose) System.out.println("  Restoring idref=" + idref);

            return new Reference(restoredXMLObject);
        } else {
            int repeats = 1;
            if (e.getTagName().equals(CONCURRENT)) {
                if (concurrent) throw new XMLParseException("Nested concurrent elements not allowed.");
                concurrent = true;

                threads = new Vector<Thread>();
            } else if (e.getTagName().equals("repeat")) {
                repeats = Integer.parseInt(e.getAttribute("count"));
            }

{'fraction_non_alphanumeric': 0.09325878257200126, 'fraction_numerical': 0.00464183985652495, 'mean_word_length': 2.6349693251533743, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '3711996', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2406, 'n_words': 772}