package com.dbxml.db.client.helpers;

/*
 * 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: TextResultSetClient.java,v 1.3 2004/02/12 00:17:55 bradford Exp $
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.*;

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ResultSetClient;
import com.dbxml.db.core.query.helpers.ResultSetWrapper;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;

/**
 * TextResultSetClient
 */

public final class TextResultSetClient implements ResultSetClient {
   private CollectionClient col;
   private Iterator iterator;
   private Document doc;
   private Element elem;
   private int count = -1;
   private String style;
   private String query;
   private String resCol;
   private String resKey;

   public TextResultSetClient(CollectionClient col, String text, String style, String query) {
      this.col = col;
      this.style = style;
      this.query = query;

      try {
         doc = DOMHelper.parseText(text);
         Element root = doc.getDocumentElement();
         String cs = root.getAttribute(ResultSetWrapper.COUNT);
         if ( cs != null && cs.length() > 0 )
            count = Integer.parseInt(cs);
         NodeList nl = root.getChildNodes();
         List nodes = new ArrayList(nl.getLength());
         for ( int i = 0; i < nl.getLength(); i++ ) {
            Node n = nl.item(i);
            if ( n.getNodeType() == Node.ELEMENT_NODE )
               nodes.add(n);
         }
         iterator = nodes.iterator();
      }
      catch ( Exception e ) {
         e.printStackTrace(System.err);
      }
   }

   public CollectionClient getCollection() throws dbXMLException {
      return col;
   }

   public String getQueryStyle() throws dbXMLException {
      return style;
   }

   public String getQueryString() throws dbXMLException {
      return query;
   }

   public boolean next() throws dbXMLException {
      checkClosed();
      if ( iterator.hasNext() ) {
         elem = (Element)iterator.next();
         resCol = elem.getAttribute(ResultSetWrapper.COL);
         resKey = elem.getAttribute(ResultSetWrapper.KEY);
         return true;
      }
      else {
         elem = null;
         return false;
      }
   }

   public void close() throws dbXMLException {
      iterator = null;
   }

   private void checkClosed() throws dbXMLException {
      if ( iterator == null )
         throw new dbXMLException("ResultSet is closed");
   }

   public int getCount() throws dbXMLException {
      checkClosed();
      return count;
   }

   public Node getResult() throws dbXMLException {
      checkClosed();
      if ( elem != null ) {
         NodeList nl = elem.getChildNodes();
         if ( nl.getLength() == 1 )
            return nl.item(0);
         else {
            DocumentFragment df = doc.createDocumentFragment();
            for ( int i = 0; i < nl.getLength(); i++ )
               df.appendChild(nl.item(i));
            return df;
         }
      }
      else
         return null;
   }

   public String getResultAsText() throws dbXMLException {
      Node n = getResult();
      if ( n != null )
         return TextWriter.toString(n);
      else
         return null;
   }

   public CollectionClient getResultCollection() throws dbXMLException {
      checkClosed();
      if ( resCol != null && resCol.length() > 0 )
         return col.getClient().getCollection(resCol);
      else
         return col;
   }

   public String getResultKey() throws dbXMLException {
      checkClosed();
      return resKey;
   }
}
