CTutil Class Source Code |
The utility methods in the following sample code are called by several of the examples in Implementing a New Verification Point. The code for the CTutil
class is included in this appendix.
The CTutil
class is in rational_ct.jar
.
public class CTutil { public static boolean csvGetNextElement( StringBuffer bufCSV, StringBuffer bufElement ) { String sCSV = bufCSV.toString(); int iCommaIndex = sCSV.indexOf(','); if (iCommaIndex == -1) { bufElement.insert(0, sCSV); bufElement.setLength(sCSV.length()); bufCSV.setLength(0); return false; } else { bufElement.insert(0, sCSV.substring(0, iCommaIndex)); bufElement.setLength(iCommaIndex); bufCSV.insert(0, sCSV.substring(iCommaIndex+1)); bufCSV.setLength( sCSV.length() - iCommaIndex - 1 ); return true; } } // This function reads an INI file and returns a hashtable. The // hashtable maps strings (section names from the INI file) to // hashtables of those sections. These section hastables map // strings (keys from the section) to strings (values from those // keys.) You can pass the hashtable constructed by this function // to readPrivateProfileString(), which returns values from // the .INI file. public static Hashtable mapINIfile(InputStream in) throws IOException { BufferedReader brIn = new BufferedReader ( new InputStreamReader ( in )); String sLine = ""; String sKey = ""; String sValue = ""; String sSection = ""; int iEquals = 0; Hashtable tblINI = new Hashtable(); Hashtable tblSection = new Hashtable(); // Read the file one line at a time. for ( sLine = brIn.readLine(); sLine != null && sLine.trim() != null; sLine = brIn.readLine() ) { sLine = sLine.trim(); if ( sLine.length() == 0 ) { continue; } else if ( sLine.charAt(0) == '[' ) { // This is a new category. If this isn't the first one, // write the previous one into the hashtable. if ( !tblSection.isEmpty() ) { tblINI.put(sSection, tblSection); tblSection = new Hashtable(); } // Store the new Section name. sSection = sLine.substring(1, sLine.length()-1); } else { // Find the separator between the key and the value. iEquals = sLine.indexOf('='); if ( iEquals < 0 ) { // The entry in the INI file doesn't match INI spec. // ignore it and continue reading the file. continue; } else if ( iEquals == 0 ) { // There is no Key name. Invalid INI format. // ignore and continue. continue; } else if ( iEquals == sLine.length()-1 ) { // Key with no Value. Set the Value to null. sKey = sLine.substring(0, iEquals); sValue = ""; tblSection.put(sKey, sValue); } else { // Parse the line. sKey = sLine.substring(0, iEquals); sValue = sLine.substring(iEquals+1); // Add the entry to the table for this section. tblSection.put(sKey, sValue); } } } if ( !tblSection.isEmpty() ) { tblINI.put(sSection, tblSection); } if ( !tblINI.isEmpty() ) return tblINI; else return null; } public static String readPrivateProfileString( Hashtable tblMap, String sSection, String sKey ) { String sValue = ""; Hashtable tblSection = (Hashtable) tblMap.get(sSection); if ( tblSection != null ) { sValue = (String) tblSection.get(sKey); if ( sValue == null ) sValue = ""; } return sValue; } }
Rational Test Script Services for Java | Rational Software Corporation |
Copyright (c) 2003, Rational Software Corporation | http://www.rational.com support@rational.com info@rational.com |