1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 package helper;
28 
29 import lib.TestParameters;
30 import java.util.Properties;
31 import java.util.Enumeration;
32 import java.io.FileInputStream;
33 import util.PropertyName;
34 
35 /**
36  * This class parses the ini files and stores the data
37  * <br>
38  * inside TestParameters
39  */
40 public class CfgParser
41 {
42 
43     protected boolean debug = false;
44     protected String iniFile = "";
45 
46     public CfgParser(String ini)
47     {
48         if (ini != null)
49         {
50             this.iniFile = ini;
51         }
52     }
53 
54     public void getIniParameters(TestParameters param)
55     {
56         debug = param.DebugIsActive;
57         Properties cfg = null;
58         if (iniFile.equals(""))
59         {
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)
64             {
65                 cfg = getProperties(getDefaultFileName(false));
66             }
67         }
68         else
69         {
70             cfg = getProperties(iniFile);
71         }
72 
73         if (cfg != null)
74         {
75             Enumeration cfgEnum = cfg.keys();
76             while (cfgEnum.hasMoreElements())
77             {
78                 String pName = (String) cfgEnum.nextElement();
79                 Object pValue = cfg.getProperty(pName);
80 
81                 if (pValue instanceof String)
82                 {
83                     pValue = ((String) pValue).trim();
84                 }
85 
86                 param.put(pName.trim(), pValue);
87 
88                 if (pName.equals(PropertyName.TEST_DOCUMENT_PATH))
89                 {
90 
91                     param.put("DOCPTH", (String) pValue);
92                     System.setProperty("DOCPTH", (String) pValue);
93 
94                 }
95                 else if (pName.equals(PropertyName.SRC_ROOT))
96                 {
97 
98                     System.setProperty(pName, (String) pValue);
99 
100                 }
101             }
102         }
103 
104         debug = param.DebugIsActive;
105 
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)
110         {
111 
112             //found something that could be a prefex
113             //check all parameters for this
114             Enumeration keys = param.keys();
115             while (keys.hasMoreElements())
116             {
117                 String key = (String) keys.nextElement();
118                 if (key.startsWith(os))
119                 {
120                     Object oldValue = param.get(key);
121                     String newKey = key.substring(os.length() + 1);
122                     param.remove(key);
123                     param.put(newKey, oldValue);
124                 }
125             }
126 
127         }
128     }
129 
130     protected Properties getProperties(String name)
131     {
132         // get the resource file
133         Properties prop = new Properties();
134         if (debug)
135         {
136             System.out.println("Looking for " + name);
137         }
138         try
139         {
140             FileInputStream propFile = new FileInputStream(name);
141             prop.load(propFile);
142             System.out.println("Parsing properties from " + name);
143             propFile.close();
144         }
145         catch (Exception e)
146         {
147             try
148             {
149                 java.net.URL url = this.getClass().getResource("/" + name);
150                 if (url != null)
151                 {
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);
156                 }
157             }
158             catch (Exception ex)
159             {
160                 //Exception while reading prop-file, returning null
161                 return null;
162             }
163         }
164 
165         return prop;
166     }
167 
168     protected String getDefaultFileName(boolean home)
169     {
170         String fileSeparator = System.getProperty("file.separator");
171         String path = "";
172         if (home)
173         {
174             //look inside the home directory
175             path = System.getProperty("user.home");
176         }
177         else
178         {
179             path = System.getProperty("user.dir");
180         }
181         if (fileSeparator.equals("/"))
182         {
183             //suppose I'm on Unix-platform
184             return path + fileSeparator + ".runner.props";
185         }
186         else
187         {
188             //suppose I'm on Windows
189             return path + fileSeparator + "runner.props";
190         }
191     }
192 }
193