<commit_before>/*******************************************************************************
 * Copyright (c) 2018 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.opibuilder.converter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.csstudio.opibuilder.converter.writer.OpiWriter;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

/** RCP command line application for ADL file converter
 *
 *  <p>To use, start product like this:
 *
 *  <code>
 *  css -nosplash
 *      -application org.csstudio.opibuilder.converter.edl
 *      /path/to/file1.edl
 *      /path/to/file2.edl
 *  </code>
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class ConverterApplication implements IApplication
{
    private List<File> inputFiles = new ArrayList<>();
    private Optional<File> outputDirectory = Optional.empty();

    private static String renameEdlToOpi(String edlFileName)
    {
          return edlFileName.endsWith(".edl")
              ? edlFileName.substring(0, edlFileName.length()-4) + ".opi"
              : edlFileName + ".opi";
    }

    private void usage() {
        System.out.println("EDM Converter: convert all .edl files to .opi files at the same location.");
        System.out.println("Usage: <converter-cmd> [-h] [-o <output-dir>] edl-file ...");
        System.out.println("       -h: print this help and exit");
        System.out.println("       -o <output-dir>: place all converted opi files in output-dir.");
    }

    private void parseArguments(String[] args) {
        for (int i=0; i<args.length; ++i)
        {
            if (args[i].equals("-h"))
            {
                usage();
                System.exit(0);
            }
            else if (args[i].equals("-o"))
            {
                outputDirectory = Optional.of(new File(args[i+1]));
                ++i;
            }
            else if (args[i].startsWith("-"))
            {
                if (i+1 < args.length)
                {
                    System.out.println("Ignoring argument " + args[i] + " " + args[i+1]);
                    ++i;
                }
                else
                    System.out.println("Ignoring argument " + args[i]);
            }
            else
                inputFiles.add(new File(args[i]));
        }
    }

    @Override
    public Object start(final IApplicationContext context) throws Exception
    {
        System.out.println("************************");
        System.out.println("** EDL File Converter **");
        System.out.println("************************");

        final String args[] =
                (String []) context.getArguments().get("application.args");
        parseArguments(args);
        if (outputDirectory.isPresent() && ! outputDirectory.get().exists())
        {
            System.out.println("ERROR: Output directory " + outputDirectory.get() + " does not exist.");
            System.exit(-1);
        }
        try
        {
            if (! checkThenConvert())
                System.exit(-1);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.exit(-1);
        }

        return IApplication.EXIT_OK;
    }

    private boolean checkThenConvert() throws Exception
    {
        final List<File> inputs = new ArrayList<>();
        final List<File> outputs = new ArrayList<>();
        for (File input : inputFiles)
        {<commit_msg>EDM converter app: print error if no input files specified.<commit_after>/*******************************************************************************
 * Copyright (c) 2018 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.opibuilder.converter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.csstudio.opibuilder.converter.writer.OpiWriter;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

/** RCP command line application for ADL file converter
 *
 *  <p>To use, start product like this:
 *
 *  <code>
 *  css -nosplash
 *      -application org.csstudio.opibuilder.converter.edl
 *      /path/to/file1.edl
 *      /path/to/file2.edl
 *  </code>
 *
 *  @author <NAME>
 */
@SuppressWarnings("nls")
public class ConverterApplication implements IApplication
{
    private List<File> inputFiles = new ArrayList<>();
    private Optional<File> outputDirectory = Optional.empty();

    private static String renameEdlToOpi(String edlFileName)
    {
          return edlFileName.endsWith(".edl")
              ? edlFileName.substring(0, edlFileName.length()-4) + ".opi"
              : edlFileName + ".opi";
    }

    private void usage() {
        System.out.println("EDM Converter: convert all .edl files to .opi files at the same location.");
        System.out.println("Usage: <converter-cmd> [-h] [-o <output-dir>] edl-file ...");
        System.out.println("       -h: print this help and exit");
        System.out.println("       -o <output-dir>: place all converted opi files in output-dir.");
    }

    private void parseArguments(String[] args) {
        for (int i=0; i<args.length; ++i)
        {
            if (args[i].equals("-h"))
            {
                usage();
                System.exit(0);
            }
            else if (args[i].equals("-o"))
            {
                outputDirectory = Optional.of(new File(args[i+1]));
                ++i;
            }
            else if (args[i].startsWith("-"))
            {
                if (i+1 < args.length)
                {
                    System.out.println("Ignoring argument " + args[i] + " " + args[i+1]);
                    ++i;
                }
                else
                    System.out.println("Ignoring argument " + args[i]);
            }
            else
                inputFiles.add(new File(args[i]));
        }
    }

    @Override
    public Object start(final IApplicationContext context) throws Exception
    {
        System.out.println("************************");
        System.out.println("** EDL File Converter **");
        System.out.println("************************");

        final String args[] =
                (String []) context.getArguments().get("application.args");
        parseArguments(args);
        if (outputDirectory.isPresent() && ! outputDirectory.get().exists())
        {
            System.out.println("ERROR: Output directory " + outputDirectory.get() + " does not exist.");
            System.exit(-1);
        }
        if (inputFiles.isEmpty())
        {
            System.out.println("ERROR: No input files specified.");
            System.exit(-1);
        }
        try
        {
            if (! checkThenConvert())
                System.exit(-1);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.exit(-1);
        }

        return IApplication.EXIT_OK;
    }

    private boolean checkThenConvert() throws Exception
    {
        final List<File> inputs = new ArrayList<>();
        final List<File> outputs = new ArrayList<>();
        for (File input : inputFiles)
        {
{'fraction_non_alphanumeric': 0.17342142040401473, 'fraction_numerical': 0.0052089950451022745, 'mean_word_length': 2.1176237623762377, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1897640', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2085, 'n_words': 503}