1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import lib.TestParameters;
26cdf0e10cSrcweir import java.util.Properties;
27cdf0e10cSrcweir import java.util.Enumeration;
28cdf0e10cSrcweir import java.io.FileInputStream;
29cdf0e10cSrcweir import util.PropertyName;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir  * This class parses the ini files and stores the data
33cdf0e10cSrcweir  * <br>
34cdf0e10cSrcweir  * inside TestParameters
35cdf0e10cSrcweir  */
36cdf0e10cSrcweir public class CfgParser
37cdf0e10cSrcweir {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     protected boolean debug = false;
40cdf0e10cSrcweir     protected String iniFile = "";
41cdf0e10cSrcweir 
CfgParser(String ini)42cdf0e10cSrcweir     public CfgParser(String ini)
43cdf0e10cSrcweir     {
44cdf0e10cSrcweir         if (ini != null)
45cdf0e10cSrcweir         {
46cdf0e10cSrcweir             this.iniFile = ini;
47cdf0e10cSrcweir         }
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
getIniParameters(TestParameters param)50cdf0e10cSrcweir     public void getIniParameters(TestParameters param)
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         debug = param.DebugIsActive;
53cdf0e10cSrcweir         Properties cfg = null;
54cdf0e10cSrcweir         if (iniFile.equals(""))
55cdf0e10cSrcweir         {
56cdf0e10cSrcweir             //no iniFile given, search one in the users home directory
57cdf0e10cSrcweir             cfg = getProperties(getDefaultFileName(true));
58cdf0e10cSrcweir             //try to search the user dir if no iniFile could be found yet
59cdf0e10cSrcweir             if (cfg == null)
60cdf0e10cSrcweir             {
61cdf0e10cSrcweir                 cfg = getProperties(getDefaultFileName(false));
62cdf0e10cSrcweir             }
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir         else
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             cfg = getProperties(iniFile);
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         if (cfg != null)
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             Enumeration cfgEnum = cfg.keys();
72cdf0e10cSrcweir             while (cfgEnum.hasMoreElements())
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 String pName = (String) cfgEnum.nextElement();
75cdf0e10cSrcweir                 Object pValue = cfg.getProperty(pName);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir                 if (pValue instanceof String)
78cdf0e10cSrcweir                 {
79cdf0e10cSrcweir                     pValue = ((String) pValue).trim();
80cdf0e10cSrcweir                 }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir                 param.put(pName.trim(), pValue);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir                 if (pName.equals(PropertyName.TEST_DOCUMENT_PATH))
85cdf0e10cSrcweir                 {
86cdf0e10cSrcweir 
87cdf0e10cSrcweir                     param.put("DOCPTH", (String) pValue);
88cdf0e10cSrcweir                     System.setProperty("DOCPTH", (String) pValue);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir                 }
91cdf0e10cSrcweir                 else if (pName.equals(PropertyName.SRC_ROOT))
92cdf0e10cSrcweir                 {
93cdf0e10cSrcweir 
94cdf0e10cSrcweir                     System.setProperty(pName, (String) pValue);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir                 }
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         debug = param.DebugIsActive;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         //check for platform dependend parameters
103cdf0e10cSrcweir         //this would have a $OperatingSystem as prefix
104cdf0e10cSrcweir         String os = (String) param.get(PropertyName.OPERATING_SYSTEM);
105cdf0e10cSrcweir         if (os != null && os.length() > 1)
106cdf0e10cSrcweir         {
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             //found something that could be a prefex
109cdf0e10cSrcweir             //check all parameters for this
110cdf0e10cSrcweir             Enumeration keys = param.keys();
111cdf0e10cSrcweir             while (keys.hasMoreElements())
112cdf0e10cSrcweir             {
113cdf0e10cSrcweir                 String key = (String) keys.nextElement();
114cdf0e10cSrcweir                 if (key.startsWith(os))
115cdf0e10cSrcweir                 {
116cdf0e10cSrcweir                     Object oldValue = param.get(key);
117cdf0e10cSrcweir                     String newKey = key.substring(os.length() + 1);
118cdf0e10cSrcweir                     param.remove(key);
119cdf0e10cSrcweir                     param.put(newKey, oldValue);
120cdf0e10cSrcweir                 }
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
getProperties(String name)126cdf0e10cSrcweir     protected Properties getProperties(String name)
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         // get the resource file
129cdf0e10cSrcweir         Properties prop = new Properties();
130cdf0e10cSrcweir         if (debug)
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir             System.out.println("Looking for " + name);
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir         try
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             FileInputStream propFile = new FileInputStream(name);
137cdf0e10cSrcweir             prop.load(propFile);
138cdf0e10cSrcweir             System.out.println("Parsing properties from " + name);
139cdf0e10cSrcweir             propFile.close();
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir         catch (Exception e)
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             try
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 java.net.URL url = this.getClass().getResource("/" + name);
146cdf0e10cSrcweir                 if (url != null)
147cdf0e10cSrcweir                 {
148cdf0e10cSrcweir                     System.out.println("Parsing properties from " + name);
149cdf0e10cSrcweir                     java.net.URLConnection connection = url.openConnection();
150cdf0e10cSrcweir                     java.io.InputStream in = connection.getInputStream();
151cdf0e10cSrcweir                     prop.load(in);
152cdf0e10cSrcweir                 }
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir             catch (Exception ex)
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir                 //Exception while reading prop-file, returning null
157cdf0e10cSrcweir                 return null;
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         return prop;
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
getDefaultFileName(boolean home)164cdf0e10cSrcweir     protected String getDefaultFileName(boolean home)
165cdf0e10cSrcweir     {
166cdf0e10cSrcweir         String fileSeparator = System.getProperty("file.separator");
167cdf0e10cSrcweir         String path = "";
168cdf0e10cSrcweir         if (home)
169cdf0e10cSrcweir         {
170cdf0e10cSrcweir             //look inside the home directory
171cdf0e10cSrcweir             path = System.getProperty("user.home");
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir         else
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             path = System.getProperty("user.dir");
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir         if (fileSeparator.equals("/"))
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir             //suppose I'm on Unix-platform
180cdf0e10cSrcweir             return path + fileSeparator + ".runner.props";
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         else
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             //suppose I'm on Windows
185cdf0e10cSrcweir             return path + fileSeparator + "runner.props";
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir }
189