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: ConfigHelper.java,v $
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 **********************************************************************_*/
31 package com
.sun
.star
.filter
.config
.tools
.utils
;
33 //_______________________________________________
40 //_______________________________________________
43 /** can be used to analyze command line parameters
44 * and merge it together with might existing config
45 * files. That provides the possibility to overwrite
46 * config values via command line parameter.
50 public class ConfigHelper
extends java
.util
.Properties
52 //___________________________________________
55 /** indicates an empty command line. */
56 private boolean m_bEmpty
= true;
58 //___________________________________________
61 //-------------------------------------------
62 /** initialize a new helper with the list of
63 * command line parameters and bind this new instance
64 * to a property file on disk.
67 * name of the property file.
68 * If its set to null or an empty value
71 * @param lCommandLineArgs
72 * the list of original command line arguments.
75 * in case the command line contains an unknown
76 * schema for specifiying parameters or the
77 * specified property file does not exists
78 * or seem to be corrupted.
80 public ConfigHelper(java
.lang
.String sPropFile
,
81 java
.lang
.String
[] lCommandLineArgs
)
82 throws java
.lang
.Exception
84 // first load prop file, so its values can be overwritten
85 // by command line args later
86 // Do it only, if a valid file name was given.
87 // But in case this file name is wrong, throw an exception.
88 // So the outside code can react!
90 (sPropFile
!= null) &&
91 (sPropFile
.length() > 0 )
94 java
.lang
.ClassLoader aLoader
= getClass().getClassLoader();
95 java
.io
.InputStream aStream
= aLoader
.getResourceAsStream(sPropFile
);
97 aStream
= new java
.io
.FileInputStream(sPropFile
);
102 if (lCommandLineArgs
!= null)
103 count
= lCommandLineArgs
.length
;
104 m_bEmpty
= (count
< 1);
106 for (int arg
=0; arg
<count
; ++arg
)
108 // is it a named-value argument?
109 // Note: We ignores double "=" signs! => search from left to right
110 int len
= lCommandLineArgs
[arg
].length();
111 int pos
= lCommandLineArgs
[arg
].indexOf('=');
114 java
.lang
.String sArg
= lCommandLineArgs
[arg
].substring(0,pos
);
115 java
.lang
.String sValue
= lCommandLineArgs
[arg
].substring(pos
+1);
116 setProperty(sArg
, sValue
);
120 // is it a boolean argument?
121 // Note: Because "--" and "-" will be interpreted as the same
122 // we search from right to left!
123 pos
= lCommandLineArgs
[arg
].lastIndexOf('-');
125 pos
= lCommandLineArgs
[arg
].lastIndexOf('/');
128 java
.lang
.String sArg
= lCommandLineArgs
[arg
].substring(pos
+1);
129 setProperty(sArg
, java
.lang
.String
.valueOf(true));
133 // There is an unknown format used by this argument ...
134 throw new MalformedCommandLineException("Invalid command line detected. The argument \""+lCommandLineArgs
[arg
]+"\" use an unsupported format.");
138 //-------------------------------------------
139 /** indicates if the given command line includes
142 * @return True if there was an explicit help request.
144 public synchronized boolean isHelp()
147 (containsKey("help")) ||
148 (containsKey("?") ) ||
153 //-------------------------------------------
154 /** indicates if the gioven command line was empty.
156 * @return True if there was an empty command line.
158 public synchronized boolean isEmpty()
163 //-------------------------------------------
164 /** returns the value of sProp as boolean value.
167 * the name of the parameter.
169 * @return The boolean value of the requested property.
171 * @throw [NoSuchElementException]
172 * if the requested property does not exists.
174 public synchronized boolean getBoolean(java
.lang
.String sProp
)
175 throws java
.util
.NoSuchElementException
177 java
.lang
.String sValue
= getProperty(sProp
);
179 throw new java
.util
.NoSuchElementException("The requested config value \""+sProp
+"\" does not exists!");
180 return new java
.lang
.Boolean(sValue
).booleanValue();
183 public synchronized boolean getBoolean(java
.lang
.String sProp
,
186 java
.lang
.String sDefault
= java
.lang
.String
.valueOf(bDefault
);
187 java
.lang
.String sValue
= getProperty(sProp
, sDefault
);
188 return new java
.lang
.Boolean(sValue
).booleanValue();
191 //-------------------------------------------
192 /** returns the value of sProp as int value.
195 * the name of the parameter.
197 * @return The int value of the requested property.
199 * @throw [NoSuchElementException]
200 * if the requested property does not exists.
202 public synchronized int getInt(java
.lang
.String sProp
)
203 throws java
.util
.NoSuchElementException
205 java
.lang
.String sValue
= getProperty(sProp
);
207 throw new java
.util
.NoSuchElementException("The requested config value \""+sProp
+"\" does not exists!");
208 return new java
.lang
.Integer(sValue
).intValue();
211 public synchronized int getInt(java
.lang
.String sProp
,
214 java
.lang
.String sDefault
= java
.lang
.String
.valueOf(nDefault
);
215 java
.lang
.String sValue
= getProperty(sProp
, sDefault
);
216 return new java
.lang
.Integer(sValue
).intValue();
219 //-------------------------------------------
220 /** returns the value of sProp as string value.
223 * the name of the parameter.
225 * @return The string value of the requested property.
227 * @throw [NoSuchElementException]
228 * if the requested property does not exists.
230 public synchronized java
.lang
.String
getString(java
.lang
.String sProp
)
231 throws java
.util
.NoSuchElementException
233 java
.lang
.String sValue
= getProperty(sProp
);
235 throw new java
.util
.NoSuchElementException("The requested config value \""+sProp
+"\" does not exists!");
239 //-------------------------------------------
240 /** returns the value of sProp as string list value!
242 * @descr The delimiter must be well known and
243 * it must be clear if trailing/leading
244 * whitespaces must be ignored or not.
247 * the name of the parameter.
250 * the delimiter, which must be used to split
251 * the config string value into an array.
254 * if its set to true, trailing and leading whitespace
255 * characters will be ommited.
258 * if its set to TRUE all liste items will be
259 * interpreted as "<xxx>" and converted to <xxx>!
261 * @return The string list value of the requested property.
263 * @throw [NoSuchElementException]
264 * if the requested property does not exists.
266 public synchronized java
.util
.Vector
getStringList(java
.lang
.String sProp
,
267 java
.lang
.String sDelimiter
,
270 throws java
.util
.NoSuchElementException
272 java
.lang
.String sValue
= getProperty(sProp
);
274 throw new java
.util
.NoSuchElementException("The requested config value \""+sProp
+"\" does not exists!");
276 java
.util
.Vector lValue
= new java
.util
.Vector();
279 java
.util
.StringTokenizer lTokens
= new java
.util
.StringTokenizer(sValue
, sDelimiter
);
280 while(lTokens
.hasMoreTokens())
282 java
.lang
.String sToken
= lTokens
.nextToken();
283 // remove trailing/leading whitespaces
285 sToken
= sToken
.trim();
289 (sToken
.indexOf("\"") == 0 ) &&
290 (sToken
.lastIndexOf("\"") == sToken
.length()-1)
293 sToken
= sToken
.substring(1, sToken
.length()-1);
298 catch(java
.lang
.Throwable ex
)