package com.dbxml.db.client.xmldb;

/*
 * 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: ResourceSetImpl.java,v 1.2 2004/02/12 00:17:56 bradford Exp $
 */

import com.dbxml.db.client.ResultSetClient;
import com.dbxml.util.dbXMLException;
import java.util.ArrayList;
import java.util.List;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

/**
 * ResourceSetImpl
 */

public final class ResourceSetImpl implements ResourceSet {
   private CollectionImpl col;
   private List resources = new ArrayList();

   public ResourceSetImpl(CollectionImpl col, ResultSetClient rs) throws XMLDBException {
      this.col = col;
      try {
         while ( rs.next() ) {
            String docID = rs.getResultKey();
            String id = Integer.toString(new Object().hashCode());
            String xml = rs.getResultAsText();
            Resource resource = new XMLResourceImpl(col, docID, id, xml);
            resources.add(resource);
         }
         rs.close();
      }
      catch ( dbXMLException e ) {
         throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
      }
   }

   public Resource getResource(long index) throws XMLDBException {
      return (Resource)resources.get((int)index);
   }

   public void addResource(Resource resource) throws XMLDBException {
      resources.add(resource);
   }

   public void removeResource(long index) throws XMLDBException {
      resources.remove((int)index);
   }

   public ResourceIterator getIterator() throws XMLDBException {
      return new ResourceIteratorImpl(resources);
   }

   public Resource getMembersAsResource() throws XMLDBException {
      throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED, "getMembersAsResource()'s resulting schema undefined");
   }

   public long getSize() throws XMLDBException {
      return resources.size();
   }

   public void clear() throws XMLDBException {
      resources.clear();
   }
}
