<commit_before>/**
 * Extract
 */
package org.jill.jn;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.jill.dma.DmaFile;
import org.jill.dma.DmaFileImpl;
import org.jill.jn.draw.DrawFile;
import org.jill.jn.draw.ScreenType;
import org.jill.jn.dump.DumpFile;
import org.jill.sha.ShaFile;
import org.jill.sha.ShaFileImpl;

/**
 * Extractor tool
 *
 * @author <NAME>
 */
public class JnFileExtractor {
    /**
     * automatically generate the help statement
     */
    private static final HelpFormatter formatter = new HelpFormatter() ;

    /**
     * create Options object
     */
    private static final Options options = new Options() ;

    static
    {
        createOptions() ;
    }

    public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException
    {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy k:m:s:S");
//
//        System.out.println(dateFormat.format( new Date() ) );

        // http://commons.apache.org/cli/usage.html
        CommandLineParser parser = new PosixParser() ;
        CommandLine cmd = parser.parse(options, args) ;

        boolean dumpText = false ;
        boolean drawMap = false ;

        final ExtractParameter param = new ExtractParameter() ;

        // Commands
        if (!cmd.hasOption('f'))
        {
            // automatically generate the help statement
            formatter.printHelp(JnFileExtractor.class.getCanonicalName(), options) ;

            return ;
        }

        if (cmd.hasOption("db") || cmd.hasOption("do") || cmd.hasOption("dsv") || cmd.hasOption("ds"))
        {
            param.setDumpback(cmd.hasOption("db")) ;
            param.setDumpobject(cmd.hasOption("do")) ;
            param.setDumpsave(cmd.hasOption("dsv")) ;
            param.setDumpstring(cmd.hasOption("ds")) ;

            dumpText = true ;
        }

        if (cmd.hasOption("drb") || cmd.hasOption("dro"))
        {
            param.setDrawback(cmd.hasOption("drb")) ;
            param.setDrawobject(cmd.hasOption("dro")) ;

            drawMap = true ;
        }

        param.setFile(cmd.getOptionValue('f')) ;
        param.setFileDma(cmd.getOptionValue("fd")) ;
        param.setFileSha(cmd.getOptionValue("fs")) ;
        param.setOutText(cmd.getOptionValue("ot")) ;
        param.setOutDraw(cmd.getOptionValue("od")) ;
        param.setDrawunknowobject(cmd.hasOption("dru")) ;

        if (cmd.hasOption("c") || cmd.hasOption("e") || cmd.hasOption("v"))
        {
            param.setCga(cmd.hasOption("c")) ;
            param.setEga(cmd.hasOption("e")) ;
            param.setVga(cmd.hasOption("v")) ;
        }

        if (dumpText)
        {
            extractDump(param) ;
        }

        if (drawMap)
        {
            if (!cmd.hasOption("fd"))
            {
                System.err.println("ERROR : Missing option 'file-dma'. DMA file required for draw map.");

                return ;
            }

            if (!cmd.hasOption("fs"))
            {
                System.err.println("ERROR : Missing option 'file-sha'. SHA file required for draw map.");

                return ;
            }

            extractMap(param) ;
        }

//        System.out.println(dateFormat.format( new Date() ) );
    }

    /**
     * Create options of command line
     */
    private static void createOptions()
    {
        // add t option
        options.addOption("f", "file", true, "file to read (map) to read (*.jn?).") ;
        options.addOption("fs", "file-sha", true, "file to read for get picture (*.sha).") ;
        options.addOption("fd", "file-dma", true, "file to read for get map picture (*.dma).") ;
        options.addOption("ot", "out-text", true, "output file write dump data.") ;
        options.addOption("od", "out-draw", true, "output file write draw map.") ;
        options.addOption("drb", "draw-back", false, "draw background layer.") ;
        options.addOption("dro", "draw-object", false, "draw object layer.") ;
        options.addOption("dru", "draw-object-unknow", false, "draw all unknow object in object layer.") ;
        options.addOption("db", "dump-back", false, "dump background layer.") ;
        options.addOption("do", "dump-object", false, "dump object layer.") ;
        options.addOption("dsv", "dump-save", false, "dump save data layer.") ;
        options.addOption("ds", "dump-string", false, "dump string stack layer.") ;
        options.addOption("c", "cga", false, "draw in CGA mode.") ;
        options.addOption("e", "ega", false, "draw in EGA mode.") ;
        options.addOption("v", "vga", false, "draw in VGA mode.") ;

        options.addOption("h", "help", false, "print this message.") ;
    }


    private static void extractDump(final ExtractParameter param) {
        // File to read
        JnFile jnFile = null ;
        // Dump utility
        DumpFile dumpFile  = null ;

        try {
            dumpFile = new DumpFile(param.getOutText()) ;

            jnFile = new JnFileImpl();
            jnFile.load(param.getFile()) ;

            if (param.isDumpback())
            {
                dumpFile.extractBackgroundInTextFile(jnFile) ;
            }

            if (param.isDumpobject())
            {
                dumpFile.extractObjectInTextFile(jnFile) ;<commit_msg>Fix java code typo.<commit_after>/**
 * Extract
 */
package org.jill.jn;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.jill.dma.DmaFile;
import org.jill.dma.DmaFileImpl;
import org.jill.jn.draw.DrawFile;
import org.jill.jn.draw.ScreenType;
import org.jill.jn.dump.DumpFile;
import org.jill.sha.ShaFile;
import org.jill.sha.ShaFileImpl;

/**
 * Extractor tool
 *
 * @author <NAME>
 */
public class JnFileExtractor {
    /**
     * automatically generate the help statement
     */
    private static final HelpFormatter FORMATTER = new HelpFormatter() ;

    /**
     * create Options object
     */
    private static final Options OPTIONS = new Options() ;

    static
    {
        createOptions() ;
    }

    public static void main(String[] args) throws ParseException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException
    {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy k:m:s:S");
//
//        System.out.println(dateFormat.format( new Date() ) );

        // http://commons.apache.org/cli/usage.html
        CommandLineParser parser = new PosixParser() ;
        CommandLine cmd = parser.parse(OPTIONS, args) ;

        boolean dumpText = false ;
        boolean drawMap = false ;

        final ExtractParameter param = new ExtractParameter() ;

        // Commands
        if (!cmd.hasOption('f'))
        {
            // automatically generate the help statement
            FORMATTER.printHelp(JnFileExtractor.class.getCanonicalName(), OPTIONS) ;

            return ;
        }

        if (cmd.hasOption("db") || cmd.hasOption("do") || cmd.hasOption("dsv") || cmd.hasOption("ds"))
        {
            param.setDumpback(cmd.hasOption("db")) ;
            param.setDumpobject(cmd.hasOption("do")) ;
            param.setDumpsave(cmd.hasOption("dsv")) ;
            param.setDumpstring(cmd.hasOption("ds")) ;

            dumpText = true ;
        }

        if (cmd.hasOption("drb") || cmd.hasOption("dro"))
        {
            param.setDrawback(cmd.hasOption("drb")) ;
            param.setDrawobject(cmd.hasOption("dro")) ;

            drawMap = true ;
        }

        param.setFile(cmd.getOptionValue('f')) ;
        param.setFileDma(cmd.getOptionValue("fd")) ;
        param.setFileSha(cmd.getOptionValue("fs")) ;
        param.setOutText(cmd.getOptionValue("ot")) ;
        param.setOutDraw(cmd.getOptionValue("od")) ;
        param.setDrawunknowobject(cmd.hasOption("dru")) ;

        if (cmd.hasOption("c") || cmd.hasOption("e") || cmd.hasOption("v"))
        {
            param.setCga(cmd.hasOption("c")) ;
            param.setEga(cmd.hasOption("e")) ;
            param.setVga(cmd.hasOption("v")) ;
        }

        if (dumpText)
        {
            extractDump(param) ;
        }

        if (drawMap)
        {
            if (!cmd.hasOption("fd"))
            {
                System.err.println("ERROR : Missing option 'file-dma'. DMA file required for draw map.");

                return ;
            }

            if (!cmd.hasOption("fs"))
            {
                System.err.println("ERROR : Missing option 'file-sha'. SHA file required for draw map.");

                return ;
            }

            extractMap(param) ;
        }

//        System.out.println(dateFormat.format( new Date() ) );
    }

    /**
     * Create options of command line
     */
    private static void createOptions()
    {
        // add t option
        OPTIONS.addOption("f", "file", true, "file to read (map) to read (*.jn?).") ;
        OPTIONS.addOption("fs", "file-sha", true, "file to read for get picture (*.sha).") ;
        OPTIONS.addOption("fd", "file-dma", true, "file to read for get map picture (*.dma).") ;
        OPTIONS.addOption("ot", "out-text", true, "output file write dump data.") ;
        OPTIONS.addOption("od", "out-draw", true, "output file write draw map.") ;
        OPTIONS.addOption("drb", "draw-back", false, "draw background layer.") ;
        OPTIONS.addOption("dro", "draw-object", false, "draw object layer.") ;
        OPTIONS.addOption("dru", "draw-object-unknow", false, "draw all unknow object in object layer.") ;
        OPTIONS.addOption("db", "dump-back", false, "dump background layer.") ;
        OPTIONS.addOption("do", "dump-object", false, "dump object layer.") ;
        OPTIONS.addOption("dsv", "dump-save", false, "dump save data layer.") ;
        OPTIONS.addOption("ds", "dump-string", false, "dump string stack layer.") ;
        OPTIONS.addOption("c", "cga", false, "draw in CGA mode.") ;
        OPTIONS.addOption("e", "ega", false, "draw in EGA mode.") ;
        OPTIONS.addOption("v", "vga", false, "draw in VGA mode.") ;

        OPTIONS.addOption("h", "help", false, "print this message.") ;
    }


    private static void extractDump(final ExtractParameter param) {
        // File to read
        JnFile jnFile = null ;
        // Dump utility
        DumpFile dumpFile  = null ;

        try {
            dumpFile = new DumpFile(param.getOutText()) ;

            jnFile = new JnFileImpl();
            jnFile.load(param.getFile()) ;

            if (param.isDumpback())
            {
                dumpFile.extractBackgroundInTextFile(jnFile) ;
            }

            if (param.isDumpobject())
            {
                dumpFile.extractObjectInTextFile(jnFile) ;
{'fraction_non_alphanumeric': 0.1422893481717011, 'fraction_numerical': 0.0001766472354707649, 'mean_word_length': 2.26970834536529, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1217975', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3309, 'n_words': 699}