1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 ************************************************************************/
32 import lib
.TestParameters
;
33 import java
.util
.Properties
;
34 import java
.util
.Enumeration
;
35 import java
.io
.FileInputStream
;
36 import util
.PropertyName
;
39 * This class parses the ini files and stores the data
41 * inside TestParameters
43 public class CfgParser
46 protected boolean debug
= false;
47 protected String iniFile
= "";
49 public CfgParser(String 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
68 cfg
= getProperties(getDefaultFileName(false));
73 cfg
= getProperties(iniFile
);
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);
126 param
.put(newKey
, oldValue
);
133 protected Properties
getProperties(String name
)
135 // get the resource file
136 Properties prop
= new Properties();
139 System
.out
.println("Looking for " + name
);
143 FileInputStream propFile
= new FileInputStream(name
);
145 System
.out
.println("Parsing properties from " + name
);
152 java
.net
.URL url
= this.getClass().getResource("/" + name
);
155 System
.out
.println("Parsing properties from " + name
);
156 java
.net
.URLConnection connection
= url
.openConnection();
157 java
.io
.InputStream in
= connection
.getInputStream();
163 //Exception while reading prop-file, returning null
171 protected String
getDefaultFileName(boolean home
)
173 String fileSeparator
= System
.getProperty("file.separator");
177 //look inside the home directory
178 path
= System
.getProperty("user.home");
182 path
= System
.getProperty("user.dir");
184 if (fileSeparator
.equals("/"))
186 //suppose I'm on Unix-platform
187 return path
+ fileSeparator
+ ".runner.props";
191 //suppose I'm on Windows
192 return path
+ fileSeparator
+ "runner.props";