<commit_before>/*******************************************************************************
 Copyright 2012 <NAME>

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.


     Read the included LICENSE.TXT for more information.
 ******************************************************************************/

package unquietcode.tools.flapi;

import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.writer.FileCodeWriter;
import com.sun.codemodel.writer.SingleStreamCodeWriter;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.FileChannel;

/**
 * @author <NAME>
 * @version 03-10-2012
 */
public class CodeWriter {

	public static void writeToStream(JCodeModel model, OutputStream stream) {
		SingleStreamCodeWriter writer = new SingleStreamCodeWriter(stream);

		try {
			model.build(writer);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}

		System.out.println(stream.toString());
	}

	public static void writeToDirectory(JCodeModel model, File directory) {
		try {
			FileCodeWriter fileWriter = new FileCodeWriter(directory, false);
			model.build(fileWriter);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static void writeRequiredClasses(File folder) {
		String path = Constants.getSupportPath(folder);
		File dir = new File(path);

		if (!dir.exists()) {
			boolean create = dir.mkdirs();
			if (!create) {
				throw new DescriptorBuilderException("Unable create output directory '"+path+"'.");
			}
		}

		for (String requiredFile : Constants.REQUIRED_FILES) {
			String fileName = requiredFile+".java";
			copyFile(getResourceFile(fileName), createFile(dir.getAbsolutePath(), fileName));
		}
	}

	//---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---//

	private static File getResourceFile(String name) {
		URL url = CodeWriter.class.getClassLoader().getResource(name);
		File f;

		if (url == null) {
			throw new DescriptorBuilderException("Cannot find file '"+name+"' (this is an internal error).");
		}

		try {
			f = new File(url.toURI());
		} catch (URISyntaxException ex) {
			throw new DescriptorBuilderException(ex);
		}

		if (!f.exists()) {
			throw new DescriptorBuilderException("Cannot find file '"+name+"' (this is an internal error).");
		}

		return f;
	}

	private static File createFile(String path, String name) {
		File file = new File(path+File.separator+name);

		if (file.exists()) {
			if (!file.delete()) {
				throw new DescriptorBuilderException("Could not remove old file '"+file.getAbsolutePath()+"'.");
			}
		}

		boolean create;
		try {
			create = file.createNewFile();
		} catch (IOException ex) {
			throw new DescriptorBuilderException("Error creating file.", ex);
		}

		if (!create) {
			throw new DescriptorBuilderException("Error creating file.");
		}

		return file;
	}

	private static void copyFile(File sourceFile, File destFile) {
		FileChannel source = null;
		FileChannel destination = null;

		try {
			source = new FileInputStream(sourceFile).getChannel();
			destination = new FileOutputStream(destFile).getChannel();
			destination.transferFrom(source, 0, source.size());
		} catch (Exception ex) {
			throw new DescriptorBuilderException("Error while writing files.", ex);
		} finally {
			if (source != null) {
				try {source.close();} catch (Exception ex) { /* nothing */ }
			}

			if (destination != null) {
				try {destination.close();} catch (Exception ex) { /* nothing */ }
			}
		}
	}
}<commit_msg>Fixes a bug which prevented support files from being read inside of the jar. (Pro tip: you cannot create a File object for a path inside of a jar. You must access it as a stream.)<commit_after>/*******************************************************************************
 Copyright 2012 <NAME>

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.


     Read the included LICENSE.TXT for more information.
 ******************************************************************************/

package unquietcode.tools.flapi;

import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.writer.FileCodeWriter;
import com.sun.codemodel.writer.SingleStreamCodeWriter;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.Scanner;

/**
 * @author <NAME>
 * @version 03-10-2012
 */
public class CodeWriter {

	public static void writeToStream(JCodeModel model, OutputStream stream) {
		SingleStreamCodeWriter writer = new SingleStreamCodeWriter(stream);

		try {
			model.build(writer);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}

		System.out.println(stream.toString());
	}

	public static void writeToDirectory(JCodeModel model, File directory) {
		try {
			FileCodeWriter fileWriter = new FileCodeWriter(directory, false);
			model.build(fileWriter);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	public static void writeRequiredClasses(File folder) {
		String path = Constants.getSupportPath(folder);
		File dir = new File(path);

		if (!dir.exists()) {
			boolean create = dir.mkdirs();
			if (!create) {
				throw new DescriptorBuilderException("Unable create output directory '"+path+"'.");
			}
		}

		for (String requiredFile : Constants.REQUIRED_FILES) {
			String fileName = requiredFile+".java";
			copyFile(getResourceFile(fileName), createFile(dir.getAbsolutePath(), fileName));
		}
	}

	//---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---o---//

	private static InputStream getResourceFile(String name) {
		InputStream stream = CodeWriter.class.getClassLoader().getResourceAsStream(name);

		if (stream == null) {
			throw new DescriptorBuilderException("Cannot find file '"+name+"' (this is an internal error).");
		}

		return stream;
	}

	private static File createFile(String path, String name) {
		File file = new File(path+File.separator+name);

		if (file.exists()) {
			if (!file.delete()) {
				throw new DescriptorBuilderException("Could not remove old file '"+file.getAbsolutePath()+"'.");
			}
		}

		boolean create;
		try {
			create = file.createNewFile();
		} catch (IOException ex) {
			throw new DescriptorBuilderException("Error creating file.", ex);
		}

		if (!create) {
			throw new DescriptorBuilderException("Error creating file.");
		}

		return file;
	}

	private static void copyFile(InputStream sourceFile, File destFile) {
		FileOutputStream destination = null;

		try {
			String data = new Scanner(sourceFile).useDelimiter("\\A").next();
			destination = new FileOutputStream(destFile);
			destination.write(data.getBytes());
		} catch (Exception ex) {
			throw new DescriptorBuilderException("Error while writing files.", ex);
		} finally {
			if (destination != null) {
				try {destination.close();} catch (Exception ex) { /* nothing */ }
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.1607773851590106, 'fraction_numerical': 0.0041645633518425035, 'mean_word_length': 3.7798552472858864, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6435398', 'n_tokens_mistral': 2480, 'n_tokens_neox': 2203, 'n_words': 672}