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

de.invation.code.toval.file.CharacterReplaceParser Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.file;

import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;

import de.invation.code.toval.validate.ParameterException;
import de.invation.code.toval.validate.Validate;


public class CharacterReplaceParser extends LineBasedFileTransformer{
	
	protected String replaceString = null;
	protected String replacementString = null;
	
	public CharacterReplaceParser(String replaceString, String replacementString) throws ParameterException {
		super();
		Validate.notNull(replaceString);
		Validate.notNull(replacementString);
		this.replaceString = replaceString;
		this.replacementString = replacementString;
	}
	
	public CharacterReplaceParser(Charset charset, String replaceString, String replacementString) throws ParameterException {
		this(charset, charset, replaceString, replacementString);
	}

	public CharacterReplaceParser(Charset inputCharset, Charset outputCharset, String replaceString, String replacementString) throws ParameterException {
		super(inputCharset, outputCharset);
		Validate.notNull(replaceString);
		Validate.notNull(replacementString);
		this.replaceString = replaceString;
		this.replacementString = replacementString;
	}

	@Override
	protected Set transformLine(String line) {
		Set result = new HashSet();
		if(replaceString != null && replacementString != null){
			result.add(line.replace(replaceString, replacementString));
		} else {
			result.add(line);
		}
		return result;
	}

	public static void main(String[] args) throws Exception{
		if(args.length < 3 || args.length > 4){
			System.out.println("Usage: FileParser [charset] find replace filename");
			return;
		}
		CharacterReplaceParser parser;
		if(args.length == 3){
			parser = new CharacterReplaceParser(args[0], args[1]);
			parser.parseFile(args[2]);
		} else {
			parser = new CharacterReplaceParser(Charset.forName(args[0]), args[1], args[2]);
			parser.parseFile(args[3]);
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy