import java.io.File; /** File: Convert.java */ /** * Class Convert to convert one Table to HTML. This class also * contains various mapping functions used in the conversion as * well as extracting a subtable for inclusion. *

*

 * List of Methods
 * Convert() - Constructor
 * cvtTable2HTML() - Process a FileTable into an output HTML file.
 * cvtPrefaceTable2HTML() - Process a preface FileTable to HTML file. 
 * cvtText2HTML() - convert text to an output HTML file.
 * mapCellToStr() - map a cell string at [r, c] to an HTML string
 * buildMapUrl() - map a cell string at [r, c] to an HTML link string
 * makeOptionsListFromSemicolonList() - map ";;" delim. str to OPTION list
 * testIfRowIsBlank() - test if remove trailing blank lines after first line 
 * concatInputFilesWithSameFields() - create concat. tab-delim. .txt file
 * shrinkStrToSmallerFont() - shrink string if text is > threshold size.
 * exportBigStrToHTMLfile() - if the |str| > bigExportCellThr.
 * mapHeaderNames() - try to map header names within the header table
 * mapQuestionmarks() - Map cell data of the form '??{cell text}'??'
 * mapDollarsigns() - map data form '$$keyword$$' to '{toString}.
 * extractRowFromResourceTableAsHTML() - extract row data from resource file
 * checkAndMakeIndexHtmlDirList() - make index.html files in list of dirs
 * checkAndMakeIndexHtmlDir() - make index.html files in a directory.
 * makeIndexHtml() - make "index.html" file in the directory.
 * checkIfDir() - test if fileName is a directory
 * 
*

* 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/21 11:45:56 $ $Revision: 1.37 $ *
* Copyright 2008, 2009 by Peter Lemkin * E-Mail: lemkin@users.sourceforge.net * http://lemkingroup.com/ *
*/ public class Convert { /* 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; /* Style HTML generation constant for DL type HTML generation * used with -extractR:{colName,rowNbr,resourceTblFile,htmlStyle} * switch. */ final private static int STYLE_DL= 1; /* Style HTML generation constant for OL type HTML generation * used with -extractR:{colName,rowNbr,resourceTblFile,htmlStyle} * switch. */ final private static int STYLE_OL= 2; /* Style HTML generation constant for UL type HTML generation * used with -extractR:{colName,rowNbr,resourceTblFile,htmlStyle} * switch. */ final private static int STYLE_UL= 3; /* Style HTML generation constant for TABLE type HTML generation * used with -extractR:{colName,rowNbr,resourceTblFile,htmlStyle} * switch. */ final private static int STYLE_TABLE= 4; /** * Convert() - Constructor * @param cvt is instance of HTMLtools */ public Convert(HTMLtools cvt) { /* Convert */ this.cvt= cvt; this.fio= cvt.fio; } /* Convert */ /** * cvtTable2HTML() - Process a FileTable into an output HTML file. * @param ft is FileTable containing the data to convert * @param iFileName full path of input file name * @param fileNbr number of the input file (special processing for 1st) * @param sName name for the prolog HTML header if not null * @param sProlog prolog string if not null * @param sEpilogue epilogue string it not null * @param oPrefaceFile name of preface file to generate if not null. * @param limitedRows if limited length of rows in table. * @return true if succeed, then return the output HTML * web page string, else null. * @see #mapCellToStr * @see #testIfRowIsBlank */ public String cvtTable2HTML(FileTable ft, String iFileName, int fileNbr, String sName, String sProlog, String sEpilogue, String oPrefaceFile, String limitedRows) { /* cvtTable2HTML */ String s= ""; /* [1] Copy data locally for faster processing */ int nColUrlMap= cvt.nColUrlHmapData, colHdrIdx[]= cvt.colHdrIdxHmapData, nbrTableHdrLines= cvt.nbrTableHdrLines, tRows= ft.tRows, tCols= ft.tCols, r, c, m; String colHdrNameHmap[]= cvt.colHdrNameHmapData, cell, sRow, /* work with smaller string */ tRowData[], /* faster unpacking */ tFields[]= ft.tFields, tHeader[][]= ft.tHeader, tData[][]= ft.tData; /* [rows][cols] */ if(tRows<=0 || tData==null) { UtilCM.logMsg("Table '"+iFileName+"' has no data.\n"+ " tRows="+tRows+" tCols="+tCols+ " |tData|="+tData.length+"\n"); return(null); } /* [2] Set the HREF mapping indexes from tField names * to not-found (-1) for now. Then setup the colHdrIdx map * by the column field names. */ if(tCols>0) { /* init the tField col to Hdr map */ for(int mp=0;mp0) { /* Add the limited number of rows */ String sLimitedRows= limitedRows+"

\n"; sProlog= UtilCM.replaceSubstrInString(sProlog,"$$LIMITED-ROWS$$", sLimitedRows); } else sProlog= UtilCM.replaceSubstrInString(sProlog,"$$LIMITED-ROWS$$", ""); /* remove it from prolog */ if(sProlog!=null && sProlog.length()!=0) s += sProlog; /* [3.1] Add colormap data scale quantile TABLE HTML if it exists. * This maps the cvt.heatMapColors[] quantiles to positive numeric value * in range of [cvt.glbMinRowVal, cvt.glbMaxRowVal]. Computed by * convert.makeColorMapScaleHTML() if * cvt.showDataHeatmapFlag and cvt.hasStatIndexMapFileFlag are set. */ if(cvt.colorMapScaleHTML!=null) s += cvt.colorMapScaleHTML; /* [4] If '-addTableName:{table name to use}' , then use * that else if sName is not null, use that, else nothing. */ if(cvt.addTableNameFlag && cvt.addTableName!=null && cvt.addTableName.length()!=0) s += "

"+cvt.addTableName+"

\n"; else if(sName!=null && sName.length()!=0) s += "

"+sName+"

\n"; /* [4.1] Add the Link to the Preface file if any */ if(oPrefaceFile!=null && oPrefaceFile.length()!=0) s += "

Preface to table

\n"; /* [5] Add
*/ if(!cvt.noBorderFlag) s += "
\n"; else s += "
\n"; /* [5.1] Add the
header row. * Flag set if the input table has a header, * default is header. use \n"; r= 0; if(cvt.addRowNumbersFlag) { sRow += mapCellToStr("Row", 0,0, "TH", -1, sName); } for(c=0;c0) hRefIdx= colHdrIdx[h]; else hRefIdx= -1; cell= hData[c]; sRow += mapCellToStr(cell, 0,c, "TH", hRefIdx, sName); } /* process the column */ sRow += "\n"; s += sRow; } /* make a
instead of . */ if(cvt.hasTableHeaderRowFlag) { /* process the header rows */ for(int h=0;h line for all header lines */ /* Note: If doing a "-hrefHeaderRow" mapping, then * try to map Table Header Row by Column for columns * > 1st (Table tHeader) column. * Lookup the HREF map if any */ int hRefIdx= -1; /* [5.1.1] Now create the row of data */ String hData[]= tHeader[h]; /* get header row data*/ sRow = "
line for all header lines */ } /* process the header rows */ /* [5.2] Add all of the row entries */ for(r=0;r0 && cvt.rmvTrailingBlankRowsAndColumnsFlag) { if(testIfRowIsBlank(tRowData, tCols)) continue; /* ignore the line */ } sRow = "\n"; /* Add row # to output if required */ if(cvt.addRowNumbersFlag) { sRow += mapCellToStr((r+1)+"", r,0, "TH", -1, sName); /* was "TD" */ } /* Add row data where we also do cell URL, font size and color mapping * if required. */ int hRefIdx= -1; for(c=0;c end table */ s += "

\n"; /* [6] Add the epilogue if it exists */ if(sEpilogue!=null && sEpilogue.length()!=0) s += sEpilogue; return(s); } /* cvtTable2HTML */ /** * cvtPrefaceTable2HTML() - Process a preface FileTable into an * output HTML file. * @param ft is FileTable containing the data to convert * @param fileName full path of input file name * @param sName name for the prolog HTML header if not null * @param sProlog prolog string if not null * @param sEpilogue epilogue string it not null * @return true if succeed, then return the output HTML * web page string, else null. * @see #mapCellToStr */ public String cvtPrefaceTable2HTML(FileTable ft, String fileName, String sName, String sProlog, String sEpilogue) { /* cvtPrefaceTable2HTML */ String s= ""; /* [1] Copy data locally for faster processing */ int r, c, tRows= ft.tRows, tCols= ft.tCols; String cell, sRow, /* work with smaller string */ tRowData[], /* faster unpacking */ tFields[]= ft.tFields, tData[][]= ft.tData; /* [rows][cols] */ if(tRows<=0 || tData==null) { UtilCM.logMsg("Preface Table '"+fileName+"' has no data.\n"+ " tRows="+tRows+" tCols="+tCols+ " |tData|="+tData.length+"\n"); return(null); } /* [2] Set the HREF mapping indexes from tField names * to not-found (-1) for now. */ if(tCols>0) { /* init the tField col to Hdr map */ for(int mp=0;mp\n"; else if(sName!=null && sName.length()!=0) s += "

Preface "+sName+"

\n"; /* [5] Add
*/ if(!cvt.noBorderFlag) s += "
\n"; else s += "
\n"; /* [5.1] Add the
header row. * Flag set if the input table has a header, * default is header. use \n"; r= 0; for(c=0;c row entries */ for(r=0;r end table */ s += "
instead of . */ if(tFields!=null) { /* process the header rows */ /* [5.1.1] Now create the row of data */ sRow = "

\n"; /* [6] Add the epilogue if it exists */ if(sEpilogue!=null && sEpilogue.length()!=0) s += sEpilogue; return(s); } /* cvtPrefaceTable2HTML */ /** * cvtText2HTML() - convert text to an output HTML file. This is * used for prefacing text from the input file or additional preface * table. * @param sText is the string to convert to HTML * @param sName name for the prolog HTML header if not null * @param sProlog prolog string if not null * @param sEpilogue epilogue string it not null * @return true if succeed, then return HTML string */ public String cvtText2HTML(String sText, String sName, String sProlog, String sEpilogue) { /* cvtText2HTML */ String s= ""; /* [2] Add the prologue if it exists */ if(sProlog!=null && sProlog.length()!=0) s += sProlog; /* [3] Add name header */ if(sName!=null && sName.length()!=0) s += "

"+sName+"

\n"; /* [3] Add the text */ s += "
\n"+ sText + "\n
\n"; /* [4] Add the epilogue if it exists */ if(sEpilogue!=null && sEpilogue.length()!=0) s += sEpilogue; return(s); } /* cvtText2HTML */ /** * mapCellToStr() - map a cell string at [r, c] to an HTML string * adding the HREF if hrefIdx==c using the cvt.urlBaseLink. * It generates "< TH >{sCell}< /TH >" or "< TD >{sCell}< /TD >" * and then {sCell} is inside of * "< FONT fontSize={useHTMLfontSize} > sCell < /FONT >" * and hCell is < A HREF="{urlBaseLink}+cell" > cell < /A >" * @param cell data to convert * @param r is row of the cell * @param c is col of the cell * @param tHD is either "TH" or "TD" depending on whether it's a * header cell. * @param hrefIdx is the hypertext map index if not -1 * @param oFileBaseName is the output File base name * @return converted HTML string or the original string if not converted. * * @see #mapQuestionmarks * @see #makeOptionsListFromSemicolonList * @see #exportBigStrToHTMLfile * @see #shrinkStrToSmallerFont */ public String mapCellToStr(String cell, int r, int c, String tHD, int hrefIdx, String oFileBaseName) { /* mapCellToStr */ String sBGcolor= ((r & 01)==1 && cvt.altRowBackgroundColor!=null) ? " bgcolor=\""+cvt.altRowBackgroundColor+"\"" /* only even rows */ : ""; /* Do additional process of numeric data for
cells. */ if(tHD.equals("TD") && UtilCM.isNumber(cell)) { /* Do additional process of numeric data for cells */ /* Change the prcession if requested */ if(cvt.dataPrecisionHTMLtable>=0) { float fVal= UtilCM.cvs2f(cell); cell= UtilCM.cvf2s(fVal, cvt.dataPrecisionHTMLtable); } /* If doing a heat map by changing the background of cells, then * map the cell value to a background color. */ if(cvt.showDataHeatmapFlipTableFlag && cvt.hasStatIndexMapFileFlag) { /* Map data cell number to cell background color */ sBGcolor= mapDataToCellBGcolor(cell,sBGcolor); } } /* Do additional process of numeric data for cells */ String sR= "<"+tHD+" valign=top align=left"+sBGcolor+">", sCell= cell, hCell= cell; if(cell==null || cell.equals("")) sCell= " "; else { /* process the cell */ if(cvt.mapQuestionmarksFlag) cell= mapQuestionmarks(cell); /* map ?? before other maps */ if(cvt.mapDollarsignsFlag) cell= mapDollarsigns(cell); if(hrefIdx>-1) hCell= buildMapUrl(cell, c, hrefIdx); else if(cvt.makeOptionsListFromSemicolonListFlag) hCell= makeOptionsListFromSemicolonList(cell); /* override other * mappings */ if(hCell.equals(cell)) { /* only try to export big cells if no other mapping */ if(cvt.exportBigCellsToHTMLfileFlag) { /* Try to export big cells */ String oDir= cvt.outputDataDir, oFileName= oFileBaseName+cvt.addOutfilePostfixName; int maxChars= cvt.bigExportCellThr; /* [TODO] size javascript abrev string */ hCell= exportBigStrToHTMLfile(cell,oDir,oFileName,r,c,maxChars); hCell= shrinkStrToSmallerFont(hCell); } /* Try to export big cells */ } /* only try to export big cells if no other mapping */ /* Finish now if any mappings were performed */ if(!hCell.equals(cell)) { /* did mapping, just return it now */ sR += hCell + "\n"; return (sR); } /* did mapping, just return it now */ /* Finally, if the useHTMLfontSize or the |str|>bigCellThr * Then shrink the string by decreasing the font size. */ sCell= shrinkStrToSmallerFont(hCell); } /* process the cell */ sR += sCell+ "\n"; return(sR); } /* mapCellToStr */ /** * buildMapUrl() - map a cell string at [r, c] to an HTML link * string by adding the HREF if hrefIdx==c using the cvt.urlBaseLink * For the 1:1 mapping, it adds the cell value {} to the base * urlBaseLink value {}. * e.g., < A HREF="{urlBaseLink}+{cell}" > {cell} < /A >" *

* If the mapTokenInUrl exists, then substitute the cell for * cvt.mapTokenInUrl[hrefIdx] in the cvt.urlBaseLink[hrefIdx]. *

* [TODO] more complex cases require breaking up the cell value and * inserting the pieces into the different parts of the urlBaseLink. *

* [MAYBE] add special cases here. Would be nice to automate it better. * @param cell data to convert * @param c is col of the cell * @param hrefIdx is the hypertext map index if not -1 * @return converted HTML string or the original string if not converted. * * @see UtilCM#replaceSubstrInString */ public String buildMapUrl(String cell, int c, int hrefIdx) { /* buildMapUrl */ /* Remove enclosing white space which messes up URLs */ cell= cell.trim(); String baseUrl= cvt.urlBaseLinkHmapData[hrefIdx], mapTok= cvt.mapTokenInUrlHmapData[hrefIdx], sUrl= baseUrl+cell, /* default just append cell data to baseUrl */ smallCell= shrinkStrToSmallerFont(cell), hCell; /* [TODO] Test if parses baseUrl correctly */ if(mapTok!=null && mapTok.length()>0) { /* replace mapTok in the sUrl with cell */ String sR= UtilCM.replaceSubstrInString(baseUrl,mapTok,cell); if(sR==null) hCell= smallCell; /* Do not map the URL since problem */ else { /* Use the mapped token URL */ sUrl= sR; /* mapping succeeded, else just use cell */ hCell= ""+smallCell+""; } } /* replace mapTok in the sUrl with cell */ else { /* just append cell to URL */ sUrl= baseUrl+cell; hCell= ""+smallCell+""; } return(hCell); } /* buildMapUrl */ /** * makeOptionsListFromSemicolonList() - map a ";;" delimited cell * string to an < OPTION > list. This is triggered by * makeOptionsListFromSemicolonListFlag. * @param cell data to convert * @return converted HTML string or the original string if not converted. */ public String makeOptionsListFromSemicolonList(String cell) { /* makeOptionsListFromSemicolonList */ String sToken= "", sTmp= cell, sR= ""; int idx= cell.indexOf(";"); if(idx==-1 || cell==null) return(cell); String semiStr= cell.substring(idx); if(!semiStr.startsWith(";;")) return(cell); sR += "\n"; return(sR); } /* makeOptionsListFromSemicolonList */ /** * testIfRowIsBlank() - test if remove trailing blank lines after * first line. Note the trailing line could be short so check for * that as well. If tRowData.length != tCols, then ill-formed Table data. * @param tRowData is row of tData to test if blank. * @param tCols is the number of columns to test. * @return true if the line is blank */ public boolean testIfRowIsBlank(String tRowData[], int tCols) { /* testIfRowIsBlank */ if(tRowData==null || tCols<=0) return(true); int testCols= tCols, trdSize= tRowData.length; if(trdSize0) return(false); /* has at least 1 non-null data cell */ } return(true); } /* testIfRowIsBlank */ /** * concatInputFilesWithSameFields() - to create a new concatenated * tab-delimited .txt output file from the set of input text files * IFF they have exactly the same column header names. The * -outputDir switch specifies where the file is saved. Default is * not to concatenate the input files. Data is appended into concatStr. * The '-concatTables:{outputConcatenateFile.txt}' switch enables this * option by setting the concatInputFilesWithSameFieldsFlag switch. * @param fileNbr - is the order of the file in the list of files * starting with 0. * @param ft is the instance of the FileTable to be concatenated * @return true if succeed. */ public boolean concatInputFilesWithSameFields(int fileNbr, FileTable ft) { /* concatInputFilesWithSameFields */ if(ft==null) return(false); int tRows= ft.tRows; String tRowData[]; /* faster unpacking */ if(fileNbr==0) { /* do any initialization we need */ cvt.ftConcat= new FileTable("Concat-Table"); cvt.ftConcat.setHasTableHeaderFlag(true); cvt.ftConcat.setDuplicateFieldsFlag(false); cvt.ftConcat.setNbrTableHdrLines(1); cvt.ftConcat.setHasEmptyLineBeforeTableFlag(false); /* Set the Fields list to the Table and set tCols * for the first file only. */ cvt.ftConcat.setFieldsToTable(ft.tFields); cvt.ftConcat.setHeadersToTable(ft.tHeader, ft.tHdrRows, ft.tCols); } /* do any initialization we need */ for(int r=0;r bigShrinkCellThr. * Then shrink the string by decreasing the font size. * @param str to shrink * @return shrunken string. */ public String shrinkStrToSmallerFont(String str) { /* shrinkStrToSmallerFont */ String sR= str; if(str.length()>cvt.bigShrinkCellThr) sR = ""+str+""; else if(cvt.useHTMLfontSize!=null) sR = ""+str+""; return(sR); } /* shrinkStrToSmallerFont */ /** * exportBigStrToHTMLfile() - if the |str| > bigExportCellThr. * Then export the entire string and replace it with a * leading string of maxChars. Create the file in oDir. * Create a file "big-R"+r+"C"+c+oFileName in oDir. *

* [TODO] add javascript focus option instead of separate files. * @param str to export to a file * @param oDir is output directory to write the file * @param ofileName is name of .html file with .html extension * @param r row of cell * @param c column of cell * @param maxChars to use in HREF * @return shrunken string. * * @see FileTable#writeStringToFile */ public String exportBigStrToHTMLfile(String str, String oDir, String oFileName, int r, int c, int maxChars) { /* exportBigStrToHTMLfile */ String sR= str; if(str.length()>cvt.bigExportCellThr) { /* go Export it */ String rcURLfile= "big-R"+r+"C"+c+"-"+oFileName+".html", fullPath= oDir + rcURLfile, subStr= str.substring(0,cvt.bigExportCellThr); sR = ""+subStr+"..."; /* Create the file */ fullPath= fio.mapPathFileSeparators(fullPath); /* Ensure right file sep */ fio.makePathSubDirs(fullPath); boolean flag= fio.writeStringToFile(fullPath, str); if(!flag) return(str); /* failed - just return the string */ } /* go Export it */ return(sR); } /* exportBigStrToHTMLfile */ /** * mapHeaderNames() - try to map header names within the header table * in the tFields[] and tHeader[last][] arrays. This uses the * cvt.nLong2ShortHdrNamesMap names in cvt.fromLongHdrNamesMap[] and * cvt.toShortHdrNamesMap[] set by the * "-mapHdrNames:{mapFile,fromHdrName,toHdrName}" switch. * @param ft is FileTable to map. * @return the number mapped if succeed, also make a list of * this.unmapped names that are not in the names map. */ public int mapHeaderNames(FileTable ft) { /* mapHeaderNames */ int i, c, nMapped= 0, nUnmapped= 0, nNames= cvt.nLong2ShortHdrNamesMap; boolean CHECK_FOR_UNMAPPED_ENTRIES_DBUG= true; /* enable if DEBUGGING */ String fieldC, fromNameI, toNameI, fromNames[]= cvt.fromLongHdrNamesMap, toNames[]= cvt.toShortHdrNamesMap; /* Allocate a list of unmodified tFields[] names with list size * nUnmodifiedFieldNames. */ ft.unmodifiedFieldNames= new String[ft.tCols]; ft.nUnmodifiedFieldNames= 0; for(c=0;c"; else if(sToString.equals("BOLD_GREEN")) sToString= "" + sKeyword + "?"; else if(sToString.equals("BOLD_BLUE")) sToString= "?" + sKeyword + "?"; /* Build the new string */ sR = sPreface + sToString + sRest; } /* map keyword to toString */ return(sR); } /* mapQuestionmarks */ /** * mapDollarsigns() - map data form '$$keyword$$' to '{toString}. * Do this for all instances in the mapDollarsign mapping list. * It can process the string recursively. * @param str input string containing possible $$ mapping. * @return true if succeed */ public String mapDollarsigns(String str) { /* mapDollarsigns */ String sTmp, sTmp2, sPreface= null, sKeyword= null, sRest= null, sToString= null, sR= str; int prefaceIdx= -1, restIdx= -1; /* Look for '$$keyword$$' and extract */ prefaceIdx= str.indexOf("$$"); if(prefaceIdx==-1) return(str); /* not found */ sTmp= str.substring(prefaceIdx+2); restIdx= sTmp.indexOf("$$"); if(restIdx==-1) return(str); /* not found */ sTmp2= sTmp.substring(restIdx+2); /* Extract the three substrings */ sPreface= str.substring(0,prefaceIdx); sKeyword= sTmp.substring(0,restIdx); sRest= sTmp2; /* See if the keyword is in the list */ for(int i=0;i * The args are specified with the * '-extractRow:{colName,rowNbr,resourceTblFile,htmlStyle}' * switch. * @param ft is the FileTable being processed * @param extractColName - column common to both this ft table and * the reference database file * @param extractRowNbr - row to pick up examplar to search with * @param extractResTblFile - reference database file to extract data * @param extractHtmlStyle - string DL, OL, UL or TABLE * @return extracted string from the resource table converted to * HTML if successful, else null. * * @see UtilCM#logMsg * @see FileTable#readFileAsString * @see FileTable#readFileAsTable * @see FileTable#setDuplicateFieldsFlag * @see FileTable#setNbrTableHdrLines * @see FileTable#setRmvTrailingBlankLinesFlag * @see FileTable#setRmvTrailingEmptyColumnsFlag * @see FileTable#setHasEmptyLineBeforeTableFlag * @see FileTable#lookupFieldIdx */ public String extractRowFromResourceTableAsHTML(FileTable ft, String extractColName, int extractRowNbr, String extractResTblFile, String extractHtmlStyle) { /* extractRowFromResourceTableAsHTML */ if(ft==null) return(null); /* [1] Extract the process table cell at (row,col). */ int rIdx= extractRowNbr, cIdx= ft.lookupFieldIdx(extractColName); if(cIdx==-1 || rIdx==-1) { /* extractColName not found in ft.tFields */ UtilCM.logMsg("Problem: There was no '"+extractColName+ "' keyword at row " + extractRowNbr+ "\n in the input file '"+ft.tName+ "' - ignoring -extractRow.\n"); return(null); } /* [1.1] Make sure good keyword from input Table */ String keyword= ft.tData[rIdx][cIdx]; if(keyword==null || keyword.length()==0) { UtilCM.logMsg("Problem: Bad keyword data in '"+extractColName+ "' extractColName at row" + extractRowNbr+ " in the input file - ignoring -extractRow\n"); return(null); } /* [1.2] Set up the switch index for: DL, OL, UL and TABLE */ int styleIdx= STYLE_DL; if(extractHtmlStyle.equals("DL")) styleIdx= STYLE_DL; else if(cvt.extractHtmlStyle.equals("OL")) styleIdx= STYLE_OL; else if(cvt.extractHtmlStyle.equals("UL")) styleIdx= STYLE_UL; else if(cvt.extractHtmlStyle.equals("TABLE")) styleIdx= STYLE_TABLE; /* [2] Make sure resource table file exists */ String extractResTableFile= extractResTblFile; String resTblStr= fio.readFileAsString(extractResTableFile); if(resTblStr==null) { /* See if path is good else try -tableDir directory */ extractResTableFile= cvt.tableDataDir + extractResTblFile; resTblStr= fio.readFileAsString(extractResTableFile); if(resTblStr==null) { UtilCM.logMsg("Problem: can't find resourceTblFile '"+ extractResTableFile+ "' in -extractRow:{colName,rowNbr," + "resourceTblFile,htmlStyle}.\n"); return(null); } } /* [2.1] Setup Table instance */ FileTable ftRsc= new FileTable(resTblStr); ft.setHasTableHeaderFlag(true); ftRsc.setDuplicateFieldsFlag(true); ftRsc.setNbrTableHdrLines(1); ftRsc.setRmvTrailingBlankLinesFlag(true); ftRsc.setRmvTrailingEmptyColumnsFlag(true); ftRsc.setHasEmptyLineBeforeTableFlag(false); /* [2.2] Now, read table from the input file */ if(!ftRsc.readAndParseTable(extractResTableFile) || ftRsc.tFields==null || ftRsc.tCols<=1 ||ftRsc.tRows<=1 ) { UtilCM.logMsg("Bad Table in resourceTblFile '"+ extractResTblFile+ "' in -extractRow:{colName,rowNbr," + "resourceTblFile,htmlStyle}.\n"); return(null); } /* Clean (remove) header and data enclosing whitespace */ ft.trimTableEnclWhitespace(true,true); /* [3] Extract the header row and data row */ String tFieldsRec[]= ftRsc.tFields, tDataRec[][]= ftRsc.tData, keywordRow[]= null; int nColsRsc= ftRsc.tCols, nRowsRsc= ftRsc.tRows, cIdxRsc= -1, /* index in resource table */ rIdxRsc= -1; /* [3.1] Find the resource column in both the input and the * resource Tables if it exists by extractColName. */ cIdxRsc= ftRsc.lookupFieldIdx(extractColName); if(cIdxRsc==-1) { /* extractColName not found in ftRes.tFields */ UtilCM.logMsg("Problem: There was no '"+extractColName+ "' keyword at row" + extractRowNbr+ " in the resource file - ignoring -extractRow\n"); return(null); } /* [3.2] Find the resource row if it exists in the resource table. */ for(int r=0;r\n"; sR += "\n"; /* [5.1] Do first part if any */ switch(styleIdx) { case STYLE_DL: sR += "

\n"; break; case STYLE_OL: sR += "
    \n"; break; case STYLE_UL: sR += "
      \n"; break; case STYLE_TABLE: sR += "\n"; break; } /* process cell */ /* [5.2] All header and data columns except TABLEs */ for(int c=0;c\n
      "+dataCell+"
      \n\n"; break; case STYLE_OL: sR += "
    • "+hdrCell+": "+dataCell+"\n"; break; case STYLE_UL: sR += "
    • "+hdrCell+": "+dataCell+"\n"; break; case STYLE_TABLE: if(c==0) sR += "
    • "; if(cvt.useHTMLfontSize!=null) sR += "\n"; else sR += "\n"; if(c==(nColsRsc-1)) sR += ""; break; } /* process cell */ } /* Process all columns */ /* [5.3] All data columns only for TABLEs */ for(int c=0;c"+ dataCell+"\n"; else sR += "\n"; if(c==(nColsRsc-1)) sR += ""; break; } /* process cell */ } /* Process all columns */ /* [5.4] Do end part if any */ switch(styleIdx) { case STYLE_DL: sR += "\n"; break; case STYLE_OL: sR += "\n"; break; case STYLE_UL: sR += "\n"; break; case STYLE_TABLE: sR += "
      "+ hdrCell+""+hdrCell+"
      "+dataCell+"
      \n"; break; } /* process cell */ sR += "
      \n"; sR += "


      \n"; return(sR); } /* extractRowFromResourceTableAsHTML */ /** * checkAndMakeIndexHtmlDirList() - make index.html files in list of dirs. * in the dirIndexHtml[0:nDirIndexHtml-1] without them. * If '-dirIndexHtml:{dir,'O'verride}' switches are set, * then make an index.html file of all of the files in the * specified directories in each directory in the list of * directories dirIndexHtml[0:nDirIndexHtml-1]. In addition, * if the corresponding dirIndexHtmlOverrideFlag[0:nDirIndexHtml-1] * flag is set to 'Override', then override "index.html" files. * Call subdirectories recursively. * @param nDirIndexHtml - number of directories specified * @param dirIndexHtml - list of relative directories to check * @param dirIndexHtmlOverrideFlag - list of 'Override' "index.html" flags * @return true if successful */ public boolean checkAndMakeIndexHtmlDirList(int nDirIndexHtml, String parentDir, String dirIndexHtml[], boolean dirIndexHtmlOverrideFlag[]) { /* checkAndMakeIndexHtmlDirList */ if(nDirIndexHtml<=0 || dirIndexHtml==null || dirIndexHtmlOverrideFlag==null) return(false); for(int i=0;i0) dirToCheck= parentDir + cvt.fileSep + dirToCheck; try { /* analyze directory */ File fd= new File(dirToCheck); if(fd.isFile()) return(true); /* ignore simple files */ String dirList[]= fd.list(); /* get list of files */ int nFiles= dirList.length; /* Call recursively */ boolean hasIndexHtmlFlag= false; for(int i= 0; i < nFiles; i++) if(dirList[i].equals("index.html")) { /* has index file, just continue */ hasIndexHtmlFlag= true; break; } /* go make the index file */ if(!hasIndexHtmlFlag || overrideFlag) { /* make or override the index.html file */ if(!makeIndexHtml(fd,dirToCheck,dirList)) return(false); else UtilCM.logMsg("Added 'index.html' file in directory '"+ dirToCheck+"'\n"); } /* Call recursively */ for(int i=0;i\n"+ "\n\n"+ ""+title+"\n\n"+ "\n", sEpilogue= "\n\n", sText= "

      "+title+"

      \n\n"+ "
        \n"; /* Process all but the "index.html" file in dirName/ */ for(int i=0;i"+fileName+"\n"; } sText += "
      \n\n"; sText += "


      \n"; /* Write out the index.html file */ String sR= sPreface + sText+ sEpilogue, indexFileName= dirName+"/index.html", indexFileNamePath= fio.mapPathFileSeparators(indexFileName); if(!fio.writeStringToFile(indexFileNamePath, sR)) { UtilCM.logMsg("Problem saving 'index.html' file in '"+ indexFileNamePath+"\n"); return(false); } return(true); } /* makeIndexHtml */ /** * checkIfDir() - test if fileName is a directory * @param fileName to test * @return true if directory */ public boolean checkIfDir(String dirName, String fileName) { /* checkIfDir */ String pathName= dirName + cvt.fileSep + fileName; pathName= fio.mapPathFileSeparators(pathName); try { /* analyze directory */ File fd= new File(pathName); if(fd.isDirectory()) return(true); /* ignore simple files */ } catch (Exception e) { return (false); } return (false); } /* checkIfDir */ /** * mapDataToCellBGcolor() - map data cell number to cell background color * the colors are in cvt.heatMapColors[]. * @param sVal is positive numeric value in range of * [cvt.glbMinRowVal, cvt.glbMaxRowVal] * @param defaultBGcolor is the default background color. * @return background color from cvt.heatMapColors in corresponding quantile. */ public String mapDataToCellBGcolor(String sVal, String defaultBGcolor) { /* mapDataToCellBGcolor */ String sBGcolor= defaultBGcolor; if(!UtilCM.isNumber(sVal)) return(defaultBGcolor); float fVal= UtilCM.cvs2f(sVal); if(fVal<0.0F) return(defaultBGcolor); int nQuantiles= cvt.heatMapColors.length; float fMin= cvt.glbMinRowVal, fMax= cvt.glbMaxRowVal, fRange= (fMax-fMin), qDiff= fRange/nQuantiles, fQ= (fVal - fMin)/qDiff; int qq= (int)(fQ), q= Math.min(Math.max(0,qq), (nQuantiles-1)); sBGcolor= " bgcolor=\""+cvt.heatMapColors[q]+"\""; return(sBGcolor); } /* mapDataToCellBGcolor */ /** * makeColorMapScaleHTML() - make colormap data scale TABLE HTML * mapping the cvt.heatMapColors[] to positive numeric value in range of * [cvt.glbMinRowVal, cvt.glbMaxRowVal] * @return background color from cvt.heatMapColors in corresponding quantile. */ public String makeColorMapScaleHTML() { /* makeColorMapScaleHTML */ int nQuantiles= cvt.heatMapColors.length; float fMin= cvt.glbMinRowVal, fMax= cvt.glbMaxRowVal, fRange= (fMax-fMin), qDiff= fRange/nQuantiles; String sR= "

      \n"+ "Scale of intensity data quantile values and corresponding background color
      \n"+ "The intensity data range ["+UtilCM.cvf2s(fMin,2)+" : "+UtilCM.cvf2s(fMax,2)+ "] is for the entire database '"+cvt.curInputFile+"' file.\n
      ", sTbl= "\n", sHdr= "\n", sQuantile= "\n", sData= "\n"; sHdr += "\n"; sQuantile= "\n"; sData= "\n"; for(int q=0;q\n"; sQuantile += "\n"; sData += " "+"\n"; } sHdr += "\n"; sQuantile += "\n"; sData += "\n"; sTbl += sHdr + sQuantile + sData + "
      Data valueQuantileColor"+(q+1)+"
      \n"; sR += sTbl; return(sR); } /* makeColorMapScaleHTML */ } /* end of class Convert */