FileDocCategorySizeDatePackage
LineTokenReplacer.javaAPI DocGlassfish v2 API5096Fri May 04 22:25:58 BST 2007com.sun.enterprise.admin.util

LineTokenReplacer

public final class LineTokenReplacer extends Object

Fields Summary
private final com.sun.enterprise.admin.util.TokenValue[]
tokenArray
private final String
charsetName
Constructors Summary
public LineTokenReplacer(com.sun.enterprise.admin.util.TokenValueSet tokens)

        this(tokens, null);
    
public LineTokenReplacer(com.sun.enterprise.admin.util.TokenValueSet tokens, String charset)
Creates a new instance of TokenReplacer

            final Object[] tmp = tokens.toArray();
            final int length = tmp.length;
            this.tokenArray = new TokenValue[length];
            System.arraycopy(tmp, 0, tokenArray, 0, length);
            this.charsetName = charset;
    
Methods Summary
public voidreplace(java.io.File inputFile, java.io.File outputFile)

        //Edge-cases
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            if(charsetName!=null)
            {
                FileOutputStream outputStream = new FileOutputStream(outputFile);
                Charset charset = Charset.forName(charsetName);
                writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset));
            }
            else
            {
                writer = new BufferedWriter(new FileWriter(outputFile));
            }
            String lineContents = null;
            while((lineContents = reader.readLine()) != null) {
                String modifiedLine = replaceLine(lineContents);
                writer.write(modifiedLine);
                writer.newLine();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (reader != null)
                    reader.close();
                if (writer != null)
                    writer.close();
            }
            catch(Exception e) {}
        }
    
public voidreplace(java.lang.String inputFileName, java.lang.String outputFileName)

        this.replace(new File(inputFileName), new File(outputFileName));
    
private java.lang.StringreplaceLine(java.lang.String lineWithTokens)

        String tokenFreeString = lineWithTokens;
        
        for (int i = 0 ; i < tokenArray.length ; i++) {
            TokenValue aPair        = tokenArray[i];
            //System.out.println("To replace: " + aPair.delimitedToken);
            //System.out.println("Value replace: " + aPair.value);
            tokenFreeString = tokenFreeString.replaceAll(aPair.delimitedToken,
                aPair.value);
        }
        return ( tokenFreeString );