001package net.sf.cpsolver.ifs.util;
002
003import java.io.File;
004import java.io.PrintWriter;
005
006import net.sf.cpsolver.ifs.util.CSVFile.CSVLine;
007
008/**
009 * A simple class converting CSV files to LaTeX tables.
010 * 
011 * @version IFS 1.2 (Iterative Forward Search)<br>
012 *          Copyright (C) 2006 - 2010 Tomáš Müller<br>
013 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
014 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
015 * <br>
016 *          This library is free software; you can redistribute it and/or modify
017 *          it under the terms of the GNU Lesser General Public License as
018 *          published by the Free Software Foundation; either version 3 of the
019 *          License, or (at your option) any later version. <br>
020 * <br>
021 *          This library is distributed in the hope that it will be useful, but
022 *          WITHOUT ANY WARRANTY; without even the implied warranty of
023 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024 *          Lesser General Public License for more details. <br>
025 * <br>
026 *          You should have received a copy of the GNU Lesser General Public
027 *          License along with this library; if not see
028 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
029 */
030
031public class Csv2Html {
032    
033    public static void main(String args[]) {
034        try {
035            for (int i = 0; i < args.length; i++) {
036                File file = new File(args[i]);
037                
038                String name = file.getName();
039                if (name.contains(".")) name = name.substring(0, name.indexOf('.'));
040                
041                CSVFile csv = new CSVFile(file);
042                
043                PrintWriter pw = new PrintWriter(new File(file.getParentFile(), name + ".html"));
044
045                pw.println("<table>");
046
047                pw.println("\t<tr>");
048                for (int j = 0; j < csv.getHeader().size(); j++) {
049                    pw.println("\t\t<th style='border-bottom: 1px #8000AD dashed;' align='" + (j == 0 ? "left" : "center") + "'>" + csv.getHeader().getField(j).toString() + "</th>");
050                }
051                pw.println("\t</tr>");
052                
053                boolean header = false;
054                for (CSVLine line: csv.getLines()) {
055                    if (line.getField(0).isEmpty()) {
056                        pw.println("\t<tr><td colspan='" + line.size() + "'>&nbsp;</td></tr>");
057                        header = true;
058                        continue;
059                    }
060                    pw.println("\t<tr>");
061                    for (int j = 0; j < line.size(); j++) {
062                        if (header) {
063                            pw.println("\t\t<th style='border-bottom: 1px #8000AD dashed;' align='" + (j == 0 ? "left" : "center") + "'>" + line.getField(j).toString() + "</th>");
064                        } else {
065                            pw.println("\t\t<td align='" + (j == 0 ? "left" : "center") + "' nowrap>" + (line.getField(j) == null || line.getField(j).isEmpty() ? "-" : line.getField(j).toString().replace("+/-", "&plusmn;")) + "</td>");
066                        }
067                    }
068                    pw.println("\t</tr>");
069                    header = false;
070                }
071                
072                pw.println("</table>");
073                
074                pw.flush(); pw.close();
075            }
076        } catch (Exception e) {
077            e.printStackTrace();
078        }
079    }
080
081}