    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 Csv2Tex {
    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 + ".txt"));
    044
    045                pw.println("\\begin{table}[htb]");
    046                pw.println("\\renewcommand{\\arraystretch}{1.2}");
    047                String t = "l|"; for (int j = 1; j < csv.getHeader().size(); j++) t += "c";
    048                pw.println("\\begin{tabular}{" + t + "} \\hline \\hline");
    049
    050                for (int j = 0; j < csv.getHeader().size(); j++) {
    051                    if (j > 0) pw.print(" \\ & \\ ");
    052                    pw.print(csv.getHeader().getField(j).toString());
    053                }
    054                pw.println(" \\ \\\\ \\hline");
    055
    056                for (CSVLine line: csv.getLines()) {
    057                    for (int j = 0; j < line.size(); j++) {
    058                        if (j == 0)
    059                            pw.print(line.getField(j));
    060                        else
    061                            pw.print(" & " + (line.getField(j) == null || line.getField(j).isEmpty() ? "-" : "$" + line.getField(j).toString().replace("%", "\\%") + "$"));
    062                    }
    063                    pw.println(" \\\\");
    064                }
    065
    066                pw.println("\\hline \\hline");
    067                pw.println("\\end{tabular}");
    068                pw.println("\\caption{FIXME: Add caption here.}");
    069                pw.println("\\label{tab:" + name.toLowerCase() + "}");
    070                pw.println("\\end{table}");
    071
    072                pw.flush(); pw.close();
    073            }
    074        } catch (Exception e) {
    075            e.printStackTrace();
    076        }
    077    }
    078
    079}