org.fife.ui.search.ReplaceInFilesThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rtext Show documentation
Show all versions of rtext Show documentation
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.
/*
* 09/19/2006
*
* ReplaceInFilesThread.java - Thread that replaces text in files.
* Copyright (C) 2006 Robert Futrell
* http://fifesoft.com/rtext
* Licensed under a modified BSD license.
* See the included license file for details.
*/
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.rsta.ui.search.FindDialog;
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.getString2("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