<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-2010 The eXist Project
 *  http://exist-db.org
 *
 *  This program 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.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */
package org.exist.backup;

import org.exist.EXistException;
import org.exist.storage.BrokerPool;
import org.exist.storage.DBBroker;
import org.exist.util.Configuration;
import org.exist.util.DatabaseConfigurationException;
import org.exist.util.SystemExitCodes;
import org.exist.xquery.TerminatedException;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import org.exist.security.PermissionDeniedException;
import se.softhouse.jargo.Argument;
import se.softhouse.jargo.ArgumentException;
import se.softhouse.jargo.CommandLineParser;
import se.softhouse.jargo.ParsedArguments;

import static org.exist.util.ArgumentUtil.getBool;
import static org.exist.util.ArgumentUtil.getOpt;
import static se.softhouse.jargo.Arguments.*;


public class ExportMain {

    /* general arguments */
    private static final Argument<?> helpArg = helpArgument("-h", "--help");
    private static final Argument<Boolean> verboseArg = optionArgument("-v", "--verbose")
            .description("print processed resources to stdout")
            .defaultValue(false)
            .build();

    /* control arguments */
    private static final Argument<Boolean> noCheckArg = optionArgument("-n", "--nocheck")
            .description("do not run a consistency check. Just export the data.")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> checkDocsArg = optionArgument("-s", "--check-docs")
            .description("scan every document to find errors in the the nodes stored (costs time)")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> directAccessArg = optionArgument("-D", "--direct")
            .description("use an (even more) direct access to the db, bypassing some index structures")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> exportArg = optionArgument("-x", "--export")
            .description("export database contents while preserving as much data as possible")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> incrementalArg = optionArgument("-i", "--incremental")
            .description("create incremental backup (use with --export|-x)")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> zipArg = optionArgument("-z", "--zip")
            .description("write output to a ZIP instead of a file system directory")
            .defaultValue(false)
            .build();

    /* export parameters */
    private static final Argument<File> configArg = fileArgument("-c", "--config")
            .description("the database configuration (conf.xml) file to use for launching the db.")
            .build();
    private static final Argument<File> outputDirArg = fileArgument("-d", "--dir")
            .description("the directory to which all output will be written.")
            .required()
            .defaultValue(Paths.get("export").toAbsolutePath().toFile())
            .build();

    protected static BrokerPool startDB(final Optional<Path> configFile) {
        try {
            Configuration config;

            if (configFile.isPresent()) {
                config = new Configuration(configFile.get().toAbsolutePath().toString(), Optional.empty());
            } else {
                config = new Configuration();
            }
            config.setProperty(BrokerPool.PROPERTY_EXPORT_ONLY, Boolean.TRUE);
            BrokerPool.configure(1, 5, config);
            return (BrokerPool.getInstance());
        } catch (final DatabaseConfigurationException | EXistException e) {
            System.err.println("ERROR: Failed to open database: " + e.getMessage());
        }
        return (null);
    }


    @SuppressWarnings("unchecked")
    public static void main(final String[] args) {
        try {
            final ParsedArguments arguments = CommandLineParser
                    .withArguments(noCheckArg, checkDocsArg, directAccessArg, exportArg, incrementalArg, zipArg)
                    .andArguments(configArg, outputDirArg)
                    .andArguments(helpArg, verboseArg)<commit_msg>[bugfix] Args cannot be both required and default. Closes https://github.com/eXist-db/exist/issues/1499<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-2010 The eXist Project
 *  http://exist-db.org
 *
 *  This program 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.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */
package org.exist.backup;

import org.exist.EXistException;
import org.exist.storage.BrokerPool;
import org.exist.storage.DBBroker;
import org.exist.util.Configuration;
import org.exist.util.DatabaseConfigurationException;
import org.exist.util.SystemExitCodes;
import org.exist.xquery.TerminatedException;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import org.exist.security.PermissionDeniedException;
import se.softhouse.jargo.Argument;
import se.softhouse.jargo.ArgumentException;
import se.softhouse.jargo.CommandLineParser;
import se.softhouse.jargo.ParsedArguments;

import static org.exist.util.ArgumentUtil.getBool;
import static org.exist.util.ArgumentUtil.getOpt;
import static se.softhouse.jargo.Arguments.*;


public class ExportMain {

    /* general arguments */
    private static final Argument<?> helpArg = helpArgument("-h", "--help");
    private static final Argument<Boolean> verboseArg = optionArgument("-v", "--verbose")
            .description("print processed resources to stdout")
            .defaultValue(false)
            .build();

    /* control arguments */
    private static final Argument<Boolean> noCheckArg = optionArgument("-n", "--nocheck")
            .description("do not run a consistency check. Just export the data.")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> checkDocsArg = optionArgument("-s", "--check-docs")
            .description("scan every document to find errors in the the nodes stored (costs time)")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> directAccessArg = optionArgument("-D", "--direct")
            .description("use an (even more) direct access to the db, bypassing some index structures")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> exportArg = optionArgument("-x", "--export")
            .description("export database contents while preserving as much data as possible")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> incrementalArg = optionArgument("-i", "--incremental")
            .description("create incremental backup (use with --export|-x)")
            .defaultValue(false)
            .build();
    private static final Argument<Boolean> zipArg = optionArgument("-z", "--zip")
            .description("write output to a ZIP instead of a file system directory")
            .defaultValue(false)
            .build();

    /* export parameters */
    private static final Argument<File> configArg = fileArgument("-c", "--config")
            .description("the database configuration (conf.xml) file to use for launching the db.")
            .build();
    private static final Argument<File> outputDirArg = fileArgument("-d", "--dir")
            .description("the directory to which all output will be written.")
            .defaultValue(Paths.get("export").toAbsolutePath().toFile())
            .build();

    protected static BrokerPool startDB(final Optional<Path> configFile) {
        try {
            Configuration config;

            if (configFile.isPresent()) {
                config = new Configuration(configFile.get().toAbsolutePath().toString(), Optional.empty());
            } else {
                config = new Configuration();
            }
            config.setProperty(BrokerPool.PROPERTY_EXPORT_ONLY, Boolean.TRUE);
            BrokerPool.configure(1, 5, config);
            return (BrokerPool.getInstance());
        } catch (final DatabaseConfigurationException | EXistException e) {
            System.err.println("ERROR: Failed to open database: " + e.getMessage());
        }
        return (null);
    }


    @SuppressWarnings("unchecked")
    public static void main(final String[] args) {
        try {
            final ParsedArguments arguments = CommandLineParser
                    .withArguments(noCheckArg, checkDocsArg, directAccessArg, exportArg, incrementalArg, zipArg)
                    .andArguments(configArg, outputDirArg)
                    .andArguments(helpArg, verboseArg)
{'fraction_non_alphanumeric': 0.10334608030592735, 'fraction_numerical': 0.004588910133843212, 'mean_word_length': 2.916510670160988, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3936206', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2625, 'n_words': 860}