O2Plib.misc
Class Switches

java.lang.Object
  extended by O2Plib.misc.Switches

public class Switches
extends java.lang.Object

Class Switches parses and manipulates command line switch lists.

This code was derived and refactored from the Open2Dprot Seg2Dgel program.

This work was produced by Peter Lemkin of the National Cancer Institute, an agency of the United States Government. As a work of the United States Government there is no associated copyright. It is offered as open source software under the Mozilla Public License (version 1.1) subject to the limitations noted in the accompanying LEGAL file. This notice must be included with the code. The Open2Dprot Mozilla and Legal files are available on http://Open2Dprot.sourceforge.net/.

Version:
$Date: 2005/10/21 16:33:51 $ $Revision: 1.22 $
Author:
P. Lemkin, NCI-Frederick, Frederick, MD, 21702
See Also:
Open2Dprot Home

Field Summary
static java.lang.String[] dbugBitName
          Debug bits name from the parsed Open2Dprot.dbug file
static int[] dbugBitValues
          Debug bits values from the parsed Open2Dprot.dbug file
static int nSwitches
          the number of Command line switches
static java.lang.String[] sw
          Command line + Open2Dprot.properties state switches
 
Constructor Summary
Switches()
          Switches() - Constructor
 
Method Summary
static java.lang.String[] addSwitch(java.lang.String[] swList, java.lang.String newSW)
          addSwitch() - add switch to the sw[]list, by extending the list and then appending the new switch.
static java.lang.String[] changeArgVswitchData(java.lang.String[] argsV, java.lang.String switchName, java.lang.String newData, boolean switchOnFlag)
          changeArgVswitchData() change the switch data to the new switch.
static java.lang.String cvtCSDformatCodeToStr(int fmtCode)
          cvtCSDformatCodeToStr() - convert CSD.CSD_DBTYPE_xxxx to letter code
static int cvtCSDformatCodeToStr(java.lang.String fmtCodeStr)
          cvtCSDformatCodeToStr() - convert letter code to CSD.CSD_DBTYPE_xxxx value
static java.lang.String cvtFormatCodeToStr(int fmtCode)
          cvtFormatCodeToStr() -convert IO_FORMAT_xxxx to letter code
static int cvtFormatCodeToStr(java.lang.String fmtCodeStr)
          cvtFormatStrToCode() - convert letter code to IO_FORMAT_xxxx value
static java.lang.String cvtSwitchListToStr(java.lang.String[] swList)
          cvtSwitchListToStr() - convert switch list to a string <= 60 cols/line.
static java.lang.String getdBugBitsHelpStr()
          getdBugBitsHelpStr() - make a help string from the parsed debug bits
static java.lang.String[] getDefaultSwitches(java.lang.String[] argv, java.lang.String propFile)
          getDefaultSwitches() - get the default swtich list from the properties file if any.
static java.lang.String getSwitchOptionsTableStr(java.lang.String msg, java.lang.String[] swOptData, boolean[] swOptCBvalue, java.lang.String[] swOptValue)
          getSwitchOptionsTableStr() - get list ALL switch options listing on/off status, name and value if any
static java.lang.String getUsageStr(java.lang.String applicationName, java.lang.String[] swOptData)
          getUsageStr() - get usage message string of command line switches
static void init(int argc)
          init() - reset the switches database
static java.lang.String[] mergeSwitches(java.lang.String[] origSW, java.lang.String[] newSW, java.lang.String omitSwitch)
          mergeSwitches() - merge switches where new switches overide old switches.
static boolean parsedBugBitsFile(java.lang.String dbugBitsFile)
          parsedBugBitsFile() - parse the debug bits values from the parsed Open2Dprot.dbug file.
static boolean parsedBugBitsHelpStr(java.lang.String dbugBitsHelpStr)
          parsedBugBitsHelpStr() - parse the debug bits values from the parsed debug bits help string.
static java.lang.String[] replaceFileInArgList(java.lang.String[] argList, java.lang.String newFile)
          replaceFileInArgList() - replace the file in the arg list.
static java.lang.String[] replaceSwitchInArgList(java.lang.String[] argList, java.lang.String newSwitch)
          replaceSwitchInArgList() - replace the switch in the arg list.
static boolean rmvSwitch(java.lang.String newSW)
          rmvSwitch() - remove switch from the sw[]list by shrinking the list
static boolean savePropertiesFile(java.lang.String propertyFile, java.lang.String[] swOptData, java.lang.String[] swOptValue, boolean[] swOptCBvalue)
          savePropertiesFile() - save the switches in propertyFile - typically "{application}.properties".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sw

public static java.lang.String[] sw
Command line + Open2Dprot.properties state switches


nSwitches

public static int nSwitches
the number of Command line switches


dbugBitValues

public static int[] dbugBitValues
Debug bits values from the parsed Open2Dprot.dbug file


dbugBitName

public static java.lang.String[] dbugBitName
Debug bits name from the parsed Open2Dprot.dbug file

Constructor Detail

Switches

public Switches()
Switches() - Constructor

Method Detail

init

public static void init(int argc)
init() - reset the switches database

Parameters:
argc - is the number of switches to be parsed.

getDefaultSwitches

public static java.lang.String[] getDefaultSwitches(java.lang.String[] argv,
                                                    java.lang.String propFile)
getDefaultSwitches() - get the default swtich list from the properties file if any.

Parameters:
argv - is the command line arg list that we scan for "-propertiesFile:newPropFile"
propertiesFile - is file to lookup for default switches
Returns:
list of switches. If none, return null.

savePropertiesFile

public static boolean savePropertiesFile(java.lang.String propertyFile,
                                         java.lang.String[] swOptData,
                                         java.lang.String[] swOptValue,
                                         boolean[] swOptCBvalue)
savePropertiesFile() - save the switches in propertyFile - typically "{application}.properties". Note: resync switch arg lists by calling the application ini.initSwitchOptionsData() before calling this method. Write out the positive (i.e., not '-no' switches).

Parameters:
propertiesFile - - file name to write
swOptData - is list of names of switches
swOptValue - is list of current values
swOptCBvalue - is list of checkbox switch values (for -no prefix)

addSwitch

public static java.lang.String[] addSwitch(java.lang.String[] swList,
                                           java.lang.String newSW)
addSwitch() - add switch to the sw[]list, by extending the list and then appending the new switch.

Parameters:
newSW - - switch to add
Returns:
new switch list if succeed, else swList if fail.

rmvSwitch

public static boolean rmvSwitch(java.lang.String newSW)
rmvSwitch() - remove switch from the sw[]list by shrinking the list

Parameters:
newSW - - switch to remove
Returns:
true if succeed

changeArgVswitchData

public static java.lang.String[] changeArgVswitchData(java.lang.String[] argsV,
                                                      java.lang.String switchName,
                                                      java.lang.String newData,
                                                      boolean switchOnFlag)
changeArgVswitchData() change the switch data to the new switch. If the switch is not found, then grow argsV[] by 1 and add it at the end. [DEPRICATE]

Parameters:
switchName - name of switch e.g. "-thrSP"
newData - (if not null) for the switch parameters "-thrSP:1,100"
switchOnFlag - status of the switch. E.g., "-secondaryPairing" or "-nosecondaryPairing"
Returns:
true if made the change

replaceFileInArgList

public static java.lang.String[] replaceFileInArgList(java.lang.String[] argList,
                                                      java.lang.String newFile)
replaceFileInArgList() - replace the file in the arg list. We find the file since it is the only entry with no "-" switch prefix.

Parameters:
argList - is the array to scan
newFile - is file to replace the old file name
Returns:
arg list. If there was no file in the input arg list then we grow the size of argList by 1 and add newFile to it.

replaceSwitchInArgList

public static java.lang.String[] replaceSwitchInArgList(java.lang.String[] argList,
                                                        java.lang.String newSwitch)
replaceSwitchInArgList() - replace the switch in the arg list. We find the switch '-abc' or '-noabc' if it is in the list by matching for either prefix up to the ':' if any.

Parameters:
argList - is the array to scan
newSwitch - is switch to replace the
Returns:
arg list. If there was no switch in the input arg list then we grow the size of argList by 1 and add the newSwitch to it.

mergeSwitches

public static java.lang.String[] mergeSwitches(java.lang.String[] origSW,
                                               java.lang.String[] newSW,
                                               java.lang.String omitSwitch)
mergeSwitches() - merge switches where new switches overide old switches. It can also remove a switch from the merged list. Either origSW[] or newSW[] must be non-null. Switches are first mapped to normalized form (i.e., upper case and the -NO' mapped to '-', and ':parameters' removed) before they are compared.
 1. Create mergeSW[] that is a copy of newSW[] omitting omitSwitch
    if found.
 2. For each sw in origSW[] and sw is NOT in mergeSW[],
    then add sw to mergeSW[] omitting omitSwitch if found.
 3. Make the size of mergeSW[] the exact size.

Parameters:
oldSW - - list of old switches
newSW - - list of new switches
omitSwitch - is an optional switch to omit if found, ignore it null (test if -noXXX or -XXX).
Returns:
merged switch list

parsedBugBitsFile

public static boolean parsedBugBitsFile(java.lang.String dbugBitsFile)
parsedBugBitsFile() - parse the debug bits values from the parsed Open2Dprot.dbug file. It then creates dbugBitValues[] and dbugBitName[].

Parameters:
dbugBitsFile - is debug bits file to parse.
Returns:
true if succeed.

parsedBugBitsHelpStr

public static boolean parsedBugBitsHelpStr(java.lang.String dbugBitsHelpStr)
parsedBugBitsHelpStr() - parse the debug bits values from the parsed debug bits help string. It then creates dbugBitValues[] and dbugBitName[].

Parameters:
dbugBitsHelpStr - is debug bits help string to parse.
Returns:
true if succeed.

getdBugBitsHelpStr

public static java.lang.String getdBugBitsHelpStr()
getdBugBitsHelpStr() - make a help string from the parsed debug bits

Parameters:
dbugBitName - is list of debug bit names
dbugBitValues - is list of debug bit values
Returns:
"" if no data available.

getUsageStr

public static java.lang.String getUsageStr(java.lang.String applicationName,
                                           java.lang.String[] swOptData)
getUsageStr() - get usage message string of command line switches

Parameters:
applicationName - is the applciation program name
swOptData[0:(2*nData)-1] - is used by the PopupOptions. It contains pairs of (prompt, helpStr) data. If the prompt data has a "-", then make a CheckBox (switchOptCBvalue), else it is a Label with the prompt data. If the user data is not null, then make a TextField with this user (switchOptValue) data on the right else make an empty Label.
Returns:
command line help message

getSwitchOptionsTableStr

public static java.lang.String getSwitchOptionsTableStr(java.lang.String msg,
                                                        java.lang.String[] swOptData,
                                                        boolean[] swOptCBvalue,
                                                        java.lang.String[] swOptValue)
getSwitchOptionsTableStr() - get list ALL switch options listing on/off status, name and value if any

Parameters:
msg - optional message to put in front of string if not null.
swOptData[0:(3*nData)-1] - is used by the PopupOptions. It contains pairs of (prompt, helpStr) data. If the prompt data has a "-", then make a CheckBox (switchOptCBvalue), else it is a Label with the prompt data. If the user data is not null, then make a TextField with this user (switchOptValue) data on the right else make an empty Label.
swOptcCBvalue[0:nData-1] - is used by the PopupOptions. It contains boolean values of switch data use for Checkboxes.
swOptValue[0:nData-1] - is used by the PopupOptions. It contains String values of associated switch data.
Returns:
string value of table

cvtSwitchListToStr

public static java.lang.String cvtSwitchListToStr(java.lang.String[] swList)
cvtSwitchListToStr() - convert switch list to a string <= 60 cols/line. There is no final \n.

Parameters:
swList - - list to convert
Returns:
string

cvtFormatCodeToStr

public static java.lang.String cvtFormatCodeToStr(int fmtCode)
cvtFormatCodeToStr() -convert IO_FORMAT_xxxx to letter code

Parameters:
fmtCode - to convert IO_FORMAT_xxxx
Returns:
string code "X", "T", "F", or "G"

cvtFormatCodeToStr

public static int cvtFormatCodeToStr(java.lang.String fmtCodeStr)
cvtFormatStrToCode() - convert letter code to IO_FORMAT_xxxx value

Parameters:
fmtCodeStr - string code "X", "T", "F", or "G"
Returns:
to convert FileIO.IO_FORMAT_xxxx, else -1 if error

cvtCSDformatCodeToStr

public static java.lang.String cvtCSDformatCodeToStr(int fmtCode)
cvtCSDformatCodeToStr() - convert CSD.CSD_DBTYPE_xxxx to letter code

Parameters:
fmtCode - to convert CSD.CSD_DBTYPE_xxxx
Returns:
string code "C", "R", or "X"

cvtCSDformatCodeToStr

public static int cvtCSDformatCodeToStr(java.lang.String fmtCodeStr)
cvtCSDformatCodeToStr() - convert letter code to CSD.CSD_DBTYPE_xxxx value

Parameters:
fmtCodeStr - string code "C", "R", or "X". "T" is special case.
Returns:
to convert CSD.CSD_DBTYPE_CACHE, else 0 if error