<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.vxquery.xtest;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class TestCaseFactory {
    private TestConfiguration tConfig;
    private Map<String, File> srcMap;
    private String xqtsBase;
    private TestRunnerFactory trf;
    private ExecutorService eSvc;
    private TestCase tc;
    private Pattern include;
    private Pattern exclude;
    private XTestOptions opts;

    private String nextVariable;
    private boolean expectedError;
    private boolean outputFile;
    private int currPathLen;
    int count;

    public TestCaseFactory(String xqtsBase, TestRunnerFactory trf, ExecutorService eSvc, XTestOptions opts) {
        this.xqtsBase = xqtsBase;
        this.trf = trf;
        tConfig = new TestConfiguration();
        tConfig.options = opts;
        this.eSvc = eSvc;
        this.opts = opts;
        if (opts.include != null) {
            this.include = Pattern.compile(opts.include);
        }
        if (opts.exclude != null) {
            this.exclude = Pattern.compile(opts.exclude);
        }
        srcMap = new HashMap<String, File>();
        try {
            currPathLen = new File(".").getCanonicalPath().length();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int process() throws Exception {
        count = 0;
        XMLReader parser = XMLReaderFactory.createXMLReader();
        nextVariable = null;
        Handler handler = new Handler();
        parser.setContentHandler(handler);
        parser.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                URL url = new URL(systemId);
                return new InputSource(xqtsBase + new File(url.getFile()).getCanonicalPath().substring(currPathLen));
            }
        });
        parser.parse(new InputSource(new FileReader(new File(xqtsBase, "XQTSCatalog.xml"))));
        return count;
    }

    private void submit(TestCase tc) {
        boolean toSubmit = include == null || include.matcher(tc.getXQueryDisplayName()).find();
        toSubmit = exclude == null || !exclude.matcher(tc.getXQueryDisplayName()).find();
        if (toSubmit) {
            if (opts.verbose) {
                System.err.println(tc);
            }
            eSvc.submit(trf.createRunner(tc));
            ++count;
        }
    }

    private class Handler implements ContentHandler {
        StringBuilder buffer = null;

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (buffer == null) {
                buffer = new StringBuilder();
            }
            buffer.append(ch, start, length);
        }<commit_msg>- only execute included tests if no tests are excluded (used to execute all tests if no tests are excluded)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.vxquery.xtest;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class TestCaseFactory {
    private TestConfiguration tConfig;
    private Map<String, File> srcMap;
    private String xqtsBase;
    private TestRunnerFactory trf;
    private ExecutorService eSvc;
    private TestCase tc;
    private Pattern include;
    private Pattern exclude;
    private XTestOptions opts;

    private String nextVariable;
    private boolean expectedError;
    private boolean outputFile;
    private int currPathLen;
    int count;

    public TestCaseFactory(String xqtsBase, TestRunnerFactory trf, ExecutorService eSvc, XTestOptions opts) {
        this.xqtsBase = xqtsBase;
        this.trf = trf;
        tConfig = new TestConfiguration();
        tConfig.options = opts;
        this.eSvc = eSvc;
        this.opts = opts;
        if (opts.include != null) {
            this.include = Pattern.compile(opts.include);
        }
        if (opts.exclude != null) {
            this.exclude = Pattern.compile(opts.exclude);
        }
        srcMap = new HashMap<String, File>();
        try {
            currPathLen = new File(".").getCanonicalPath().length();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int process() throws Exception {
        count = 0;
        XMLReader parser = XMLReaderFactory.createXMLReader();
        nextVariable = null;
        Handler handler = new Handler();
        parser.setContentHandler(handler);
        parser.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                URL url = new URL(systemId);
                return new InputSource(xqtsBase + new File(url.getFile()).getCanonicalPath().substring(currPathLen));
            }
        });
        parser.parse(new InputSource(new FileReader(new File(xqtsBase, "XQTSCatalog.xml"))));
        return count;
    }

    private void submit(TestCase tc) {
        boolean toSubmit = include == null || include.matcher(tc.getXQueryDisplayName()).find();
        toSubmit = toSubmit && (exclude == null || !exclude.matcher(tc.getXQueryDisplayName()).find());
        if (toSubmit) {
            if (opts.verbose) {
                System.err.println(tc);
            }
            eSvc.submit(trf.createRunner(tc));
            ++count;
        }
    }

    private class Handler implements ContentHandler {
        StringBuilder buffer = null;

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (buffer == null) {
                buffer = new StringBuilder();
            }
            buffer.append(ch, start, length);
        }
{'fraction_non_alphanumeric': 0.09125060591371789, 'fraction_numerical': 0.0012118274357731458, 'mean_word_length': 2.756486117432863, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, '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': '3137733', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2176, 'n_words': 682}