Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / helper / CfgParser.java
blobf4f919c5cfc44c7083f27243356103333507dd58
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package helper;
29 import lib.TestParameters;
30 import java.util.Properties;
31 import java.util.Enumeration;
32 import java.io.FileInputStream;
33 import util.PropertyName;
35 /**
36 * This class parses the ini files and stores the data
37 * <br>
38 * inside TestParameters
40 public class CfgParser
43 protected boolean debug = false;
44 protected String iniFile = "";
46 public CfgParser(String ini)
48 if (ini != null)
50 this.iniFile = ini;
54 public void getIniParameters(TestParameters param)
56 debug = param.DebugIsActive;
57 Properties cfg = null;
58 if (iniFile.equals(""))
60 //no iniFile given, search one in the users home directory
61 cfg = getProperties(getDefaultFileName(true));
62 //try to search the user dir if no iniFile could be found yet
63 if (cfg == null)
65 cfg = getProperties(getDefaultFileName(false));
68 else
70 cfg = getProperties(iniFile);
73 if (cfg != null)
75 Enumeration cfgEnum = cfg.keys();
76 while (cfgEnum.hasMoreElements())
78 String pName = (String) cfgEnum.nextElement();
79 Object pValue = cfg.getProperty(pName);
81 if (pValue instanceof String)
83 pValue = ((String) pValue).trim();
86 param.put(pName.trim(), pValue);
88 if (pName.equals(PropertyName.TEST_DOCUMENT_PATH))
91 param.put("DOCPTH", (String) pValue);
92 System.setProperty("DOCPTH", (String) pValue);
95 else if (pName.equals(PropertyName.SRC_ROOT))
98 System.setProperty(pName, (String) pValue);
104 debug = param.DebugIsActive;
106 //check for platform dependend parameters
107 //this would have a $OperatingSystem as prefix
108 String os = (String) param.get(PropertyName.OPERATING_SYSTEM);
109 if (os != null && os.length() > 1)
112 //found something that could be a prefex
113 //check all parameters for this
114 Enumeration keys = param.keys();
115 while (keys.hasMoreElements())
117 String key = (String) keys.nextElement();
118 if (key.startsWith(os))
120 Object oldValue = param.get(key);
121 String newKey = key.substring(os.length() + 1);
122 param.remove(key);
123 param.put(newKey, oldValue);
130 protected Properties getProperties(String name)
132 // get the resource file
133 Properties prop = new Properties();
134 if (debug)
136 System.out.println("Looking for " + name);
140 FileInputStream propFile = new FileInputStream(name);
141 prop.load(propFile);
142 System.out.println("Parsing properties from " + name);
143 propFile.close();
145 catch (Exception e)
149 java.net.URL url = this.getClass().getResource("/" + name);
150 if (url != null)
152 System.out.println("Parsing properties from " + name);
153 java.net.URLConnection connection = url.openConnection();
154 java.io.InputStream in = connection.getInputStream();
155 prop.load(in);
158 catch (Exception ex)
160 //Exception while reading prop-file, returning null
161 return null;
165 return prop;
168 protected String getDefaultFileName(boolean home)
170 String fileSeparator = System.getProperty("file.separator");
171 String path = "";
172 if (home)
174 //look inside the home directory
175 path = System.getProperty("user.home");
177 else
179 path = System.getProperty("user.dir");
181 if (fileSeparator.equals("/"))
183 //suppose I'm on Unix-platform
184 return path + fileSeparator + ".runner.props";
186 else
188 //suppose I'm on Windows
189 return path + fileSeparator + "runner.props";