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