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