/*
 * This file is part of the Poliqarp suite.
 * 
 * Copyright (C) 2004-2009 by Instytut Podstaw Informatyki Polskiej
 * Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 * Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file gpl.txt included in the packaging
 * of this file.  (See http://www.gnu.org/licenses/translations.html for
 * unofficial translations.)
 * 
 * A commercial license is available from IPI PAN (contact
 * Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
 * information).  Licensees holding a valid commercial license from IPI
 * PAN may use this file in accordance with that license.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

package ipipan.poliqarp.gui;

import java.io.*;
import java.util.Iterator;

import ipipan.poliqarp.logic.Match;
import ipipan.poliqarp.logic.Query;
import ipipan.poliqarp.logic.Segment;
import ipipan.poliqarp.logic.results.ColumnType;
import ipipan.poliqarp.util.PoliqarpUtilities;

public class HTMLResultWriter extends ResultWriter 
{
   public HTMLResultWriter(Configuration cfg)
   {
      super(cfg);
   }

   protected void writeHeader(Query query, String encoding) 
   {
      String squery = PoliqarpUtilities.htmlSpecialChars(query.toString());
      writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
            + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
      writer.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\""
            + " lang=\"en\">");
      writer.println("  <head>");
      writer.println("    <meta http-equiv=\"Content-type\" "
            + "content=\"text/html; charset=" + encoding + "\" />");
      writer.println("    <meta name=\"Generator\" content=\"Poliqarp\" />");
      writer.println("    <title>" + cfg.getComponentName("TEXT_RESULTS") + " " 
            + squery + "</title>");
      writer.println("  </head>");
      writer.println("  <body>");
      writer.println("    <h1>" + cfg.getComponentName("TEXT_RESULTS") + " <tt>" 
            + squery + "</tt></h1>");
      writer.println("    <table>");
      writer.println("      <tr>");

      for (String th : colHeaders) {
         writer.println("        <th>" + th + "</th>");
      } 
      writer.println("      </tr>");
   }
    
   protected void writeMatch(Match match) 
   {
      writer.println("      <tr>");
      for (int i = 0; i < numCols; i++) {
         writer.println("        <td>" + match.getCell(i).toHTML(cfg) + "</td>");
      }
      writer.println("      </tr>");
   }
    
   protected void writeFooter() {
      writer.println("    </table>");
      writer.println("  </body>");
      writer.println("</html>");
   }
}
