/*
 * Created on 2004-08-30
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.skins;

import java.io.BufferedWriter;
import java.io.IOException;

/**
 * Thsi rewriter changes the parameter of a given command to a given string
 * 
 * @author mikolaj
 */
public class SetParameterRewriter implements IRewriterRule {

    public SetParameterRewriter(String command, String newArgs, boolean forceAdd) {
        super();
        fCommand=command;
        fNewArgs=newArgs;
        fForceAdd=forceAdd;
    }
    
    /**
     * Does nothing
     * @see pl.zalewski.mikolaj.symbdev.skins.IRewriterRule#fileBegin(java.io.BufferedWriter)
     */
    public void fileBegin(BufferedWriter output) throws IOException {
    }
    
    /**
     * Changes the argument of that token to the one given in constructor 
     */
    public void rewrite(String token, String args, BufferedWriter output)
    	throws IOException
    {
        if (fCommand.equalsIgnoreCase(token))
        {
            output.write(token+" "+fNewArgs);
        	output.newLine();
        	fWasAdded=true;
        } else
        {
            output.write(token+" "+args);
        	output.newLine();            
        }
    }
    
    /**
     * If fForceAdd is true and that command wasn't written to the file. It is written at the
     * end
     * @see pl.zalewski.mikolaj.symbdev.skins.IRewriterRule#fileEnd(java.io.BufferedWriter)
     */
    public void fileEnd(BufferedWriter output) throws IOException {
        if (!fWasAdded)
        {
            output.write(fCommand+" "+fNewArgs);
            output.newLine();
        }
    }
    
    
    String fCommand, fNewArgs;
    boolean fForceAdd;
    boolean fWasAdded;
}
