Update ooo320-m1
[ooovba.git] / qadevOOo / runner / helper / CfgParser.java
blob2f9bff1e74f56872d2d31ad8226a69687b4cba4c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CfgParser.java,v $
10 * $Revision: 1.8.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package helper;
32 import lib.TestParameters;
33 import java.util.Properties;
34 import java.util.Enumeration;
35 import java.io.FileInputStream;
36 import util.PropertyName;
38 /**
39 * This class parses the ini files and stores the data
40 * <br>
41 * inside TestParameters
43 public class CfgParser
46 protected boolean debug = false;
47 protected String iniFile = "";
49 public CfgParser(String ini)
51 if (ini != null)
53 this.iniFile = ini;
57 public void getIniParameters(TestParameters param)
59 debug = param.DebugIsActive;
60 Properties cfg = null;
61 if (iniFile.equals(""))
63 //no iniFile given, search one in the users home directory
64 cfg = getProperties(getDefaultFileName(true));
65 //try to search the user dir if no iniFile could be found yet
66 if (cfg == null)
68 cfg = getProperties(getDefaultFileName(false));
71 else
73 cfg = getProperties(iniFile);
76 if (cfg != null)
78 Enumeration cfgEnum = cfg.keys();
79 while (cfgEnum.hasMoreElements())
81 String pName = (String) cfgEnum.nextElement();
82 Object pValue = cfg.getProperty(pName);
84 if (pValue instanceof String)
86 pValue = ((String) pValue).trim();
89 param.put(pName.trim(), pValue);
91 if (pName.equals(PropertyName.TEST_DOCUMENT_PATH))
94 param.put("DOCPTH", (String) pValue);
95 System.setProperty("DOCPTH", (String) pValue);
98 else if (pName.equals(PropertyName.SRC_ROOT))
101 System.setProperty(pName, (String) pValue);
107 debug = param.DebugIsActive;
109 //check for platform dependend parameters
110 //this would have a $OperatingSystem as prefix
111 String os = (String) param.get(PropertyName.OPERATING_SYSTEM);
112 if (os != null && os.length() > 1)
115 //found something that could be a prefex
116 //check all parameters for this
117 Enumeration keys = param.keys();
118 while (keys.hasMoreElements())
120 String key = (String) keys.nextElement();
121 if (key.startsWith(os))
123 Object oldValue = param.get(key);
124 String newKey = key.substring(os.length() + 1);
125 param.remove(key);
126 param.put(newKey, oldValue);
133 protected Properties getProperties(String name)
135 // get the resource file
136 Properties prop = new Properties();
137 if (debug)
139 System.out.println("Looking for " + name);
143 FileInputStream propFile = new FileInputStream(name);
144 prop.load(propFile);
145 System.out.println("Parsing properties from " + name);
146 propFile.close();
148 catch (Exception e)
152 java.net.URL url = this.getClass().getResource("/" + name);
153 if (url != null)
155 System.out.println("Parsing properties from " + name);
156 java.net.URLConnection connection = url.openConnection();
157 java.io.InputStream in = connection.getInputStream();
158 prop.load(in);
161 catch (Exception ex)
163 //Exception while reading prop-file, returning null
164 return null;
168 return prop;
171 protected String getDefaultFileName(boolean home)
173 String fileSeparator = System.getProperty("file.separator");
174 String path = "";
175 if (home)
177 //look inside the home directory
178 path = System.getProperty("user.home");
180 else
182 path = System.getProperty("user.dir");
184 if (fileSeparator.equals("/"))
186 //suppose I'm on Unix-platform
187 return path + fileSeparator + ".runner.props";
189 else
191 //suppose I'm on Windows
192 return path + fileSeparator + "runner.props";