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
instead of | .
*/
if(cvt.hasTableHeaderRowFlag)
{ /* process the header rows */
for(int h=0;h 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 | |
---|
instead of | . */ if(tFields!=null) { /* process the header rows */ /* [5.1.1] Now create the row of | data */ sRow = " |
---|
\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
* 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(trdSize
* [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 \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";
break;
case STYLE_OL:
sR += "
\n";
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";
break;
} /* process cell */
sR += "";
if(cvt.useHTMLfontSize!=null)
sR += " ";
break;
} /* process cell */
} /* Process all columns */
/* [5.3] All data columns only for TABLEs */
for(int c=0;c"+
hdrCell+" \n";
else
sR += ""+hdrCell+" \n";
if(c==(nColsRsc-1))
sR += ""+
dataCell+" \n";
else
sR += ""+dataCell+" \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 += "
\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;i"+title+"
\n\n"+
"\n";
/* Process all but the "index.html" file in dirName/ */
for(int i=0;i
\n\n";
sText += "
\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";
sR += sTbl;
return(sR);
} /* makeColorMapScaleHTML */
} /* end of class Convert */
\n",
sQuantile= " \n",
sData= " \n";
sHdr += " \n";
sQuantile += "\n";
sData += "\n";
sTbl += sHdr + sQuantile + sData + "Data value \n";
sQuantile= "Quantile \n";
sData= "Color \n";
for(int q=0;q"+(q+1)+" \n";
sData += " "+" \n";
}
sHdr += "