KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > test > TokenReplacementTester


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  * TokenReplacementTester.java
31  *
32  * Created on March 6, 2003, 5:11 PM
33  */

34
35 package com.sun.enterprise.admin.util.test;
36
37 import com.sun.enterprise.admin.util.LineTokenReplacer;
38 import com.sun.enterprise.admin.util.TokenValue;
39 import com.sun.enterprise.admin.util.TokenValueSet;
40
41 import java.io.File JavaDoc;
42 import java.io.FileReader JavaDoc;
43 import java.io.BufferedReader JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45
46 /**
47  *
48  * @author kedar
49  */

50 public class TokenReplacementTester {
51     
52     /** Creates a new instance of TokenReplacementTester */
53     private final LineTokenReplacer replacer;
54     
55     public TokenReplacementTester(String JavaDoc tokensFileName, String JavaDoc fromFile, String JavaDoc toFile) {
56         final TokenValueSet tokens = getTokensFromFile(tokensFileName);
57         replacer = new LineTokenReplacer(tokens);
58         replacer.replace(fromFile, toFile);
59     }
60     
61     private TokenValueSet getTokensFromFile(String JavaDoc fileName) {
62         final TokenValueSet tokens = new TokenValueSet();
63         BufferedReader JavaDoc reader = null;
64         try {
65             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
66             String JavaDoc line = null;
67             while ((line = reader.readLine()) != null) {
68                 final TokenValue tv = getTokenValue(line);
69                 tokens.add(tv);
70             }
71             reader.close();
72             reader = null;
73         }
74         catch(Exception JavaDoc e) {
75             e.printStackTrace();
76             if (reader != null) {
77                 try {
78                     reader.close();
79                 } catch (Exception JavaDoc ex) {}
80             }
81         }
82         return tokens;
83     }
84
85     private TokenValue getTokenValue(String JavaDoc line) {
86         final String JavaDoc delim = "=";
87         final StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(line, delim);
88         final String JavaDoc[] output = new String JavaDoc[2];
89         int i = 0;
90         while(parser.hasMoreTokens()) {
91             output[i++] = parser.nextToken();
92         }
93         final String JavaDoc DELIM = "%%%";
94         TokenValue tv = new TokenValue(output[0], output[1], DELIM);
95         return ( tv );
96     }
97     /**
98      * @param args the command line arguments
99      */

100     public static void main(String JavaDoc[] args) {
101         int length = args.length;
102         if (length < 2) {
103             usage();
104             System.exit(1);
105         }
106         final String JavaDoc tokensFile = args[0];
107         final String JavaDoc fromFile = args[1];
108         final String JavaDoc toFile = fromFile + ".out";
109         new TokenReplacementTester(tokensFile, fromFile, toFile);
110     }
111     
112     private static void usage() {
113         System.out.println("java TokenReplacementTester <tokens-file> <template-file>");
114     }
115 }
116
Popular Tags