1 5 8 package com.teamkonzept.lib; 9 10 import java.io.*; 11 import java.util.*; 12 import java.net.*; 13 import org.apache.log4j.Category; 14 15 public class TKIniFile implements ConfigurationErrorCodes 16 { 17 private static Category cat = Category.getInstance(TKIniFile.class); 18 19 String theIniFile; 20 String emptyPat = " "; 21 String tabPat = " "; 22 23 27 public TKIniFile(String theIniFile) { 28 this.theIniFile = theIniFile; 29 } 30 31 35 InputStream openInputStream() throws TKException 36 { 37 try 38 { 39 URL url = getClass().getResource(theIniFile); 40 if (url == null) 41 { 42 cat.error("TKUploadField.ini is missing !"); 43 throw new TKConfigurationException("TKUploadField.ini fehlt",NO_PROPERTY_FILE,HIGH_SEVERITY,false, null); 44 } 45 return url.openStream(); 46 } 47 catch (IOException e) 48 { 49 cat.error("Can't locate ResourceFile " + theIniFile, e); 50 } 51 return null; 52 } 53 54 66 public String getValue(String key) throws TKException 67 { 68 69 try { 70 Properties props = new Properties(); props.load(openInputStream()); 73 String value = props.getProperty(key); 74 return value; 75 } 76 catch (IOException e) { 77 cat.error("Can't open Inifile: ", e); 78 return ""; 79 } 80 } 81 82 96 public TKHashtable getAll() throws TKException 97 { 98 99 TKHashtable myIniHash = new TKHashtable(); 100 try { 101 Properties props = new Properties(); props.load(openInputStream()); 104 105 Enumeration enumObject = props.propertyNames(); 106 while(enumObject.hasMoreElements()) { 107 String key = enumObject.nextElement().toString(); 108 String val = props.getProperty(key); 109 myIniHash.put(key,val); 110 } 111 112 return myIniHash; 113 } 114 catch (IOException e) { 115 cat.error("Can't open Inifile: ", e); 116 myIniHash = null; 117 return myIniHash; 118 } 119 120 } 121 122 152 153 public TKHashtable getAllSections() throws TKException 154 { 155 try { 156 String sectionPat = "["; 157 String comentPat = "#"; 158 String splitPat = "="; 159 String merke = ""; 160 String sectionKey = ""; 161 TKHashtable lineHash = null; 162 163 TKHashtable sectionsHash = new TKHashtable(); 164 165 BufferedReader dataInputStream = new BufferedReader(new InputStreamReader(openInputStream())); 167 String line; 168 169 while( (line = dataInputStream.readLine()) != null) { 170 String key= ""; 171 String value = ""; 172 173 if( (line.indexOf(sectionPat) > -1) && (!line.equals(""))) { 174 if( merke.equals("") ){ 175 merke = line; 176 sectionKey = line; 177 } 178 else { 179 merke = sectionKey; 180 181 if(! line.equals("[END]") ) 182 sectionKey = line; 183 184 if(line.equals("[END]") ) 186 sectionsHash.put(sectionKey.substring(1, sectionKey.length()-1), lineHash); 187 else 188 sectionsHash.put(merke.substring(1, merke.length()-1), lineHash); 189 190 } 191 192 if(! line.equals("[END]") ) 193 lineHash = new TKHashtable(); 194 195 } 196 else { 198 line = cutEmptyStrings(line); 199 if( (!line.equals("")) && (!line.startsWith(comentPat)) ){ 200 int idx = line.indexOf(splitPat); 201 if( idx > -1 ){ 202 key = line.substring( 0, idx ); 203 value = line.substring( idx+1, line.length() ); 204 lineHash.put(key,value); 205 } 206 } 207 } 208 } 209 210 return sectionsHash; 212 213 } 214 catch (IOException e) { 215 cat.error("Can't open Inifile: ", e); 216 return null; 217 } 218 219 } 220 221 230 public String cutEmptyStrings (String line) { 231 232 if( !line.equals("") ) { 233 234 while( line.startsWith(emptyPat) || line.startsWith(tabPat) ){ 235 236 if(line.startsWith(emptyPat) ) 237 line = emptyString(line); 238 239 if(line.startsWith(tabPat) ) 240 line = tabString(line); 241 242 } 243 } 244 return line; 245 } 246 247 256 public String emptyString (String line) { 257 258 while( line.startsWith(emptyPat) ) 259 line = line.substring(1, line.length()); 260 261 262 return line; 263 } 264 265 274 public String tabString (String line) { 275 276 while( line.startsWith(tabPat)) 277 line = line.substring(1, line.length()); 278 279 return line; 280 } 281 282 290 public TKHashtable getOneSection(String section) throws TKException 291 { 292 293 TKHashtable iniHash = getAllSections(); 294 TKHashtable contentHash = (TKHashtable) iniHash.get(section); 295 return contentHash; 296 297 } 298 299 309 public TKHashtable getOneSection(String section, TKHashtable iniHash) { 310 311 TKHashtable contentHash = (TKHashtable) iniHash.get(section); 312 return contentHash; 313 314 } 315 316 325 public TKVector getValsFromOneSection(String section) throws TKException 326 { 327 TKHashtable iniHash = getAllSections(); 328 return getKeysFromOneSection(section, iniHash); 329 330 } 331 332 341 public TKVector getValsFromOneSection(String section, TKHashtable iniHash) { 342 343 TKVector valVector = new TKVector(); 344 TKHashtable contentHash = (TKHashtable) iniHash.get(section); 345 346 Enumeration contentEnum = contentHash.keys(); 347 while( contentEnum.hasMoreElements() ) { 348 String contentKey = (String ) contentEnum.nextElement(); 349 String contentVal = (String ) contentHash.get(contentKey); 350 valVector.addElement( contentVal ); 351 } 352 353 return valVector; 354 } 355 356 366 367 public TKVector getKeysFromOneSection(String section) throws TKException 368 { 369 TKHashtable iniHash = getAllSections(); 370 return getKeysFromOneSection(section, iniHash); 371 372 } 373 374 375 385 386 public TKVector getKeysFromOneSection(String section, TKHashtable iniHash) { 387 388 TKVector keyVector = new TKVector(); 389 TKHashtable contentHash = (TKHashtable) iniHash.get(section); 390 391 Enumeration contentEnum = contentHash.keys(); 392 while( contentEnum.hasMoreElements() ) { 393 String contentKey = (String ) contentEnum.nextElement(); 394 keyVector.addElement( contentKey ); 395 } 396 397 return keyVector; 398 } 399 } 400 401 | Popular Tags |