xref: /AOO41X/main/qadevOOo/runner/helper/CfgParser.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package helper;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import lib.TestParameters;
30*cdf0e10cSrcweir import java.util.Properties;
31*cdf0e10cSrcweir import java.util.Enumeration;
32*cdf0e10cSrcweir import java.io.FileInputStream;
33*cdf0e10cSrcweir import util.PropertyName;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /**
36*cdf0e10cSrcweir  * This class parses the ini files and stores the data
37*cdf0e10cSrcweir  * <br>
38*cdf0e10cSrcweir  * inside TestParameters
39*cdf0e10cSrcweir  */
40*cdf0e10cSrcweir public class CfgParser
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     protected boolean debug = false;
44*cdf0e10cSrcweir     protected String iniFile = "";
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     public CfgParser(String ini)
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         if (ini != null)
49*cdf0e10cSrcweir         {
50*cdf0e10cSrcweir             this.iniFile = ini;
51*cdf0e10cSrcweir         }
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     public void getIniParameters(TestParameters param)
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         debug = param.DebugIsActive;
57*cdf0e10cSrcweir         Properties cfg = null;
58*cdf0e10cSrcweir         if (iniFile.equals(""))
59*cdf0e10cSrcweir         {
60*cdf0e10cSrcweir             //no iniFile given, search one in the users home directory
61*cdf0e10cSrcweir             cfg = getProperties(getDefaultFileName(true));
62*cdf0e10cSrcweir             //try to search the user dir if no iniFile could be found yet
63*cdf0e10cSrcweir             if (cfg == null)
64*cdf0e10cSrcweir             {
65*cdf0e10cSrcweir                 cfg = getProperties(getDefaultFileName(false));
66*cdf0e10cSrcweir             }
67*cdf0e10cSrcweir         }
68*cdf0e10cSrcweir         else
69*cdf0e10cSrcweir         {
70*cdf0e10cSrcweir             cfg = getProperties(iniFile);
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         if (cfg != null)
74*cdf0e10cSrcweir         {
75*cdf0e10cSrcweir             Enumeration cfgEnum = cfg.keys();
76*cdf0e10cSrcweir             while (cfgEnum.hasMoreElements())
77*cdf0e10cSrcweir             {
78*cdf0e10cSrcweir                 String pName = (String) cfgEnum.nextElement();
79*cdf0e10cSrcweir                 Object pValue = cfg.getProperty(pName);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir                 if (pValue instanceof String)
82*cdf0e10cSrcweir                 {
83*cdf0e10cSrcweir                     pValue = ((String) pValue).trim();
84*cdf0e10cSrcweir                 }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir                 param.put(pName.trim(), pValue);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir                 if (pName.equals(PropertyName.TEST_DOCUMENT_PATH))
89*cdf0e10cSrcweir                 {
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir                     param.put("DOCPTH", (String) pValue);
92*cdf0e10cSrcweir                     System.setProperty("DOCPTH", (String) pValue);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir                 }
95*cdf0e10cSrcweir                 else if (pName.equals(PropertyName.SRC_ROOT))
96*cdf0e10cSrcweir                 {
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir                     System.setProperty(pName, (String) pValue);
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir                 }
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         debug = param.DebugIsActive;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         //check for platform dependend parameters
107*cdf0e10cSrcweir         //this would have a $OperatingSystem as prefix
108*cdf0e10cSrcweir         String os = (String) param.get(PropertyName.OPERATING_SYSTEM);
109*cdf0e10cSrcweir         if (os != null && os.length() > 1)
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir             //found something that could be a prefex
113*cdf0e10cSrcweir             //check all parameters for this
114*cdf0e10cSrcweir             Enumeration keys = param.keys();
115*cdf0e10cSrcweir             while (keys.hasMoreElements())
116*cdf0e10cSrcweir             {
117*cdf0e10cSrcweir                 String key = (String) keys.nextElement();
118*cdf0e10cSrcweir                 if (key.startsWith(os))
119*cdf0e10cSrcweir                 {
120*cdf0e10cSrcweir                     Object oldValue = param.get(key);
121*cdf0e10cSrcweir                     String newKey = key.substring(os.length() + 1);
122*cdf0e10cSrcweir                     param.remove(key);
123*cdf0e10cSrcweir                     param.put(newKey, oldValue);
124*cdf0e10cSrcweir                 }
125*cdf0e10cSrcweir             }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     protected Properties getProperties(String name)
131*cdf0e10cSrcweir     {
132*cdf0e10cSrcweir         // get the resource file
133*cdf0e10cSrcweir         Properties prop = new Properties();
134*cdf0e10cSrcweir         if (debug)
135*cdf0e10cSrcweir         {
136*cdf0e10cSrcweir             System.out.println("Looking for " + name);
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir         try
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             FileInputStream propFile = new FileInputStream(name);
141*cdf0e10cSrcweir             prop.load(propFile);
142*cdf0e10cSrcweir             System.out.println("Parsing properties from " + name);
143*cdf0e10cSrcweir             propFile.close();
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir         catch (Exception e)
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             try
148*cdf0e10cSrcweir             {
149*cdf0e10cSrcweir                 java.net.URL url = this.getClass().getResource("/" + name);
150*cdf0e10cSrcweir                 if (url != null)
151*cdf0e10cSrcweir                 {
152*cdf0e10cSrcweir                     System.out.println("Parsing properties from " + name);
153*cdf0e10cSrcweir                     java.net.URLConnection connection = url.openConnection();
154*cdf0e10cSrcweir                     java.io.InputStream in = connection.getInputStream();
155*cdf0e10cSrcweir                     prop.load(in);
156*cdf0e10cSrcweir                 }
157*cdf0e10cSrcweir             }
158*cdf0e10cSrcweir             catch (Exception ex)
159*cdf0e10cSrcweir             {
160*cdf0e10cSrcweir                 //Exception while reading prop-file, returning null
161*cdf0e10cSrcweir                 return null;
162*cdf0e10cSrcweir             }
163*cdf0e10cSrcweir         }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         return prop;
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     protected String getDefaultFileName(boolean home)
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         String fileSeparator = System.getProperty("file.separator");
171*cdf0e10cSrcweir         String path = "";
172*cdf0e10cSrcweir         if (home)
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             //look inside the home directory
175*cdf0e10cSrcweir             path = System.getProperty("user.home");
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         else
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir             path = System.getProperty("user.dir");
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir         if (fileSeparator.equals("/"))
182*cdf0e10cSrcweir         {
183*cdf0e10cSrcweir             //suppose I'm on Unix-platform
184*cdf0e10cSrcweir             return path + fileSeparator + ".runner.props";
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir         else
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             //suppose I'm on Windows
189*cdf0e10cSrcweir             return path + fileSeparator + "runner.props";
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir }
193