1 23 24 28 29 32 33 package com.sun.enterprise.admin.util; 34 35 import java.io.File ; 36 import java.io.Reader ; 37 import java.io.Writer ; 38 import java.io.FileReader ; 39 import java.io.FileWriter ; 40 import java.io.BufferedReader ; 41 import java.io.BufferedWriter ; 42 43 import com.sun.enterprise.admin.util.TokenValue; 44 import com.sun.enterprise.admin.util.TokenValueSet; 45 46 48 public final class LineTokenReplacer { 49 50 private final TokenValue[] tokenArray; 51 52 53 public LineTokenReplacer(TokenValueSet tokens) { 54 final Object [] tmp = tokens.toArray(); 55 final int length = tmp.length; 56 this.tokenArray = new TokenValue[length]; 57 System.arraycopy(tmp, 0, tokenArray, 0, length); 58 } 59 public void replace(File inputFile, File outputFile) { 60 BufferedReader reader = null; 62 BufferedWriter writer = null; 63 try { 64 reader = new BufferedReader (new FileReader (inputFile)); 65 writer = new BufferedWriter (new FileWriter (outputFile)); 66 String lineContents = null; 67 while((lineContents = reader.readLine()) != null) { 68 String modifiedLine = replaceLine(lineContents); 69 writer.write(modifiedLine); 70 writer.newLine(); 71 } 72 } 73 catch (Exception e) { 74 e.printStackTrace(); 75 throw new RuntimeException (e); 76 } 77 finally { 78 try { 79 if (reader != null) 80 reader.close(); 81 if (writer != null) 82 writer.close(); 83 } 84 catch(Exception e) {} 85 } 86 } 87 88 public void replace(String inputFileName, String outputFileName) { 89 this.replace(new File (inputFileName), new File (outputFileName)); 90 } 91 92 private String replaceLine(String lineWithTokens) { 93 String tokenFreeString = lineWithTokens; 94 95 for (int i = 0 ; i < tokenArray.length ; i++) { 96 TokenValue aPair = tokenArray[i]; 97 tokenFreeString = tokenFreeString.replaceAll(aPair.delimitedToken, 100 aPair.value); 101 } 102 return ( tokenFreeString ); 103 } 104 } 105 | Popular Tags |