//
// ------
//
// SAVOT Sample
//
// Author:  André Schaaff
// Address: Centre de Donnees astronomiques de Strasbourg
//          11 rue de l'Universite
//          67000 STRASBOURG
//          FRANCE
// Email:   schaaff@astro.u-strasbg.fr, question@simbad.u-strasbg.fr
//
// -------
//
// In accordance with the international conventions about intellectual
// property rights this software and associated documentation files
// (the "Software") is protected. The rightholder authorizes :
// the reproduction and representation as a private copy or for educational
// and research purposes outside any lucrative use,
// subject to the following conditions:
//
// The above copyright notice shall be included.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON INFRINGEMENT,
// LOSS OF DATA, LOSS OF PROFIT, LOSS OF BARGAIN OR IMPOSSIBILITY
// TO USE SUCH SOFWARE. IN NO EVENT SHALL THE RIGHTHOLDER BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For any other exploitation contact the rightholder.
//
//                        -----------
//
// Conformement aux conventions internationales relatives aux droits de
// propriete intellectuelle ce logiciel et sa documentation sont proteges.
// Le titulaire des droits autorise :
// la reproduction et la representation a titre de copie privee ou des fins
// d'enseignement et de recherche et en dehors de toute utilisation lucrative.
// Cette autorisation est faite sous les conditions suivantes :
//
// La mention du copyright portee ci-dessus devra etre clairement indiquee.
//
// LE LOGICIEL EST LIVRE "EN L'ETAT", SANS GARANTIE D'AUCUNE SORTE.
// LE TITULAIRE DES DROITS NE SAURAIT, EN AUCUN CAS ETRE TENU CONTRACTUELLEMENT
// OU DELICTUELLEMENT POUR RESPONSABLE DES DOMMAGES DIRECTS OU INDIRECTS
// (Y COMPRIS ET A TITRE PUREMENT ILLUSTRATIF ET NON LIMITATIF,
// LA PRIVATION DE JOUISSANCE DU LOGICIEL, LA PERTE DE DONNEES,
// LE MANQUE A GAGNER OU AUGMENTATION DE COUTS ET DEPENSES, LES PERTES
// D'EXPLOITATION,LES PERTES DE MARCHES OU TOUTES ACTIONS EN CONTREFACON)
// POUVANT RESULTER DE L'UTILISATION, DE LA MAUVAISE UTILISATION
// OU DE L'IMPOSSIBILITE D'UTILISER LE LOGICIEL, ALORS MEME
// QU'IL AURAIT ETE AVISE DE LA POSSIBILITE DE SURVENANCE DE TELS DOMMAGES.
//
// Pour toute autre utilisation contactez le titulaire des droits.
//

import java.io.*;

import cds.savot.model.*;
import cds.savot.pull.*;

/**
 * <p>A sample designed to show the sequential usage of the pull parser </p>
 * @author Andre Schaaff
 * @version 2.5 Copyright CDS 2002-2004
 */
public class PullSeqSample {

  /**
  *
  * @param source
  * @param target
  * @param type
  */
  public PullSeqSample(String source, String target, int type) {

    TRSet tr = null;

    try {

    // begin the parsing
    SavotPullParser sb = new SavotPullParser(source, type);

    // get the next resource of the VOTable file
    SavotResource currentResource = sb.getNextResource();

    // while a resource is available
    while (currentResource != null) {
      System.out.println("-----------------------------------------------------");
      System.out.println("RESOURCE" + " (" + currentResource.getName() + ")" + " BEGIN");

      // for each table of this resource
      for (int i = 0; i < currentResource.getTableCount(); i++) {

        tr = currentResource.getTRSet(i);

        System.out.println("Desc : " + currentResource.getDescription());

        if (tr != null) {
          System.out.println("Number of items in TRset (= number of <TR></TR>) : " + tr.getItemCount());

          // for each row of the table
          for (int j = 0; j < tr.getItemCount(); j++) {

            // get all the data of the row
            TDSet theTDs = tr.getTDSet(j);

            String currentLine = new String();

            System.out.println("Number of items in TDSet for the index " + (j+1) + " tr (= number of <TD></TD>) : " + theTDs.getItemCount());

            // for each data of the row
            for (int k = 0; k < theTDs.getItemCount(); k++) {
              currentLine = currentLine + theTDs.getContent(k);
              System.out.println("<" + theTDs.getContent(k) + ">");
            }
          }
        }
      }
      // get the next resource
      currentResource = sb.getNextResource();
      if (currentResource != null) System.out.println("RESOURCE" + " (" + currentResource.getName() + ")" + " END");
    }
    System.out.println("resource counter   : " + sb.getResourceCount());
    System.out.println("table counter      : " + sb.getTableCount());
    System.out.println("table per resource : " + (float)sb.getTableCount() / (float)sb.getResourceCount());
    System.out.println("row counter        : " + sb.getTRCount());
    System.out.println("row per table      : " + (float)sb.getTRCount() / (float)sb.getTableCount());
    System.out.println("data counter       : " + sb.getDataCount());
    System.out.println("data per raw       : " + (float)sb.getDataCount() / (float)sb.getTRCount());
    System.out.println("data per table     : " + (float)sb.getDataCount() / (float)sb.getTableCount());
    }
    catch(Exception e) {System.err.println("PullSeqSample : " + e);};
  }

  /**
  * Main method
  * @param argv
  * @throws IOException
  */
  public static void main  (String [] argv) throws IOException{

    if (argv.length == 0)
      System.out.println("Usage: java PullSeqSample <source> <target> or java PullSeqSample <source> ");
    else  {
      if (argv.length == 2)
        new PullSeqSample(argv[0], argv[1], SavotPullEngine.SEQUENTIAL);
      else
        new PullSeqSample(argv[0], null, SavotPullEngine.SEQUENTIAL);
    }
  }
}

