<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package widgethelper;

import java.util.List;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

/**
 *
 * @author EricGummerson
 */
public class MainFrame extends javax.swing.JFrame {

    DefaultListModel list;
    int count;
    String[] widgetModelList;
    List<String> widgetStrings;
    String[] listString;
    int[] xInc, yInc, ioIds;
    boolean gen = false;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        count = 0;
        loadWidgets2();
        /*
         try {
         loadWidgets();
         } catch (IOException e) {
         System.out.println("Error trying to open widget files");
         e.printStackTrace();
         } catch (URISyntaxException ee) {
         System.out.println("Error with URI Syntax");
         ee.printStackTrace();
         }*/
    }

    private void loadWidgets() throws IOException, URISyntaxException {

        widgetStrings = new ArrayList<>();
        String path = "widgethelper/Widgets/";

        List<File> widgetList = Files.walk(Paths.get(this.getClass().getResource(path).toURI()))
                .filter(Files::isRegularFile)
                .map(Path::toFile)
                .collect(Collectors.toList());

        widgetModelList = new String[widgetList.size()];
        Scanner scan;
        for (File f : widgetList) {

            scan = new Scanner(f);
            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }

            scan.close();
            widgetStrings.add(totalString);
            widgetModelList[count] = f.getName();
            count++;

        }

        _ComboBox_WidgetList.setModel(new DefaultComboBoxModel(widgetModelList));
        _ComboBox_WidgetList.setSelectedIndex(5);

    }

    private void loadWidgets2() {
        boolean ide = true;
        String path = "Widgets/";
        ArrayList<String> results = new ArrayList<>();
        String dir = new File("").getAbsolutePath() + "/src/widgethelper/" + path;

        try {
            getWidgetFiles(dir, results);
            widgetStrings = new ArrayList<>();
            widgetModelList = new String[results.size()];
            results.stream().forEach((s) -> {
                try {
                    readWidgetNormal(s);
                } catch (FileNotFoundException e) {

                    System.out.println("File not found error " + e.getMessage());
                    System.out.println("Error trying to read " + s);
                }
            });
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            ide = false;
        }

        if (!ide) {
            CodeSource src = MainFrame.class.getProtectionDomain().getCodeSource();
            try {
                List<String> list = new ArrayList<>();
                if (src != null) {
                    URL jar = src.getLocation();
                    ZipInputStream zip = new ZipInputStream(jar.openStream());
                    ZipEntry ze = null;

                    while ((ze = zip.getNextEntry()) != null) {

                        String entryName = ze.getName();
                        if (entryName.startsWith("widgethelper/Widgets") && entryName.endsWith(".txt")) {

                            list.add("/" + entryName);
                            //System.out.println("Added name: " + entryName);
                        }
                    }
                    widgetStrings = new ArrayList<>();
                    widgetModelList = new String[list.size()];
                    list.stream().forEach((s) -> {
                        readJarFile(s);
                    });

                } else {
                    System.out.println("Src null");
                }

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        _ComboBox_WidgetList.setModel(new DefaultComboBoxModel(widgetModelList));
        _ComboBox_WidgetList.setSelectedIndex(5);

    }

    public void getWidgetFiles(String dirName, ArrayList<String> filePaths) throws FileNotFoundException {

        File directory = new File(dirName);

        if (!directory.exists()) {
            throw new FileNotFoundException("File Not Found: " + dirName);
        }

        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                filePaths.add(file.getName() + "," + file.getAbsolutePath());
            } else if (file.isDirectory()) {
                getWidgetFiles(file.getAbsolutePath(), filePaths);
            }
        }

    }

    public void readWidgetNormal(String result) throws FileNotFoundException {

        //System.out.println("Reading " + result);
        String[] ss = result.split(",");
        // s[0] -> File name
        // s[1] -> File path        

        String filename = ss[1];

        File f = new File(filename);
        try (Scanner scan = new Scanner(f)) {

            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }

            scan.close();
            widgetStrings.add(totalString);
            widgetModelList[count] = f.getName();
            count++;

        }

    }

    /**
     * Reads a a jar file and returns a list of strings which contain all the
     * variable names
     *
     * @param result the combination of files + file paths
     */
    public void readJarFile(String result) {

        String name = result.substring(result.lastIndexOf("/") + 1);

        InputStream loc = this.getClass().getResourceAsStream(result);
        try (Scanner scan = new Scanner(loc)) {

            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }
<commit_msg>Fixed reading text files in jar <commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package widgethelper;

import java.util.List;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

/**
 *
 * @author EricGummerson
 */
public class MainFrame extends javax.swing.JFrame {

    DefaultListModel list;
    int count;
    String[] widgetModelList;
    List<String> widgetStrings;
    String[] listString;
    int[] xInc, yInc, ioIds;
    boolean gen = false;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        count = 0;
        loadWidgets2();
        /*
         try {
         loadWidgets();
         } catch (IOException e) {
         System.out.println("Error trying to open widget files");
         e.printStackTrace();
         } catch (URISyntaxException ee) {
         System.out.println("Error with URI Syntax");
         ee.printStackTrace();
         }*/
    }

    private void loadWidgets() throws IOException, URISyntaxException {

        widgetStrings = new ArrayList<>();
        String path = "widgethelper/Widgets/";

        List<File> widgetList = Files.walk(Paths.get(this.getClass().getResource(path).toURI()))
                .filter(Files::isRegularFile)
                .map(Path::toFile)
                .collect(Collectors.toList());

        widgetModelList = new String[widgetList.size()];
        Scanner scan;
        for (File f : widgetList) {

            scan = new Scanner(f);
            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }

            scan.close();
            widgetStrings.add(totalString);
            widgetModelList[count] = f.getName();
            count++;

        }

        _ComboBox_WidgetList.setModel(new DefaultComboBoxModel(widgetModelList));
        _ComboBox_WidgetList.setSelectedIndex(5);

    }

    private void loadWidgets2() {
        boolean ide = true;
        String path = "Widgets/";
        ArrayList<String> results = new ArrayList<>();
        String dir = new File("").getAbsolutePath() + "/src/widgethelper/" + path;

        try {
            getWidgetFiles(dir, results);
            widgetStrings = new ArrayList<>();
            widgetModelList = new String[results.size()];
            results.stream().forEach((s) -> {
                try {
                    readWidgetNormal(s);
                } catch (FileNotFoundException e) {

                    System.out.println("File not found error " + e.getMessage());
                    System.out.println("Error trying to read " + s);
                }
            });
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            ide = false;
        }

        if (!ide) {
            CodeSource src = MainFrame.class.getProtectionDomain().getCodeSource();
            try {
                List<String> list = new ArrayList<>();
                if (src != null) {
                    URL jar = src.getLocation();
                    ZipInputStream zip = new ZipInputStream(jar.openStream());
                    ZipEntry ze = null;

                    while ((ze = zip.getNextEntry()) != null) {

                        String entryName = ze.getName();
                        if (entryName.startsWith("widgethelper/Widgets") && entryName.endsWith(".txt")) {

                            list.add("/" + entryName);
                            //System.out.println("Added name: " + entryName);
                        }
                    }
                    widgetStrings = new ArrayList<>();
                    widgetModelList = new String[list.size()];
                    list.stream().forEach((s) -> {
                        readJarFile(s);
                    });

                } else {
                    System.out.println("Src null");
                }

            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        _ComboBox_WidgetList.setModel(new DefaultComboBoxModel(widgetModelList));
        _ComboBox_WidgetList.setSelectedIndex(5);

    }

    public void getWidgetFiles(String dirName, ArrayList<String> filePaths) throws FileNotFoundException {

        File directory = new File(dirName);

        if (!directory.exists()) {
            throw new FileNotFoundException("File Not Found: " + dirName);
        }

        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                filePaths.add(file.getName() + "," + file.getAbsolutePath());
            } else if (file.isDirectory()) {
                getWidgetFiles(file.getAbsolutePath(), filePaths);
            }
        }

    }

    public void readWidgetNormal(String result) throws FileNotFoundException {

        //System.out.println("Reading " + result);
        String[] ss = result.split(",");
        // s[0] -> File name
        // s[1] -> File path        

        String filename = ss[1];

        File f = new File(filename);
        try (Scanner scan = new Scanner(f)) {

            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }

            scan.close();
            widgetStrings.add(totalString);
            widgetModelList[count] = f.getName();
            count++;

        }

    }

    /**
     * Reads a a jar file and returns a list of strings which contain all the
     * variable names
     *
     * @param result the combination of files + file paths
     */
    public void readJarFile(String result) {

        String name = result.substring(result.lastIndexOf("/") + 1);

        InputStream loc = this.getClass().getResourceAsStream(result);
        try (Scanner scan = new Scanner(loc, "UTF-8")) {

            String totalString = "";
            while (scan.hasNextLine()) {
                totalString += scan.nextLine();
            }

{'fraction_non_alphanumeric': 0.11505897677111224, 'fraction_numerical': 0.0013749185903466242, 'mean_word_length': 1.8869855859619804, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, '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': '4616541', 'n_tokens_mistral': 3685, 'n_tokens_neox': 3527, 'n_words': 827}