/** File: Switches.java */ /** * File Switches class used to parse the switches from the command line * args passed as argsV[]. The parsed switches are stored in the Globals * variables through the main class HTMLtools cvt instance. * Until we [REFACTOR] the JTV (Java TreeView) converter to * a separate application, the variables reside here. * *
* List of Methods * parseSwitches() - parse the command line args and set Global variables. * getCleanArgList() - Remove # prefaced and "" lines and resize argsV. * parseGBSswitches() - parse curSW switch to see if it is a GBS option. * parseTIswitches() - parse curSW switch to see if it is a TI option * parseJTVswitches() - parse JTV command line args and set Global variables. * createHeaderNameMapTable() - create header name map table and lists, flags. **
* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.net/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.
** It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.net/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *
* $Date: 2009/11/14 11:45:56 $ $Revision: 1.36 $
*
* Copyright 2008, 2009 by Peter Lemkin
* E-Mail: lemkin@users.sourceforge.net
* http://lemkingroup.com/
*
*/
public class Switches
{
/* Note all global variables are in Globals.java except the help
* messages which are in HelpMsgs.java.
*/
public HTMLtools
cvt;
/** Global fileTable instance */
public FileTable
fio;
/**
* Switches() - Constructor.
* Note: should only be called after set up fio in cvt.
*/
public Switches(HTMLtools cvt)
{ /* Switches */
this.cvt= cvt;
this.fio= cvt.fio;
} /* Switches */
/**
* parseSwitches() - parse the arg list and set global switches and
* variables. Note some of the switches may have multiple instances
* including: -files, -hrefData, -hrefHeaderRow -dropColumn, -keepColumn,
* -reorderColumn, -sortTableByColumn, -mapQuestionmarks.
* You only need to enter the minimum part of the switch described
* in the {@link HelpMsgs#COMMAND_MSG} documentation.
* See documentation in HelpMsgs.java for details.
* Note: the switches are case sensitive.
* @param argsV is list of command line switches
* @return true if succeed, false if fail or just need to go on to
* the next param set to process
*
* @see FileTable#readFileAsString
* @see UtilCM#cvs2Array
* @see UtilCM#cvs2i
* @see UtilCM#cvs2f
* @see UtilCM#setLogMsgs
* @see UtilCM#getLogMsgs
* @see UtilCM#logMsgs
* @see FileTable#readFileAsTable
* @see FileTable#setHasTableHeaderFlag
* @see FileTable#setRmvTrailingBlankLinesFlag
* @see FileTable#setRmvTrailingEmptyColumnsFlag
*/
public boolean parseSwitches(String argsV[])
{ /* parseSwitches */
String curSW= null;
int i= 0;
boolean flag= true;
/* clear DEBUG (name,value) list */
UtilCM.clearDebugNameValuePairs();
for(i= 0; i < argsV.length; i++)
{ /* special switch pre-processing */
curSW= argsV[i];
if(curSW.startsWith("#") || curSW.equals(""))
continue; /* ignore comments and blank lines */
else if(curSW.startsWith("-addE"))
{ /* "-addEpilogueFile:{opt. file}" */
cvt.epilogueFile= "epilogue.html";
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
cvt.epilogueFile= curSW.substring(idxColon + 1);
}
}
else if(curSW.startsWith("-addO"))
{ /* "-addOutfilePostfix:{postfix name}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
cvt.addOutfilePostfixName= curSW.substring(idxColon + 1);
}
if(cvt.addOutfilePostfixName==null)
cvt.addOutfilePostfixName= ""; /* must not be null */
}
else if(curSW.startsWith("-addP"))
{ /* "-addPrologFile:{opt file}" */
cvt.prologFile= "prolog.html";
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
cvt.prologFile= curSW.substring(idxColon + 1);
}
}
else if(curSW.startsWith("-addRow"))
{ /* "-addRowNumbers" */
cvt.addRowNumbersFlag= true;
}
else if(curSW.startsWith("-addT"))
{ /* "-addTableName:{table name to use}" */
cvt.addTableNameFlag= true;
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
cvt.addTableName= curSW.substring(idxColon + 1);
}
else
{
UtilCM.logMsg("Bad -addTableName:{table name to use}" +
" switch usage.\n");
flag= false;
break;
}
}
else if(curSW.startsWith("-allowH"))
{ /* "-allowHdrDups" */
cvt.ignoreDuplicateFieldsFlag= true;
}
else if(curSW.startsWith("-alt"))
{ /* "-alternateRowBackgroundColor:{background color}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String cName= curSW.substring(idxColon + 1);
cvt.altRowBackgroundColor= UtilCM.cvtHTMLcolorNameToHex(cName);
}
else
{
UtilCM.logMsg("Bad -alternateRowBackgroundColor:" +
"{background color}" + " switch usage.\n");
flag= false;
break;
}
}
else if(curSW.startsWith("-batchP"))
{ /* "-batchProcess:{file of param specs, opt. new working dir}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg and setup the batch process list */
String sTmp= curSW.substring(idxColon + 1);
String sTmpA[]= UtilCM.cvs2Array(sTmp,",");
cvt.batchProcessFile= sTmpA[0];
if(sTmpA.length>1)
cvt.batchCwd= sTmpA[1];
else
cvt.batchCwd= null;
cvt.batchProcessStr= fio.readFileAsString(cvt.batchProcessFile);
if(cvt.batchProcessStr==null)
{ /* Fatal error - save log and exit */
UtilCM.logMsg("Missing file '"+cvt.batchProcessFile+
"'in -batchProcess:{file of param spec.}" +
" switch.\n");
cvt.closeProgram(-1); /* Close this down big time!!! */
}
String bpStr2= UtilCM.mapCRLF2space(cvt.batchProcessStr);
cvt.batchParamsSpecs= UtilCM.cvs2Array(bpStr2," ");
cvt.maxBatchParamsSpecs= cvt.batchParamsSpecs.length;
cvt.curBatchParamsSpecs= 0;
cvt.batchProcessFlag= true;
return(true); /* ignore the other switches and return now */
} /* get the arg and setup the batch process list */
else
{
UtilCM.logMsg("Bad -batchProcess:{file of param spec.}" +
" switch usage.\n");
cvt.batchProcessFile= null;
cvt.batchCwd= null;
flag= false;
break;
}
} /* "-batchProcess:{file of param specs, opt. new working dir}" */
else if(curSW.startsWith("-concat"))
{ /* "-concatTables:{outputConcatenateFile.txt,opt. noHTML}" */
cvt.concatInputFilesWithSameFieldsFlag= true;
cvt.ftConcat= null;
String
baseCatFile= null,
catFile= null,
sMOD= null;
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String
sTmp= curSW.substring(idxColon + 1),
sData[]= UtilCM.cvs2Array(sTmp,",");
catFile= sData[0];
if(sData.length>1)
sMOD= sData[1];
if(catFile.endsWith(".txt") || catFile.endsWith(".map") ||
catFile.endsWith(".htm"))
baseCatFile= catFile.substring(0, catFile.length()-4);
if(catFile.endsWith(".html"))
baseCatFile= catFile.substring(0, catFile.length()-5);
}
else
{
UtilCM.logMsg("Bad -concatTables:{outputConcatenateFile.txt,opt. noHTML}" +
" switch usage.\n");
cvt.batchProcessFile= null;
cvt.batchCwd= null;
flag= false;
break;
}
cvt.concatOutputTxtFile= catFile; /* use what they tell us to use */
if(sMOD!=null && sMOD.equalsIgnoreCase("noTXT"))
{
cvt.makeTXTflag= false; /* default is true */
}
if(sMOD==null || !sMOD.equalsIgnoreCase("noHTML"))
cvt.concatOutputHtmlFile= baseCatFile + ".html";
else
{
cvt.concatOutputHtmlFile= null;
cvt.makeHTMLflag= false; /* default is true */
}
} /* "-concatTables:{outputConcatenateFile.txt,opt. noHTML}" */
else if(curSW.startsWith("-copyF"))
{ /* "-copyFile:{srcFile,dstFile}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String sTmp= curSW.substring(idxColon + 1);
/* parse the strings */
int idxComma= sTmp.indexOf(",");
if(idxComma!=-1)
{ /* push it onto the copy tree lists */
if(cvt.nCopyFile==0)
{ /* allocate storage first time need it */
cvt.copySrcFile= new String[cvt.MAX_COPY_FILES_AND_TREES];
cvt.copyDstFile= new String[cvt.MAX_COPY_FILES_AND_TREES];
}
/* Save instance of "-copyFile:{srcFile,dstFile}" */
cvt.copyFileFlag= true;
cvt.copySrcFile[cvt.nCopyFile]= sTmp.substring(0, idxComma);
cvt.copyDstFile[cvt.nCopyFile++]= sTmp.substring(idxComma + 1);
}
} /* get the arg */
else
{
cvt.copyTreeDataFlag= false;
UtilCM.logMsg("Bad -copyFile:{srcTreeFiles,destPath}" +
" switch usage - ignoring switch.\n");
flag= false;
break;
}
} /* "-copyFile:{srcFile,dstFile}" */
else if(curSW.startsWith("-copyT"))
{ /* "-copyTree:{srcTreeFiles,destPath}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String sTmp= curSW.substring(idxColon + 1);
/* parse the strings */
int idxComma= sTmp.indexOf(",");
if(idxComma!=-1)
{ /* push it onto the copy tree lists */
if(cvt.nCopyTreeDir==0)
{ /* allocate storage first time need it */
cvt.copyTreeSrcDir= new String[cvt.MAX_COPY_FILES_AND_TREES];
cvt.copyTreeDstDir= new String[cvt.MAX_COPY_FILES_AND_TREES];
}
/* Save instance of "-copyTree:{srcTreeFiles,destPath}" */
cvt.copyTreeDataFlag= true;
cvt.copyTreeSrcDir[cvt.nCopyTreeDir]= sTmp.substring(0, idxComma);
cvt.copyTreeDstDir[cvt.nCopyTreeDir++]= sTmp.substring(idxComma + 1);
}
} /* get the arg */
else
{
cvt.copyTreeDataFlag= false;
UtilCM.logMsg("Bad -copyTree:{srcTreeFiles,destPath}" +
" switch usage - ignoring switch.\n");
flag= false;
break;
}
} /* "-copyTree:{srcTreeFiles,destPath}" */
else if(curSW.startsWith("-dataP"))
{ /*'-dataPrecisionHTMLtable:{nbr digits precision}' */
cvt.dataPrecisionHTMLtable= -1;
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg(s) */
String sTmp= curSW.substring(idxColon + 1);
cvt.dataPrecisionHTMLtable= UtilCM.cvs2i(sTmp,-1);
}
}
else if(curSW.startsWith("-debug"))
{ /* "-debug:{debugName,debugValue}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg(s) */
String sTmp= curSW.substring(idxColon + 1);
String sTmpA[]= UtilCM.cvs2Array(sTmp,",");
if(sTmpA.length!=2)
{
UtilCM.logMsg("Bad -debug:{debugName,debugValue}" +
" switch usage - ignoring switch '"+curSW+"'\n");
}
else
{ /* save switch */
String
debugName= sTmpA[0],
debugValue= sTmpA[1];
if(!UtilCM.setDebugNameValue(debugName,debugValue))
{
UtilCM.logMsg("Problem, too many -debug:{debugName,debugValue}" +
" switchs usage - ignoring switch '"+curSW+"'\n");
}
} /* save switch */
} /* get the arg(s) */
}/* "-debug:{debugName,debugValue" */
else if(curSW.startsWith("-dirI"))
{ /* "-dirIndexHtml:{dir,'O'verride}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String sTmp= curSW.substring(idxColon + 1);
/* parse the strings */
int idxComma= sTmp.indexOf(",");
if(idxComma!=-1)
{ /* push it onto the copy tree lists */
if(cvt.nDirIndexHtml==0)
{ /* allocate storage first time need it */
cvt.dirIndexHtmlFlag= true;
cvt.dirIndexHtml= new String[cvt.MAX_COPY_FILES_AND_TREES];
cvt.dirIndexHtmlOverrideFlag= new boolean[cvt.MAX_COPY_FILES_AND_TREES];
}
/* Save instance of "-dirIndexHtml:{dir,'O'verride}" */
cvt.dirIndexHtml[cvt.nDirIndexHtml]= sTmp.substring(0, idxComma);
String sOveride= sTmp.substring(idxComma + 1,idxComma+2);
boolean overrideFlag= (sOveride.equalsIgnoreCase("O"));
cvt.dirIndexHtmlOverrideFlag[cvt.nDirIndexHtml++]= overrideFlag;
}
} /* get the arg */
else
{
cvt.dirIndexHtmlFlag= false;
UtilCM.logMsg("Bad -dirIndexHtml:{dir,'O'verride}" +
" switch usage - ignoring switch '"+curSW+"'\n");
flag= false;
break;
}
} /* "-dirIndexHtml:{dir,'O'verride}" */
else if(curSW.startsWith("-dropC"))
{ /* "-dropColumnName:{column header name}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
if(cvt.dropColNames==null)
cvt.dropColNames= new String[cvt.MAX_COL_EDITS];
String sDrop= curSW.substring(idxColon + 1);
cvt.dropColNames[cvt.nDropColNames++]= sDrop;
}
else
{
UtilCM.logMsg("Bad -dropColumnName:{column header name}" +
" switch usage.\n");
flag= false;
break;
}
}
else if(curSW.startsWith("-exportB"))
{ /* "-exportBigCellsToHTMLfile:{opt. size for big}" */
cvt.exportBigCellsToHTMLfileFlag= true;
cvt.bigExportCellThr= cvt.EXPORT_BIG_CELL_SIZE;
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
String sTmp= curSW.substring(idxColon + 1);
cvt.bigExportCellThr= UtilCM.cvs2i(sTmp,
cvt.EXPORT_BIG_CELL_SIZE);
}
} /* "-exportBigCellsToHTMLfile:{opt. size for big}" */
else if(curSW.startsWith("-extractR"))
{ /* "-extractRow:{colName,rowNbr,resourceTblFile,htmlStyle}" */
/* cvt.extractColName, cvt.extractRowNbr,
* cvt.extractResTblFile, cvt.extractHtmlStyle
*/
int
idxColon= curSW.indexOf(":"),
idxComma= -1,
idxComma2= -1,
idxComma3= -1,
rowNbr= -1;
String
sColName= null,
sRowNbr= null,
sResTblFile= null,
sHtmlStyle= null,
sTmp2= null,
sTmp3= null;
if(idxColon != -1)
{ /* get the args */
String sTmp= curSW.substring(idxColon + 1);
/* parse the colHdrName and urlBaseLink strings */
idxComma= sTmp.indexOf(",");
sColName= sTmp.substring(0, idxComma);
sTmp2= sTmp.substring(idxComma + 1);
idxComma2= sTmp2.indexOf(",");
if(idxComma2 != -1)
{ /* get the 2nd, 3rd, & 4th args */
sRowNbr= sTmp2.substring(0, idxComma2);
sTmp3= sTmp2.substring(idxComma2 + 1);
idxComma3= sTmp3.indexOf(",");
if(idxComma3 != -1)
{ /* get the 3rd, & 4th args */
sResTblFile= sTmp3.substring(0, idxComma3);
sHtmlStyle= sTmp3.substring(idxComma3 + 1);
sHtmlStyle= sHtmlStyle.toUpperCase();
}
}
} /* get the args */
rowNbr= UtilCM.cvs2i(sRowNbr,-1); /* make sure good # */
if(idxColon==-1 || idxComma==-1 || idxComma2==-1 ||
idxComma3==-1)
{
UtilCM.logMsg("Bad -extractRow:{colName,rowNbr," +
"resourceTblFile,htmlStyle}} switch usage.\n" +
"Missing arguments.\n");
flag= false;
break;
}
else if(rowNbr<=-1)
{
UtilCM.logMsg("Bad -extractRow:{colName,rowNbr," +
"resourceTblFile,htmlStyle}} switch usage.\n" +
"Missing arguments.\n");
flag= false;
break;
}
else if(!sHtmlStyle.equals("DL") && !sHtmlStyle.equals("OL") &&
!sHtmlStyle.equals("UL") &&!sHtmlStyle.equals("TABLE"))
{
UtilCM.logMsg("Bad -extractRow:{colName,rowNbr," +
"resourceTblFile,htmlStyle}} switch usage.\n" +
"HtmlStyle must be DT, OL, UL or TABLE.\n");
flag= false;
break;
}
/* OK save it - process it later after get the keyword
* from the table we will be processing. Lookup the resource
* table at that time.
*/
cvt.extractRowFromResTblAsHTMLflag= true;
cvt.extractColName= sColName;
cvt.extractRowNbr= rowNbr;
cvt.extractResTblFile= sResTblFile;
cvt.extractHtmlStyle= sHtmlStyle;
} /* "-extractRow:{colName,rowNbr,resourceTblFile,htmlStyle}" */
else if(curSW.startsWith("-fastE"))
{ /* '-fastEditFile:{opt. output file}' */
cvt.fastEditFileFlag= true;
cvt.saveEditHTMLflag= false;
cvt.makeHTMLflag= false;
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the arg */
cvt.saveEditedTable2File= curSW.substring(idxColon + 1);
}
}
else if(curSW.startsWith("-files"))
{ /* "-files:{f1,f2,...,fn}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the args */
String sFileList= curSW.substring(idxColon + 1);
if(cvt.specificInputFileList == null)
cvt.specificInputFileList= sFileList;
else cvt.specificInputFileList+= "," + sFileList;
}
else
{
UtilCM.logMsg("Bad -files:{f1,f2,...,fn}" +
" switch usage.\n");
flag= false;
break;
}
} /* "-files:{f1,f2,...,fn}" */
else if(curSW.startsWith("-flipA"))
{ /* "-flipAclass:{flipAclass}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the args */
cvt.flipAclass= curSW.substring(idxColon + 1);
if(cvt.flipBclass!=null)
cvt.reportFoldChangeFlag= true;
} /* get the args */
else
{
UtilCM.logMsg("Bad -flipAclass:{flipAclass}"+
" switch usage.\n");
flag= false;
break;
}
} /* "-flipAclass:{flipAclass}" */
else if(curSW.startsWith("-flipB"))
{ /* "-flipBclass:{flipBclass}" */
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the args */
cvt.flipBclass= curSW.substring(idxColon + 1);
if(cvt.flipAclass!=null)
cvt.reportFoldChangeFlag= true;
} /* get the args */
else
{
UtilCM.logMsg("Bad -flipBclass:{flipBclass}"+
" switch usage.\n");
flag= false;
break;
}
} /* "-flipBclass:{flipBclass}" */
else if(curSW.startsWith("-flipC"))
{ /* "-flipColumnName:{flipColumnFile,flipColumnNames}" or
* "-flipColumnName:{*LIST*,flipColumnNames,v1,v2,...,vn}"
*/
int idxColon= curSW.indexOf(":");
if(idxColon != -1)
{ /* get the args */
String
sTmp= curSW.substring(idxColon + 1),
tmpArgs[]= UtilCM.cvs2Array(sTmp,",");
int
nArgs= tmpArgs.length;
if(nArgs<2)
{
UtilCM.logMsg("Bad -flipColumnName:{flipColumnFile,flipColumnNames}" +
" switch usage.\n");
flag= false;
break;
}
String
flipColFile= tmpArgs[0],
flipColName= tmpArgs[1],
flipColValues[]= null;
if(flipColFile.equals("*LIST*"))
{ /* build value list */
flipColValues= new String[nArgs-2];
for(int k=0;k