Update ooo320-m1
[ooovba.git] / transex3 / source / filter / utils / ConfigHelper.java
blob5cffddfc6424534d9d64e87472a97905043fa753
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigHelper.java,v $
10 * $Revision: 1.7 $
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 //_______________________________________________
34 // imports
36 import java.lang.*;
37 import java.util.*;
38 import java.io.*;
40 //_______________________________________________
41 // definition
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 //___________________________________________
53 // member
55 /** indicates an empty command line. */
56 private boolean m_bEmpty = true;
58 //___________________________________________
59 // ctor
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.
66 * @param sPropFile
67 * name of the property file.
68 * If its set to null or an empty value
69 * it will be ignored.
71 * @param lCommandLineArgs
72 * the list of original command line arguments.
74 * @throws [Exception]
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!
89 if (
90 (sPropFile != null) &&
91 (sPropFile.length() > 0 )
94 java.lang.ClassLoader aLoader = getClass().getClassLoader();
95 java.io.InputStream aStream = aLoader.getResourceAsStream(sPropFile);
96 if (aStream == null)
97 aStream = new java.io.FileInputStream(sPropFile);
98 load(aStream);
101 int count = 0;
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('=');
112 if (pos != -1)
114 java.lang.String sArg = lCommandLineArgs[arg].substring(0,pos);
115 java.lang.String sValue = lCommandLineArgs[arg].substring(pos+1);
116 setProperty(sArg, sValue);
117 continue;
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('-');
124 if (pos == -1)
125 pos = lCommandLineArgs[arg].lastIndexOf('/');
126 if (pos != -1)
128 java.lang.String sArg = lCommandLineArgs[arg].substring(pos+1);
129 setProperty(sArg, java.lang.String.valueOf(true));
130 continue;
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
140 * a help request.
142 * @return True if there was an explicit help request.
144 public synchronized boolean isHelp()
146 return (
147 (containsKey("help")) ||
148 (containsKey("?") ) ||
149 (containsKey("h") )
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()
160 return m_bEmpty;
163 //-------------------------------------------
164 /** returns the value of sProp as boolean value.
166 * @param sProp
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);
178 if (sValue == null)
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 ,
184 boolean bDefault)
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.
194 * @param sProp
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);
206 if (sValue == null)
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 ,
212 int nDefault)
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.
222 * @param sProp
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);
234 if (sValue == null)
235 throw new java.util.NoSuchElementException("The requested config value \""+sProp+"\" does not exists!");
236 return sValue;
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.
246 * @param sProp
247 * the name of the parameter.
249 * @param sDelim
250 * the delimiter, which must be used to split
251 * the config string value into an array.
253 * @param bTrim
254 * if its set to true, trailing and leading whitespace
255 * characters will be ommited.
257 * @param bDecode
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,
268 boolean bTrim ,
269 boolean bDecode )
270 throws java.util.NoSuchElementException
272 java.lang.String sValue = getProperty(sProp);
273 if (sValue == null)
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
284 if (bTrim)
285 sToken = sToken.trim();
286 // remove ""
287 if (
288 (bDecode ) &&
289 (sToken.indexOf("\"") == 0 ) &&
290 (sToken.lastIndexOf("\"") == sToken.length()-1)
293 sToken = sToken.substring(1, sToken.length()-1);
295 lValue.add(sToken);
298 catch(java.lang.Throwable ex)
299 { lValue.clear(); }
301 return lValue;