1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.filter.config.tools.utils; 25 26 //_______________________________________________ 27 // imports 28 29 import java.lang.*; 30 import java.util.*; 31 import java.io.*; 32 33 //_______________________________________________ 34 // definition 35 36 /** can be used to analyze command line parameters 37 * and merge it together with might existing config 38 * files. That provides the possibility to overwrite 39 * config values via command line parameter. 40 * 41 * 42 */ 43 public class ConfigHelper extends java.util.Properties 44 { 45 //___________________________________________ 46 // member 47 48 /** indicates an empty command line. */ 49 private boolean m_bEmpty = true; 50 51 //___________________________________________ 52 // ctor 53 54 //------------------------------------------- 55 /** initialize a new helper with the list of 56 * command line parameters and bind this new instance 57 * to a property file on disk. 58 * 59 * @param sPropFile 60 * name of the property file. 61 * If its set to null or an empty value 62 * it will be ignored. 63 * 64 * @param lCommandLineArgs 65 * the list of original command line arguments. 66 * 67 * @throws [Exception] 68 * in case the command line contains an unknown 69 * schema for specifiying parameters or the 70 * specified property file does not exists 71 * or seem to be corrupted. 72 */ ConfigHelper(java.lang.String sPropFile , java.lang.String[] lCommandLineArgs)73 public ConfigHelper(java.lang.String sPropFile , 74 java.lang.String[] lCommandLineArgs) 75 throws java.lang.Exception 76 { 77 // first load prop file, so its values can be overwritten 78 // by command line args later 79 // Do it only, if a valid file name was given. 80 // But in case this file name is wrong, throw an exception. 81 // So the outside code can react! 82 if ( 83 (sPropFile != null) && 84 (sPropFile.length() > 0 ) 85 ) 86 { 87 java.lang.ClassLoader aLoader = getClass().getClassLoader(); 88 java.io.InputStream aStream = aLoader.getResourceAsStream(sPropFile); 89 if (aStream == null) 90 aStream = new java.io.FileInputStream(sPropFile); 91 load(aStream); 92 } 93 94 int count = 0; 95 if (lCommandLineArgs != null) 96 count = lCommandLineArgs.length; 97 m_bEmpty = (count < 1); 98 99 for (int arg=0; arg<count; ++arg) 100 { 101 // is it a named-value argument? 102 // Note: We ignores double "=" signs! => search from left to right 103 int len = lCommandLineArgs[arg].length(); 104 int pos = lCommandLineArgs[arg].indexOf('='); 105 if (pos != -1) 106 { 107 java.lang.String sArg = lCommandLineArgs[arg].substring(0,pos); 108 java.lang.String sValue = lCommandLineArgs[arg].substring(pos+1); 109 setProperty(sArg, sValue); 110 continue; 111 } 112 113 // is it a boolean argument? 114 // Note: Because "--" and "-" will be interpreted as the same 115 // we search from right to left! 116 pos = lCommandLineArgs[arg].lastIndexOf('-'); 117 if (pos == -1) 118 pos = lCommandLineArgs[arg].lastIndexOf('/'); 119 if (pos != -1) 120 { 121 java.lang.String sArg = lCommandLineArgs[arg].substring(pos+1); 122 setProperty(sArg, java.lang.String.valueOf(true)); 123 continue; 124 } 125 126 // There is an unknown format used by this argument ... 127 throw new MalformedCommandLineException("Invalid command line detected. The argument \""+lCommandLineArgs[arg]+"\" use an unsupported format."); 128 } 129 } 130 131 //------------------------------------------- 132 /** indicates if the given command line includes 133 * a help request. 134 * 135 * @return True if there was an explicit help request. 136 */ isHelp()137 public synchronized boolean isHelp() 138 { 139 return ( 140 (containsKey("help")) || 141 (containsKey("?") ) || 142 (containsKey("h") ) 143 ); 144 } 145 146 //------------------------------------------- 147 /** indicates if the gioven command line was empty. 148 * 149 * @return True if there was an empty command line. 150 */ isEmpty()151 public synchronized boolean isEmpty() 152 { 153 return m_bEmpty; 154 } 155 156 //------------------------------------------- 157 /** returns the value of sProp as boolean value. 158 * 159 * @param sProp 160 * the name of the parameter. 161 * 162 * @return The boolean value of the requested property. 163 * 164 * @throw [NoSuchElementException] 165 * if the requested property does not exists. 166 */ getBoolean(java.lang.String sProp)167 public synchronized boolean getBoolean(java.lang.String sProp) 168 throws java.util.NoSuchElementException 169 { 170 java.lang.String sValue = getProperty(sProp); 171 if (sValue == null) 172 throw new java.util.NoSuchElementException("The requested config value \""+sProp+"\" does not exists!"); 173 return new java.lang.Boolean(sValue).booleanValue(); 174 } 175 getBoolean(java.lang.String sProp , boolean bDefault)176 public synchronized boolean getBoolean(java.lang.String sProp , 177 boolean bDefault) 178 { 179 java.lang.String sDefault = java.lang.String.valueOf(bDefault); 180 java.lang.String sValue = getProperty(sProp, sDefault); 181 return new java.lang.Boolean(sValue).booleanValue(); 182 } 183 184 //------------------------------------------- 185 /** returns the value of sProp as int value. 186 * 187 * @param sProp 188 * the name of the parameter. 189 * 190 * @return The int value of the requested property. 191 * 192 * @throw [NoSuchElementException] 193 * if the requested property does not exists. 194 */ getInt(java.lang.String sProp)195 public synchronized int getInt(java.lang.String sProp) 196 throws java.util.NoSuchElementException 197 { 198 java.lang.String sValue = getProperty(sProp); 199 if (sValue == null) 200 throw new java.util.NoSuchElementException("The requested config value \""+sProp+"\" does not exists!"); 201 return new java.lang.Integer(sValue).intValue(); 202 } 203 getInt(java.lang.String sProp , int nDefault)204 public synchronized int getInt(java.lang.String sProp , 205 int nDefault) 206 { 207 java.lang.String sDefault = java.lang.String.valueOf(nDefault); 208 java.lang.String sValue = getProperty(sProp, sDefault); 209 return new java.lang.Integer(sValue).intValue(); 210 } 211 212 //------------------------------------------- 213 /** returns the value of sProp as string value. 214 * 215 * @param sProp 216 * the name of the parameter. 217 * 218 * @return The string value of the requested property. 219 * 220 * @throw [NoSuchElementException] 221 * if the requested property does not exists. 222 */ getString(java.lang.String sProp)223 public synchronized java.lang.String getString(java.lang.String sProp) 224 throws java.util.NoSuchElementException 225 { 226 java.lang.String sValue = getProperty(sProp); 227 if (sValue == null) 228 throw new java.util.NoSuchElementException("The requested config value \""+sProp+"\" does not exists!"); 229 return sValue; 230 } 231 232 //------------------------------------------- 233 /** returns the value of sProp as string list value! 234 * 235 * @descr The delimiter must be well known and 236 * it must be clear if trailing/leading 237 * whitespaces must be ignored or not. 238 * 239 * @param sProp 240 * the name of the parameter. 241 * 242 * @param sDelim 243 * the delimiter, which must be used to split 244 * the config string value into an array. 245 * 246 * @param bTrim 247 * if its set to true, trailing and leading whitespace 248 * characters will be ommited. 249 * 250 * @param bDecode 251 * if its set to TRUE all liste items will be 252 * interpreted as "<xxx>" and converted to <xxx>! 253 * 254 * @return The string list value of the requested property. 255 * 256 * @throw [NoSuchElementException] 257 * if the requested property does not exists. 258 */ getStringList(java.lang.String sProp , java.lang.String sDelimiter, boolean bTrim , boolean bDecode )259 public synchronized java.util.Vector getStringList(java.lang.String sProp , 260 java.lang.String sDelimiter, 261 boolean bTrim , 262 boolean bDecode ) 263 throws java.util.NoSuchElementException 264 { 265 java.lang.String sValue = getProperty(sProp); 266 if (sValue == null) 267 throw new java.util.NoSuchElementException("The requested config value \""+sProp+"\" does not exists!"); 268 269 java.util.Vector lValue = new java.util.Vector(); 270 try 271 { 272 java.util.StringTokenizer lTokens = new java.util.StringTokenizer(sValue, sDelimiter); 273 while(lTokens.hasMoreTokens()) 274 { 275 java.lang.String sToken = lTokens.nextToken(); 276 // remove trailing/leading whitespaces 277 if (bTrim) 278 sToken = sToken.trim(); 279 // remove "" 280 if ( 281 (bDecode ) && 282 (sToken.indexOf("\"") == 0 ) && 283 (sToken.lastIndexOf("\"") == sToken.length()-1) 284 ) 285 { 286 sToken = sToken.substring(1, sToken.length()-1); 287 } 288 lValue.add(sToken); 289 } 290 } 291 catch(java.lang.Throwable ex) 292 { lValue.clear(); } 293 294 return lValue; 295 } 296 } 297