KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > LineTokenReplacer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30 * LineTokeReplacer.java
31 */

32
33 package com.sun.enterprise.admin.util;
34
35 import java.io.File JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.io.Writer JavaDoc;
38 import java.io.FileReader JavaDoc;
39 import java.io.FileWriter JavaDoc;
40 import java.io.BufferedReader JavaDoc;
41 import java.io.BufferedWriter JavaDoc;
42
43 import com.sun.enterprise.admin.util.TokenValue;
44 import com.sun.enterprise.admin.util.TokenValueSet;
45
46 /**
47  */

48 public final class LineTokenReplacer {
49     
50     private final TokenValue[] tokenArray;
51     
52     /** Creates a new instance of TokenReplacer */
53     public LineTokenReplacer(TokenValueSet tokens) {
54             final Object JavaDoc[] 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 JavaDoc inputFile, File JavaDoc outputFile) {
60         //Edge-cases
61
BufferedReader JavaDoc reader = null;
62         BufferedWriter JavaDoc writer = null;
63         try {
64             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(inputFile));
65             writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(outputFile));
66             String JavaDoc lineContents = null;
67             while((lineContents = reader.readLine()) != null) {
68                 String JavaDoc modifiedLine = replaceLine(lineContents);
69                 writer.write(modifiedLine);
70                 writer.newLine();
71             }
72         }
73         catch (Exception JavaDoc e) {
74             e.printStackTrace();
75             throw new RuntimeException JavaDoc(e);
76         }
77         finally {
78             try {
79                 if (reader != null)
80                     reader.close();
81                 if (writer != null)
82                     writer.close();
83             }
84             catch(Exception JavaDoc e) {}
85         }
86     }
87     
88     public void replace(String JavaDoc inputFileName, String JavaDoc outputFileName) {
89         this.replace(new File JavaDoc(inputFileName), new File JavaDoc(outputFileName));
90     }
91     
92     private String JavaDoc replaceLine(String JavaDoc lineWithTokens) {
93         String JavaDoc tokenFreeString = lineWithTokens;
94         
95         for (int i = 0 ; i < tokenArray.length ; i++) {
96             TokenValue aPair = tokenArray[i];
97             //System.out.println("To replace: " + aPair.delimitedToken);
98
//System.out.println("Value replace: " + aPair.value);
99
tokenFreeString = tokenFreeString.replaceAll(aPair.delimitedToken,
100                 aPair.value);
101         }
102         return ( tokenFreeString );
103     }
104 }
105
Popular Tags