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