
nyla.solutions.commas.file.ReReplaceCommand Maven / Gradle / Ivy
The newest version!
package nyla.solutions.commas.file;
import java.io.File;
import java.util.Map;
import nyla.solutions.core.exception.SystemException;
import nyla.solutions.core.io.IO;
import nyla.solutions.core.io.SynchronizedIO;
import nyla.solutions.core.util.Config;
import nyla.solutions.core.util.Debugger;
import nyla.solutions.core.util.JavaBean;
import nyla.solutions.core.util.Text;
public class ReReplaceCommand implements FileCommand
{
/**
* Use a regular expression replacing matches in the source content to an output location
*/
public Void execute(File file)
{
try
{
//read source full text
String fileContent = IO.readFile(file.getAbsolutePath());
//replace content
Debugger.println(this,"replacing RE["+regExp+"]");
String runtimeReplacement = this.replacement;
if(this.formatting)
{
runtimeReplacement = formatReplacement(file);
}
Debugger.println(this,"pre runtimeReplacement="+runtimeReplacement);
//runtimeReplacement = runtimeReplacement.replace(" ", "");
fileContent = Text.replaceForRegExprWith(fileContent, regExp, runtimeReplacement);
//fileContent = Text.replaceForRegExprWith(fileContent, regExp, "Hello world");
Debugger.println(this,"post fileContent="+runtimeReplacement);
//Debugger.println(this,"replacement="+replacement);
//save text to output file
SynchronizedIO.getInstance().writeFile(file.getAbsolutePath(), fileContent);
return null;
}
catch (Exception e)
{
throw new SystemException(e);
}
}// --------------------------------------------
/**
* @return the regExp
*/
public String getRegExp()
{
return regExp;
}// --------------------------------------------
/**
* @param regExp the regExp to set
*/
public void setRegExp(String regExp)
{
this.regExp = regExp;
}// --------------------------------------------
/**
* @return the replacement
*/
public String getReplacement()
{
return replacement;
}// --------------------------------------------
/**
* @param replacement the replacement to set
*/
public void setReplacement(String replacement)
{
this.replacement = replacement;
}// --------------------------------------------
/**
* @return the formatting
*/
public boolean isFormatting()
{
return formatting;
}//---------------------------------------------
/**
* @param formatting the formatting to set
*/
public void setFormatting(boolean formatting)
{
this.formatting = formatting;
}
private String formatReplacement(File file)
throws Exception
{
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy