

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

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

public class ComCancel extends Task {

	/**
	 * @param args
	 */

	protected String leading = "//#";
	protected String fileInputPath = "fileIn";
	protected String fileOutputPath = "fileOut/";
			
	public void execute() throws BuildException{
		// TODO Auto-generated method stub
		ComCancel cc = new ComCancel();
		cc.setFileInputPath(fileInputPath);
		cc.setFileOutputPath(fileOutputPath);
		cc.setLeading(leading);
		DoList(new File(fileInputPath));
		
	}

	private  void DoList(File filePath) {
		// TODO Auto-generated method stub
		if (!filePath.exists()||!filePath.isDirectory()){//into directory
			return;
		}else{
			String [] files = filePath.list();
			for (int i=0 ; i<files.length; i++){
				File file = new File (filePath,files[i]);
				if (file.isFile()){
					DoFile(file);
				}else{
					DoList(file);
				}
			}
		}
	}

	private  void DoFile(File fileIn) {
		// TODO Auto-generated method stub
		
		FileReader input = null ;
		FileWriter output = null;
		BufferedReader br = null ;
		BufferedWriter bw = null;
		
		try {
			input = new FileReader(fileIn);
			br = new BufferedReader(input);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			File fileOut = new File(fileOutputPath + fileIn.getName());
			output = new FileWriter(fileOut);
			bw = new BufferedWriter(output);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		String s;
		try {
			s = br.readLine();
			while(null != s){
				bw.write(DoLine (s));
				if ("" != DoLine (s)){
					bw.newLine();
				}
				s = br.readLine();
			}
		
			br.close();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private  String DoLine(String s) {
		// TODO Auto-generated method stub
		if (s.trim().startsWith(leading)){
			return "";
		}else{
			return s;
		}
	}

	public String getLeading() {
		return leading;
	}

	public void setLeading(String leading) {
		this.leading = leading;
	}

	public String getFileInputPath() {
		return fileInputPath;
	}

	public void setFileInputPath(String fileInputPath) {
		this.fileInputPath = fileInputPath;
	}

	public String getFileOutputPath() {
		return fileOutputPath;
	}

	public void setFileOutputPath(String fileOutputPath) {
		this.fileOutputPath = fileOutputPath;
	}

}
