Package de.kunysch.tvbrowser.localimdb

Source Code of de.kunysch.tvbrowser.localimdb.HtmlExporter

/* $Id: HtmlExporter.java 124 2008-03-06 07:19:25Z bananeweizen $
* GNU GPL Version 2, Copyright (C) 2005 Paul C. Kunysch */
package de.kunysch.tvbrowser.localimdb;

import java.io.*;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import javax.swing.JOptionPane;

import util.browserlauncher.Launch;
import util.ui.Localizer;
import de.kunysch.localimdb.Movie;
import de.kunysch.tvbrowser.WrappedProgram;
import devplugin.Marker;
import devplugin.Program;
import devplugin.ProgramFieldType;

/** This class can export Programs to a HTML file. */
public class HtmlExporter {
  private static Localizer mLocalizer = util.ui.Localizer.getLocalizerFor(HtmlExporter.class);

  public static void openHtmlFile(Program[] programs) {
    final boolean listSorted = isProgramListSorted(programs);
    PrintWriter pw = null;
    File file = null;
    try {
      file = File.createTempFile("localimdb_", ".html"); //$NON-NLS-1$ //$NON-NLS-2$
      pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("ISO-8859-1"))); //$NON-NLS-1$
      printHeader(pw);
      printDisclaimer(pw);
      pw.println("<table width=\"100%\">"); //$NON-NLS-1$
      long day = 0L;
      for (int i = 0; i < programs.length; ++i) {
        final Program prog = programs[i];
        final RatedProgram rprog = (null == ((WrappedProgram) prog)) ? null : (RatedProgram) ((WrappedProgram) prog)
            .getWrappedProgram(RatedProgram.class);
        final Marker[] markers = prog.getMarkerArr();
        final boolean isMarked = (null != markers && 0 < markers.length);
        boolean isFirst = false;
        if (listSorted && day < prog.getDate().getValue()) {
          day = prog.getDate().getValue();
          pw.println(MessageFormat.format("<td colspan=\"3\" class=\"date\">{0,date,full}</td></tr>", //$NON-NLS-1$
              new Object[] { prog.getDate().getCalendar().getTime() }));
          isFirst = true;
        } else if (day == 0) {
          day = prog.getDate().getValue();
          isFirst = true;
        }
        pw.println("<tr" + (isFirst ? " class=\"first\"" : "") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            + (isMarked ? " class=\"mark\"" : "") + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        printTimeAndChannel(pw, prog, listSorted);
        printTitle(pw, prog, rprog);
        printImdbData(pw, rprog);
      }
      pw.println("</table>"); //$NON-NLS-1$
      pw.println("</body></html>"); //$NON-NLS-1$
      Launch.openURL(file.toURI().toString());
    } catch (IOException e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, e.getMessage(), e.getClass().getName(), JOptionPane.ERROR_MESSAGE);
    } finally {
      if (null != pw) {
        pw.close();
      }
      if (null != file) {
        file.deleteOnExit();
      }
    }
  }

  /**
   * @param pw
   * @param rprog
   */
  private static void printImdbData(PrintWriter pw, final RatedProgram rprog) {
    if (null != rprog) {
      final Movie mov = rprog.getMovie();
      pw.println("<td>" + mov.getRank() / 10.0 + ", " + mov.getVotes() + ", " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          + mov.getYear() + "</td>"); //$NON-NLS-1$
    }
    pw.print("</tr>"); //$NON-NLS-1$
  }

  /**
   * @param pw
   * @param prog
   * @param rprog
   */
  private static void printTitle(PrintWriter pw, final Program prog, final RatedProgram rprog) {
    if (null != rprog) {
      pw.print("<td class=\"title\" width=\"100%\">"); //$NON-NLS-1$
    } else {
      pw.print("<td class=\"title\" colspan=\"2\" width=\"100%\">"); //$NON-NLS-1$
    }
    if (null != rprog && !rprog.isTitleFound()) {
      pw.println("<em>" + prog.getTitle() + "</em></td>"); //$NON-NLS-1$ //$NON-NLS-2$
    } else {
      pw.println(prog.getTitle() + "</td>"); //$NON-NLS-1$
    }
  }

  /**
   * @param pw
   * @param prog
   * @param skipDate
   */
  private static void printTimeAndChannel(PrintWriter pw, final Program prog, final boolean skipDate) {
    if (!skipDate) {
      pw.print(MessageFormat.format("<td>{0,date,short}</td>", new Object[] { prog.getDate() //$NON-NLS-1$
          .getCalendar().getTime() }));
    }
    pw.println("<td>" + prog.getTimeFieldAsString(ProgramFieldType.START_TIME_TYPE) //$NON-NLS-1$
        + " - " + prog.getTimeFieldAsString(ProgramFieldType.END_TIME_TYPE) //$NON-NLS-1$
        + ", " + prog.getChannel().getName() + "</td>"); //$NON-NLS-1$ //$NON-NLS-2$
  }

  /**
   * @param programs
   */
  private static boolean isProgramListSorted(Program[] programs) {
    for (int i = 1; i < programs.length; ++i) {
      if (0 < programs[i - 1].getDate().compareTo(programs[i].getDate())) {
        return false;
      }
    }
    return true;
  }

  /**
   * @param pw
   */
  private static void printDisclaimer(PrintWriter pw) {
    pw.print("<p class=\"notprinted\">"); //$NON-NLS-1$
    pw
        .print(mLocalizer
            .msg(
                "reminder",
                "We'd like to remind you that you may not republish TV-Browser and\u0020IMDb data. We offer this HTML-export to simplify printing. We do not\u0020want to encourage website creation."));
    pw.println("</p>"); //$NON-NLS-1$
  }

  /**
   * @param pw
   */
  private static void printHeader(PrintWriter pw) {
    pw.println(MessageFormat.format("<html><head><title>{0}</title>", //$NON-NLS-1$
        new Object[] { mLocalizer.msg("html.title", "Exported Programs") })); //$NON-NLS-1$
    pw.println("<style type=\"text/css\"><!--"); //$NON-NLS-1$
    pw.println("@media print { .notprinted { display: none; } }"); //$NON-NLS-1$
    pw.println("BODY { font-family: Georgia, serif; }"); //$NON-NLS-1$
    pw.println("TD { border-top: 1pt dashed black; vertical-align: bottom; white-space: nowrap; }"); //$NON-NLS-1$
    pw.println("TD.date, TR.first TD { border-top: 0pt dashed black; }"); //$NON-NLS-1$
    pw.println("TD.date { font-weight: bold; font-size: larger; text-align: center;"); //$NON-NLS-1$
    pw.println("          border: 1pt solid black; background-color: #ddd;}"); //$NON-NLS-1$
    pw.println("TD.title { white-space: normal; }"); //$NON-NLS-1$
    pw.println("TR.mark { font-weight: bold; }"); //$NON-NLS-1$
    pw.println("TD {  } --></style></head><body>"); //$NON-NLS-1$
  }
}
TOP

Related Classes of de.kunysch.tvbrowser.localimdb.HtmlExporter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.