Different approach? Straightforward?
Code
public class ContentReplacerDriver
{
public static void main (String[] args) throws java.lang.Exception
{
if(args.length != 2)
throw new IllegalArgumentException("args must contain replacements file and content file");
String replacementFileName = args[0];
String contentFileName = args[1];
ContentReplacer cr = new ContentReplacer(replacementFileName);
String replaced = cr.replace(contentFileName);
System.out.println(replaced);
}
}
Code
import java.util.*;
import java.io.*;
public class ContentReplacer
{
private ContentReplaceFile contentReplaceFile;
public ContentReplacer(String fileName) throws FileNotFoundException, IOException
{
this(fileName, new DefaultContentReplaceFileFactory());
}
public ContentReplacer(String fileName, ContentReplaceFileFactory contentReplaceFileFactory)
throws FileNotFoundException, IOException
{
if(fileName == null || fileName.isEmpty())
throw new IllegalArgumentException("fileName is null or empty");
if(contentReplaceFileFactory == null)
throw new IllegalArgumentException("contentReplaceFileFactory is null");
this.contentReplaceFile = contentReplaceFileFactory.createContentReplaceFile(fileName);
}
public String replace(String fileName) throws FileNotFoundException
{
Map<String, String> replacements = contentReplaceFile.getReplacements();
Scanner in = new Scanner(new File(fileName));
String contentToReplace = new String();
while(in.hasNextLine())
{
contentToReplace += in.nextLine() + "\n";
}
for(Map.Entry<String, String> kvp : replacements.entrySet())
{
String key = kvp.getKey();
String value = kvp.getValue();
contentToReplace = contentToReplace.replace(key, value);
}
return contentToReplace;
}
}
Code
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;
public class ContentReplaceFile
{
private Map<String, String> replacements;
private String kvDelimiter, kvpDelimiter;
private static final String DEFAULT_KVP_DELIMITER = ",";
private static final String DEFAULT_KV_DELIMITER = ":";
public ContentReplaceFile(String fileName)
throws FileNotFoundException, IOException
{
this(fileName, DEFAULT_KVP_DELIMITER, DEFAULT_KV_DELIMITER);
}
public ContentReplaceFile(String fileName, String kvpDelimiter, String kvDelimiter)
throws FileNotFoundException, IOException
{
if(kvpDelimiter == null || kvpDelimiter.isEmpty())
throw new IllegalArgumentException("kvpDelimiter is null or empty");
if(kvDelimiter == null || kvDelimiter.isEmpty())
throw new IllegalArgumentException("kvDelimiter is null or empty");
this.kvpDelimiter = kvpDelimiter;
this.kvDelimiter = kvDelimiter;
loadFile(fileName);
}
private void loadFile(String fileName) throws FileNotFoundException, IOException
{
Scanner in = new Scanner(new File(fileName));
replacements = new Hashtable<String, String>();
in.useDelimiter(kvpDelimiter);
while(in.hasNext())
{
String kvp = in.next();
String[] kv = kvp.split(kvDelimiter);
if(kv.length != 2)
throw new IOException(String.format("format should be key%svalue", kvDelimiter));
replacements.put(kv[0], kv[1]);
}
}
public Map<String, String> getReplacements()
{
return new Hashtable<String, String>(replacements);
}
}
Code
import java.io.FileNotFoundException;
import java.io.IOException;
public interface ContentReplaceFileFactory
{
ContentReplaceFile createContentReplaceFile(String fileName) throws FileNotFoundException, IOException;
}
Code
import java.io.FileNotFoundException;
import java.io.IOException;
public class DefaultContentReplaceFileFactory implements ContentReplaceFileFactory
{
public ContentReplaceFile createContentReplaceFile(String fileName) throws FileNotFoundException, IOException
{
return new ContentReplaceFile(fileName);
}
}
Hi guys.
This post was edited by Minkomonster on Sep 10 2015 07:05pm