KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jbatchengine > batch > util > CommandFileParser


1 /*
2  jBatchEngine - jbatchengine.sourceforge.net
3  Copyright (C) 2006 by Simon Martinelli, Gampelen, Switzerland
4
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */

19 package net.sf.jbatchengine.batch.util;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.apache.commons.lang.StringUtils;
29
30 /**
31  * Parser for jcmd Files.
32  *
33  * @author $Author: simas_ch $
34  * @version $Revision: 1.8 $, $Date: 2006/10/21 16:37:06 $
35  */

36 public class CommandFileParser {
37
38   /**
39    * It substitutes SET and put the in a map. When finished it returns a string that can be executed
40    *
41    * @param file the file
42    * @return command string
43    * @throws IOException the IO exception
44    */

45   public static String JavaDoc parse(String JavaDoc file) throws IOException JavaDoc {
46     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
47
48     String JavaDoc line = br.readLine();
49     String JavaDoc command = null;
50     Map JavaDoc<String JavaDoc, String JavaDoc> variables = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
51
52     // Get JAVA_HOME
53
String JavaDoc javahome = System.getProperty("java.home");
54     variables.put("JAVA_HOME", javahome);
55
56     while (line != null) {
57       if (line.startsWith("rem") || line.startsWith("@")) {
58         // comment and @ will be ignored
59
}
60       else {
61         if (line.startsWith("set ")) {
62           Pattern JavaDoc gleichPattern = Pattern.compile("=");
63           String JavaDoc[] strings = gleichPattern.split(line, 2);
64           String JavaDoc left = strings[0];
65           String JavaDoc right = strings[1];
66           for (String JavaDoc key : variables.keySet()) {
67             right = StringUtils.replace(right, "%" + key + "%", (String JavaDoc) variables.get(key));
68           }
69           variables.put(left.substring(4), right);
70         }
71         else {
72           for (String JavaDoc key : variables.keySet()) {
73             line = StringUtils.replace(line, "%" + key + "%", (String JavaDoc) variables.get(key));
74           }
75           command = line;
76         }
77       }
78       line = br.readLine();
79     }
80
81     return command;
82   }
83 }
84
Popular Tags