package com.dbxml.xml.dtsm;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: DTSMHelper.java,v 1.5 2004/02/12 00:17:58 bradford Exp $
 */

import java.io.*;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;

import com.dbxml.xml.QName;
import com.dbxml.xml.SymbolTable;
import com.dbxml.xml.dom.DOMProducer;
import com.dbxml.xml.dom.DOMTableGenerator;
import com.dbxml.xml.sax.SAXProducer;
import com.dbxml.xml.sax.SAXTableGenerator;
import com.dbxml.xml.text.TextProducer;
import com.dbxml.xml.text.TextTableGenerator;

/**
 * DTSMHelper
 */

public final class DTSMHelper {
   private DTSMHelper() {
   }

   public static Node tableToNode(DocumentTable table) throws DTSMException {
      if ( table != null ) {
         try {
            DOMProducer producer = new DOMProducer(table);
            producer.process();
            return producer.getNode();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static Document tableToDocument(DocumentTable table) throws DTSMException {
      if ( table != null ) {
         try {
            DOMProducer producer = new DOMProducer(table);
            producer.process();
            Node node = producer.getNode();
            if ( node.getNodeType() == Node.DOCUMENT_NODE )
               return (Document)node;
            else
               return node.getOwnerDocument();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static void tableToWriter(DocumentTable table, Writer writer) throws DTSMException {
      if ( table != null ) {
         try {
            TextProducer producer = new TextProducer(table);
            producer.setWriter(writer);
            producer.process();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
   }

   public static void tableToWriter(DocumentTable table, Writer writer, boolean omitPrefix) throws DTSMException {
      if ( table != null ) {
         try {
            TextProducer producer = new TextProducer(table, omitPrefix);
            producer.setWriter(writer);
            producer.process();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
   }

   public static void tableToWriter(DocumentTable table, PrintWriter writer) throws DTSMException {
      if ( table != null ) {
         try {
            TextProducer producer = new TextProducer(table);
            producer.setWriter(writer);
            producer.process();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
   }

   public static void tableToWriter(DocumentTable table, PrintWriter writer, boolean omitPrefix) throws DTSMException {
      if ( table != null ) {
         try {
            TextProducer producer = new TextProducer(table, omitPrefix);
            producer.setWriter(writer);
            producer.process();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
   }

   public static String tableToText(DocumentTable table) throws DTSMException {
      if ( table != null ) {
         StringWriter writer = new StringWriter(4096);
         tableToWriter(table, writer);
         return writer.toString();
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static String tableToText(DocumentTable table, boolean omitPrefix) throws DTSMException {
      if ( table != null ) {
         StringWriter writer = new StringWriter(4096);
         tableToWriter(table, writer, omitPrefix);
         return writer.toString();
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static void tableToSAX(DocumentTable table, ContentHandler handler) throws DTSMException {
      if ( table != null ) {
         try {
            SAXProducer sp = new SAXProducer(table, handler);
            sp.process();
         }
         catch ( DTSMException e ) {
            throw e;
         }
         catch ( Exception e ) {
            throw new DTSMException(e);
         }
      }
   }

   public static DocumentTable nodeToTable(Node node, SymbolTable symbols) throws DTSMException {
      if ( node != null ) {
         DOMTableGenerator dtm = new DOMTableGenerator(node);
         dtm.setSymbolTable(symbols);
         return dtm.process();
      }
      else
         throw new DTSMException("Node is null");
   }

   public static DocumentTable documentToTable(Document doc, SymbolTable symbols) throws DTSMException {
      return nodeToTable(doc, symbols);
   }

   public static DocumentTable textToTable(String text, SymbolTable symbols) throws DTSMException {
      if ( text != null ) {
         TextTableGenerator ttg = new TextTableGenerator(text);
         ttg.setSymbolTable(symbols);
         return ttg.process();
      }
      else
         throw new DTSMException("String is null");
   }

   public static DocumentTable bytesToTable(byte[] b, SymbolTable symbols) throws DTSMException {
      if ( b != null ) {
         TextTableGenerator ttg = new TextTableGenerator(b);
         ttg.setSymbolTable(symbols);
         return ttg.process();
      }
      else
         throw new DTSMException("Byte array is null");
   }

   public static DocumentTable charsToTable(char[] ch, SymbolTable symbols) throws DTSMException {
      if ( ch != null ) {
         TextTableGenerator ttg = new TextTableGenerator(ch);
         ttg.setSymbolTable(symbols);
         return ttg.process();
      }
      else
         throw new DTSMException("Char array is null");
   }

   public static DocumentTable inputStreamToTable(InputStream is, SymbolTable symbols) throws DTSMException {
      if ( is != null ) {
         SAXTableGenerator stg = new SAXTableGenerator(is);
         stg.setSymbolTable(symbols);
         return stg.process();
      }
      else
         throw new DTSMException("InputStream is null");
   }

   public static DocumentTable readerToTable(Reader reader, SymbolTable symbols) throws DTSMException {
      if ( reader != null ) {
         SAXTableGenerator stg = new SAXTableGenerator(reader);
         stg.setSymbolTable(symbols);
         return stg.process();
      }
      else
         throw new DTSMException("Reader is null");
   }

   public static DocumentTable fileToTable(File f, SymbolTable symbols) throws DTSMException {
      if ( f != null ) {
         try {
            SAXTableGenerator stg = new SAXTableGenerator(f);
            stg.setSymbolTable(symbols);
            return stg.process();
         }
         catch ( IOException e ) {
            throw new DTSMException("Couldn't convert file '"+f.getName()+"' to table", e);
         }
      }
      else
         throw new DTSMException("File is null");
   }


   public static CompressedTable compressTable(DocumentTable table) throws DTSMException {
      if ( table != null ) {
         TableCompressor c = new TableCompressor(table);
         return c.buildCompressedTable();
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static CompressedTable compressTable(TableBuilder builder) throws DTSMException {
      if ( builder != null ) {
         TableCompressor c = new TableCompressor(builder);
         return c.buildCompressedTable();
      }
      else
         throw new DTSMException("TableBuilder is null");
   }

   public static TableBuilder copyTable(DocumentTable table, SymbolTable symbols) throws DTSMException {
      if ( table != null ) {
         TableBuilder builder = new TableBuilder(symbols);
         PullProcessor proc = new PullProcessor(table);
         while ( proc.hasNext() ) {
            DocumentTableEntry entry = proc.next();
            byte typeID = entry.getTypeID();
            int valID = entry.getValID();
            if ( Constants.isPayloadSymbol(typeID) ) {
               if ( valID != -1 ) {
                  QName qname = symbols.getQName(valID);
                  builder.addEntry(typeID, qname);
               }
               else
                  builder.addEntry(typeID);
            }
            else {
               byte[] b = table.getValue(valID).getData();
               builder.addEntry(typeID, b);
            }
         }
         return builder;
      }
      else
         throw new DTSMException("DocumentTable is null");
   }

   public static TableBuilder copyTable(DocumentTable table) throws DTSMException {
      return copyTable(table, table.getSymbolTable());
   }
}
