All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.search.ReplaceInFilesThread Maven / Gradle / Ivy

Go to download

RText is a powerful, cross-platform programmer's text editor written in Java. It is designed to be easy to use, highly customizable and flexible. Part of RText's design is for the source code to be simple, easy to understand, and well documented, so that other programmers can look into its inner-workings and figure out how RText ticks with ease. A good place to start (besides the source code) is the Javadoc for all classes used in the project.

There is a newer version: 2.0.7
Show newest version
/*
 * 09/19/2006
 *
 * ReplaceInFilesThread.java - Thread that replaces text in files.
 * Copyright (C) 2006 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui.search;

import java.io.*;
import java.text.MessageFormat;
import java.util.*;
import java.util.regex.*;

import org.fife.io.*;
import org.fife.ui.rtextarea.SearchEngine;


/**
 * A thread created by a ReplaceInFilesDialog to do the
 * replacing.
 *
 * @author Robert Futrell
 * @version 1.0
 */
class ReplaceInFilesThread extends FindInFilesThread {


	/**
	 * Constructor.
	 *
	 * @param dialog The "find in files" dialog.
	 * @param directory The directory in which to search.
	 */
	public ReplaceInFilesThread(FindInFilesDialog dialog, File directory) {
		super(dialog, directory);
	}


	/**
	 * Runs the search.
	 */
	public Object construct() {

		// Get the string to search for and filters for the files to search.
		String searchString = dialog.getSearchString();
		Pattern[] filterStrings = getFilterStrings();
		if (filterStrings==null) {
			dialog.searchCompleted("");
			return null;
		}

		// Then, do the search.
		dialog.clearSearchResults();
		File[] files = directory.listFiles();
		List fileList = new ArrayList();
		fileList.addAll(Arrays.asList(files));

		boolean checkSubfolders = dialog.getCheckSubfolders();
		boolean matchCase = dialog.getMatchCase();
		boolean wholeWord = dialog.getMatchWholeWord();
		boolean useRegex = dialog.getUseRegEx();
		boolean doVerboseOutput = dialog.getDoVerboseOutput();
		String replaceString = ((ReplaceInFilesDialog)dialog).getReplaceString();
		String searchingFile = dialog.getBundle().getString("SearchingFile");

		if (!useRegex && !matchCase)
			searchString = searchString.toLowerCase();

		long startMillis = System.currentTimeMillis();
		StringBuffer sb = new StringBuffer();
		char[] buf = new char[4096];
		StringBuffer replaceSB = new StringBuffer();

		// Keep looping while there are more files to search.
		int numFiles = fileList.size();
		for (int i=0; i0) {

					try {

						int repCount = 0;
						replaceSB.setLength(0);

						if (useRegex) {
							repCount = doSearchRegex(sb, searchString,
								replaceString, matchCase, wholeWord,
								fileFullPath, replaceSB);
						}
						else {
							repCount = doSearchNoRegex(sb, searchString,
								replaceString, matchCase, wholeWord,
								fileFullPath, replaceSB);
						}

						// If text was replaced, rewrite the file with
						// its new contents.
						if (repCount>0) {

							PrintWriter w = new PrintWriter(new BufferedWriter(
								new UnicodeWriter(fileFullPath, encoding)));
							w.print(replaceSB.toString());
							w.close();

							String text = MessageFormat.format(occurrencesString,
									new Object[] { new Integer(repCount) });
							MatchData data = new MatchData(fileFullPath,
											NO_LINE_NUMBER, text);
							dialog.addMatchData(data);

						}
						else if (doVerboseOutput) { // repCount==0
							String text = MessageFormat.format(occurrencesString,
									new Object[] { new Integer(repCount) });
							MatchData data = createVerboseMatchData(
											fileFullPath, text);
							dialog.addMatchData(data);
						}

					} catch (/*IO*/Exception ioe) {
						ioe.printStackTrace();
						String desc = ioe.getMessage();
						MatchData data = createErrorMatchData(
										fileFullPath, desc);
						dialog.addMatchData(data);
					}

				}

			} // End of if (temp.isFile()).

			// Otherwise, if the file is a directory...
			else if (temp.isDirectory()) {

				// Ignore this (sub)directory if the user doesn't want
				// to search subdirectories.
				if (!checkSubfolders) {
					if (doVerboseOutput) {
						MatchData data = createVerboseMatchData(
							fileFullPath, dontSearchSubfoldersString);
						dialog.addMatchData(data);
					}
					continue;
				}

				// Add any files in this subdirectory to the master list
				// of files to search.
				List moreFilesList = getFilesFromDirectory(temp);
				int count = moreFilesList==null ? 0 : moreFilesList.size();
				if (count>0) {
					fileList.addAll(moreFilesList);
					numFiles += count;
				}
				if (doVerboseOutput) {
					MatchData data = createVerboseMatchData(
						fileFullPath, newFilesToExamineString +
						": " + count);
					dialog.addMatchData(data);
				}

			} // End of else if (temp.isDirectory()).

		} // End of for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy