<commit_before>package proclipsing.core.createproject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Vector;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import proclipsing.core.Activator;
import proclipsing.os.OSHelperManager;
import proclipsing.processingprovider.ProcessingLibrary;
import proclipsing.processingprovider.ProcessingProvider;
import proclipsing.util.LogHelper;
import proclipsing.util.Util;


/**
 * This job does the work of creating a Processing Project
 */
public class CreateProcessingProjectJob extends WorkspaceModifyOperation {

	private static final String BIN_DIR                  = "bin";
	private static final String SRC_DIR                  = "src";
	private static final String LIB_DIR                  = "lib";
	private static final String PROJECT_NAME_PLACEHOLDER = "%project_name%";
	private static final String PACKAGE_NAME_PLACEHOLDER = "%package_name%";
	
	private Vector<IClasspathEntry> classpath_entries;
	private String package_name;
	private ProjectConfiguration configuration;
	private boolean is_app;
	
	public CreateProcessingProjectJob(ProjectConfiguration configuration) {
	    this.configuration = configuration;
	    classpath_entries = new Vector<IClasspathEntry>();
		is_app = configuration.isApp();
	}
	
	@Override
	protected void execute(IProgressMonitor monitor) throws CoreException,
			InvocationTargetException, InterruptedException {
		
		IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = wsroot.getProject(configuration.getProjectName());
		
		package_name = Util.projNametoPackage(configuration.getProjectName());
		
		project.create(monitor);
		project.open(monitor);
		
		addJavaNature(project, monitor);
		
		createBinFolder(project, monitor);
		createSrcFolder(project, monitor);
		createProjFolder(project, monitor);
		
		addProcessing(createLibFolder(project, monitor), monitor);
		addMyAppletSkeleton(project, monitor);
		setProjectClassPath(project, monitor);
		
	}

    private void addMyAppletSkeleton(IProject project, IProgressMonitor monitor) throws CoreException {
		IFolder srcDir = project.getFolder(SRC_DIR + "/" + package_name);
		IFile defaultApplet = srcDir.getFile(Util.strToCamelCase(configuration.getProjectName()) + ".java");
		
		URL url = Activator.getDefault().getBundle().getResource(
				"template/PAppletTemplate.tmpl");
		
		if(is_app){
			url = Activator.getDefault().getBundle().getResource(
				"template/PAppTemplate.tmpl");
		}
		
		try{
			String templateStr = Util.convertStreamToString(url.openStream());

			String newApplet = Util.replaceAllSubString(templateStr, PACKAGE_NAME_PLACEHOLDER, package_name);
			newApplet = Util.replaceAllSubString(newApplet, PROJECT_NAME_PLACEHOLDER, Util.strToCamelCase(configuration.getProjectName()));
			
			byte[] bArray = newApplet.getBytes();
			InputStream bais = new ByteArrayInputStream(bArray);
			
			defaultApplet.create(bais, true, monitor);
		} catch (Exception e) {
			LogHelper.LogError(e);
		}
	}
	
    
    /**
     * Add processing libs to the project & set up classpath
     * 
     * @param lib - IFolder where to move the files to
     * @param monitor
     */
    private void addProcessing(IFolder lib, IProgressMonitor monitor) {
        ProcessingLibrary[] libraries = ProcessingProvider.getLibraries(
                configuration.getProcessingPath(),
    	        configuration.getSelectedLibraries().toArray(
    	                new String[configuration.getSelectedLibraries().size()]));
        
        for(ProcessingLibrary library : libraries) {
            // create folder containing lib based on identifier
            IFolder libFolder = 
            	lib.getFolder(library.getIdentifier());
            try {
            	if (!libFolder.exists())
					libFolder.create(true, true, monitor);
			} catch (CoreException e1) {
				LogHelper.LogError(e1);
				continue;
			}

        	// get urls to all jar, zip, + native lib files
            URL[] urls = library.getUrls();
            if (urls == null) continue;
			addProcessingLibs(libFolder, urls, monitor);
        }
        
    }
    
    /**
     * Given urls to the libraries, move them to the right spot
     * then add the jars and zip files to the classpath
     * 
     * @param libFolder
     * @param libUrls
     * @param monitor
     */
    private void addProcessingLibs(
    		IFolder libFolder, URL[] libUrls, IProgressMonitor monitor){
        //vars used in url loop
        String filename; IFile libFile;            
        // go through urls, moving files into project and adding jars and zips to classpath
        for (URL url : libUrls) {
            filename = url.getPath().substring(url.getPath().lastIndexOf(OSHelperManager.getHelper().getFileSeparator()) + 1);
            try {
                libFile = libFolder.getFile(filename);<commit_msg>Windows fix for URL file seperator<commit_after>package proclipsing.core.createproject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Vector;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import proclipsing.core.Activator;
import proclipsing.os.OSHelperManager;
import proclipsing.processingprovider.ProcessingLibrary;
import proclipsing.processingprovider.ProcessingProvider;
import proclipsing.util.LogHelper;
import proclipsing.util.Util;


/**
 * This job does the work of creating a Processing Project
 */
public class CreateProcessingProjectJob extends WorkspaceModifyOperation {

	private static final String BIN_DIR                  = "bin";
	private static final String SRC_DIR                  = "src";
	private static final String LIB_DIR                  = "lib";
	private static final String PROJECT_NAME_PLACEHOLDER = "%project_name%";
	private static final String PACKAGE_NAME_PLACEHOLDER = "%package_name%";
	
	private Vector<IClasspathEntry> classpath_entries;
	private String package_name;
	private ProjectConfiguration configuration;
	private boolean is_app;
	
	public CreateProcessingProjectJob(ProjectConfiguration configuration) {
	    this.configuration = configuration;
	    classpath_entries = new Vector<IClasspathEntry>();
		is_app = configuration.isApp();
	}
	
	@Override
	protected void execute(IProgressMonitor monitor) throws CoreException,
			InvocationTargetException, InterruptedException {
		
		IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
		IProject project = wsroot.getProject(configuration.getProjectName());
		
		package_name = Util.projNametoPackage(configuration.getProjectName());
		
		project.create(monitor);
		project.open(monitor);
		
		addJavaNature(project, monitor);
		
		createBinFolder(project, monitor);
		createSrcFolder(project, monitor);
		createProjFolder(project, monitor);
		
		addProcessing(createLibFolder(project, monitor), monitor);
		addMyAppletSkeleton(project, monitor);
		setProjectClassPath(project, monitor);
		
	}

    private void addMyAppletSkeleton(IProject project, IProgressMonitor monitor) throws CoreException {
		IFolder srcDir = project.getFolder(SRC_DIR + "/" + package_name);
		IFile defaultApplet = srcDir.getFile(Util.strToCamelCase(configuration.getProjectName()) + ".java");
		
		URL url = Activator.getDefault().getBundle().getResource(
				"template/PAppletTemplate.tmpl");
		
		if(is_app){
			url = Activator.getDefault().getBundle().getResource(
				"template/PAppTemplate.tmpl");
		}
		
		try{
			String templateStr = Util.convertStreamToString(url.openStream());

			String newApplet = Util.replaceAllSubString(templateStr, PACKAGE_NAME_PLACEHOLDER, package_name);
			newApplet = Util.replaceAllSubString(newApplet, PROJECT_NAME_PLACEHOLDER, Util.strToCamelCase(configuration.getProjectName()));
			
			byte[] bArray = newApplet.getBytes();
			InputStream bais = new ByteArrayInputStream(bArray);
			
			defaultApplet.create(bais, true, monitor);
		} catch (Exception e) {
			LogHelper.LogError(e);
		}
	}
	
    
    /**
     * Add processing libs to the project & set up classpath
     * 
     * @param lib - IFolder where to move the files to
     * @param monitor
     */
    private void addProcessing(IFolder lib, IProgressMonitor monitor) {
        ProcessingLibrary[] libraries = ProcessingProvider.getLibraries(
                configuration.getProcessingPath(),
    	        configuration.getSelectedLibraries().toArray(
    	                new String[configuration.getSelectedLibraries().size()]));
        
        for(ProcessingLibrary library : libraries) {
            // create folder containing lib based on identifier
            IFolder libFolder = 
            	lib.getFolder(library.getIdentifier());
            try {
            	if (!libFolder.exists())
					libFolder.create(true, true, monitor);
			} catch (CoreException e1) {
				LogHelper.LogError(e1);
				continue;
			}

        	// get urls to all jar, zip, + native lib files
            URL[] urls = library.getUrls();
            if (urls == null) continue;
			addProcessingLibs(libFolder, urls, monitor);
        }
        
    }
    
    /**
     * Given urls to the libraries, move them to the right spot
     * then add the jars and zip files to the classpath
     * 
     * @param libFolder
     * @param libUrls
     * @param monitor
     */
    private void addProcessingLibs(
    		IFolder libFolder, URL[] libUrls, IProgressMonitor monitor){
        //vars used in url loop
        String filename; IFile libFile;            
        // go through urls, moving files into project and adding jars and zips to classpath
        for (URL url : libUrls) {
//            filename = url.getPath().substring(url.getPath().lastIndexOf(OSHelperManager.getHelper().getFileSeparator()) + 1);
//        	apprently, Java uses "/" instead of the OS file seperator, even on windows.
            filename = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
            try {
                libFile = libFolder.getFile(filename);
{'fraction_non_alphanumeric': 0.09429710521754203, 'fraction_numerical': 0.0006066909343040389, 'mean_word_length': 3.709795918367347, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7324685', 'n_tokens_mistral': 3441, 'n_tokens_neox': 3187, 'n_words': 729}