<commit_before>/*
 * Copyright (c) 2002 <NAME> <<EMAIL>>
 * This code is released under the MIT license; see the file LICENSE.
 *
 * $Id: SPGenTask.java,v 1.2 2002/12/19 22:07:32 knitterb Exp $
 */

package net.spy.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;

import org.apache.tools.ant.taskdefs.MatchingTask;

/**
 * Generates Java code from SPT files.
 *
 * This is an Ant task that recurses a directory and invokes
 * net.spy.util.SPGen on .spt files to generate Java code. It does the usual
 * stuff expected of a Make-style tool: leaving up-to-date .java files intact,
 * creating output with a temporary file until it is finished so partial
 * builds don't create problems, etc.
 *
 * @author <NAME>
 * @version $Revision: 1.2 $ $Date: 2002/12/19 22:07:32 $
 **/
public class SPGenTask extends MatchingTask {

	private File srcDir;
	private File destDir;
	private String superclass=null;

	/**
	 * Sets the source directory to search for .spt files.
	 **/
	public void setSrcdir(File srcDir) {
		this.srcDir = srcDir;
	}

	/**
	 * Sets the destination directory to place .java files.
	 * If not specified, assumed to be the same as the source directory.
	 **/
	public void setDestdir(File destDir) {
		this.destDir = destDir;
	}

	public void setSuperclass(String sc) {
		this.superclass=sc;
	}

	/**
	 * Performs the operation.
	 **/
	public void execute() throws BuildException {
		if (srcDir == null) {
			throw new BuildException("srcdir attribute must be set!", location);
		}
		if (!srcDir.isDirectory()) {
			throw new BuildException("source directory \"" + srcDir
					+ "\" is not valid.");
		}
		if (destDir == null) {
			destDir = srcDir;
		}

		if (!destDir.isDirectory()) {
			throw new BuildException("destination directory \"" + destDir
				+ "\" is not valid.", location);
		}

		DirectoryScanner ds = getDirectoryScanner(srcDir);
		String[] includes = {"**\\*.spt"};
		ds.setIncludes(includes);
		ds.setBasedir(srcDir);
		ds.scan();
		String files[] = ds.getIncludedFiles();
		String toDoFiles[] = trimSPTList(files);

		if(toDoFiles.length > 0) {
			log("Compiling " + toDoFiles.length + " of " + files.length
				+ " spt file" + (files.length == 1 ? "" : "s")
				+ " to " + destDir);

			// Do the work
			for (int i = 0; i < toDoFiles.length; i ++) {
				processFile(toDoFiles[i]);
			}
		}
	}

	private String[] trimSPTList(String input[]) {
		ArrayList a=new ArrayList();

		for(int i = 0; i < input.length; i ++) {
			File srcFile = new File(srcDir, input[i]);
			File destFile = getDestFile(input[i]);

			if(destFile.exists()
				&& destFile.lastModified() > srcFile.lastModified()) {
				// Already have this file.
			} else {
				a.add(input[i]);
			}
		}

		String rv[]=new String[a.size()];
		for(int i=0; i<a.size(); i++) {
			rv[i]=(String)a.get(i);
		}

		return(rv);
	}

	// Translate a source filename (.spt) to a dest File (.java)
	private File getDestFile(String filename) {
		File destFile = new File(destDir,
				filename.substring(0, filename.length()-4)+".java");
		return(destFile);
	}

	/**
	 * Processes an individual file.
	 * @param filename The filename, relative to the source directory.
	 **/
	protected void processFile(String filename) {
		File srcFile = new File(srcDir, filename);
		File tmpFile = null;
		File destFile = getDestFile(filename);
		BufferedReader in;
		PrintWriter out;

		try {

			// Open input file
			try {
				FileReader reader = new FileReader(srcFile);
				in = new BufferedReader(reader);
			} catch (IOException e) {
				throw new BuildException(e);
			}

			if (destFile.exists()
					&& destFile.lastModified() > srcFile.lastModified()) {
				return;
			}

			// Open output file
			// Temporary so partial builds don't impact future ones
			try {
				tmpFile = File.createTempFile("sptgen", "java", destDir);
				FileWriter writer = new FileWriter(tmpFile);
				out = new PrintWriter(writer);
			} catch (IOException e) {
				throw new BuildException(e);
			}

			String name = srcFile.getName();
			// assert name.endsWith(".spt");
			SPGen spg = new SPGen(name.substring(0, name.length()-4), in, out);
			if (this.superclass!=null) {
				System.err.println("making sc: "+this.superclass);
				spg.setSuperclass(this.superclass);
			}
			try {
				spg.generate();
				in.close();
				out.close();
			} catch (Exception e) {
				throw new BuildException(e);
			}

			if (destFile.exists()) {
				if (!destFile.delete()) {
					throw new BuildException("Unable to delete "
							+ destFile);
				}
			} else {
				checkParent(destFile);
			}
			if (!tmpFile.renameTo(destFile)) {
				throw new BuildException("Unable to rename "
						+ tmpFile + " to " + destFile);
			}<commit_msg>took out stderr debug statement Keywords: cscvs:MAIN-152<commit_after>/*
 * Copyright (c) 2002 <NAME> <<EMAIL>>
 * This code is released under the MIT license; see the file LICENSE.
 *
 * $Id: SPGenTask.java,v 1.3 2002/12/19 22:10:48 knitterb Exp $
 */

package net.spy.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;

import org.apache.tools.ant.taskdefs.MatchingTask;

/**
 * Generates Java code from SPT files.
 *
 * This is an Ant task that recurses a directory and invokes
 * net.spy.util.SPGen on .spt files to generate Java code. It does the usual
 * stuff expected of a Make-style tool: leaving up-to-date .java files intact,
 * creating output with a temporary file until it is finished so partial
 * builds don't create problems, etc.
 *
 * <p>Example ant build entry:
 * <pre><code>
 *	<target name="spfiles">
 *		<taskdef name="spgen" classname="net.spy.util.SPGenTask">
 *			<classpath refid="compile.classpath"/>
 *		</taskdef>
 *		<spgen srcdir="${spt.dir}" destdir="${spt.dir}"
 *			superclass="com.tivo.db.TivoDbsp"/>
 *	</target>
 *
 * </pre></code>
 * </p>
 *
 * @author <NAME>
 * @version $Revision: 1.3 $ $Date: 2002/12/19 22:10:48 $
 **/
public class SPGenTask extends MatchingTask {

	private File srcDir;
	private File destDir;
	private String superclass=null;

	/**
	 * Sets the source directory to search for .spt files.
	 **/
	public void setSrcdir(File srcDir) {
		this.srcDir = srcDir;
	}

	/**
	 * Sets the destination directory to place .java files.
	 * If not specified, assumed to be the same as the source directory.
	 **/
	public void setDestdir(File destDir) {
		this.destDir = destDir;
	}

	public void setSuperclass(String sc) {
		this.superclass=sc;
	}

	/**
	 * Performs the operation.
	 **/
	public void execute() throws BuildException {
		if (srcDir == null) {
			throw new BuildException("srcdir attribute must be set!", location);
		}
		if (!srcDir.isDirectory()) {
			throw new BuildException("source directory \"" + srcDir
					+ "\" is not valid.");
		}
		if (destDir == null) {
			destDir = srcDir;
		}

		if (!destDir.isDirectory()) {
			throw new BuildException("destination directory \"" + destDir
				+ "\" is not valid.", location);
		}

		DirectoryScanner ds = getDirectoryScanner(srcDir);
		String[] includes = {"**\\*.spt"};
		ds.setIncludes(includes);
		ds.setBasedir(srcDir);
		ds.scan();
		String files[] = ds.getIncludedFiles();
		String toDoFiles[] = trimSPTList(files);

		if(toDoFiles.length > 0) {
			log("Compiling " + toDoFiles.length + " of " + files.length
				+ " spt file" + (files.length == 1 ? "" : "s")
				+ " to " + destDir);

			// Do the work
			for (int i = 0; i < toDoFiles.length; i ++) {
				processFile(toDoFiles[i]);
			}
		}
	}

	private String[] trimSPTList(String input[]) {
		ArrayList a=new ArrayList();

		for(int i = 0; i < input.length; i ++) {
			File srcFile = new File(srcDir, input[i]);
			File destFile = getDestFile(input[i]);

			if(destFile.exists()
				&& destFile.lastModified() > srcFile.lastModified()) {
				// Already have this file.
			} else {
				a.add(input[i]);
			}
		}

		String rv[]=new String[a.size()];
		for(int i=0; i<a.size(); i++) {
			rv[i]=(String)a.get(i);
		}

		return(rv);
	}

	// Translate a source filename (.spt) to a dest File (.java)
	private File getDestFile(String filename) {
		File destFile = new File(destDir,
				filename.substring(0, filename.length()-4)+".java");
		return(destFile);
	}

	/**
	 * Processes an individual file.
	 * @param filename The filename, relative to the source directory.
	 **/
	protected void processFile(String filename) {
		File srcFile = new File(srcDir, filename);
		File tmpFile = null;
		File destFile = getDestFile(filename);
		BufferedReader in;
		PrintWriter out;

		try {

			// Open input file
			try {
				FileReader reader = new FileReader(srcFile);
				in = new BufferedReader(reader);
			} catch (IOException e) {
				throw new BuildException(e);
			}

			if (destFile.exists()
					&& destFile.lastModified() > srcFile.lastModified()) {
				return;
			}

			// Open output file
			// Temporary so partial builds don't impact future ones
			try {
				tmpFile = File.createTempFile("sptgen", "java", destDir);
				FileWriter writer = new FileWriter(tmpFile);
				out = new PrintWriter(writer);
			} catch (IOException e) {
				throw new BuildException(e);
			}

			String name = srcFile.getName();
			// assert name.endsWith(".spt");
			SPGen spg = new SPGen(name.substring(0, name.length()-4), in, out);
			if (this.superclass!=null) {
				spg.setSuperclass(this.superclass);
			}
			try {
				spg.generate();
				in.close();
				out.close();
			} catch (Exception e) {
				throw new BuildException(e);
			}

			if (destFile.exists()) {
				if (!destFile.delete()) {
					throw new BuildException("Unable to delete "
							+ destFile);
				}
			} else {
				checkParent(destFile);
			}
			if (!tmpFile.renameTo(destFile)) {
				throw new BuildException("Unable to rename "
						+ tmpFile + " to " + destFile);
			}
{'fraction_non_alphanumeric': 0.14168883633855553, 'fraction_numerical': 0.009163464380727165, 'mean_word_length': 3.259336970205623, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '940036', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3344, 'n_words': 923}