package com.dbxml.db.client.local;

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

import org.w3c.dom.Document;

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ContentClient;
import com.dbxml.db.core.Container;
import com.dbxml.db.core.data.Value;
import com.dbxml.util.dbXMLException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;

/**
 * ContentClientImpl
 */

public class ContentClientImpl implements ContentClient {
   private CollectionClient col;
   private Container container;

   public ContentClientImpl(CollectionClient col, Container container) {
      this.col = col;
      this.container = container;
   }

   public Container getInternalContiner() {
      return container;
   }

   public int getContentType() {
      return container.getContainerType();
   }

   public String getKey() throws dbXMLException {
      return container.getKey().toString();
   }

   public String getCanonicalName() throws dbXMLException {
      return container.getCanonicalName();
   }

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

   public Document getDocument() throws dbXMLException {
      return DTSMHelper.tableToDocument(container.getDocument());
   }

   public String getDocumentAsText() throws dbXMLException {
      return DTSMHelper.tableToText(container.getDocument());
   }

   public byte[] getValue() throws dbXMLException {
      return container.getValue().getData();
   }

   public void setDocument(Document document) throws dbXMLException {
      DocumentTable dt = DTSMHelper.documentToTable(document, container.getCollection().getSymbols());
      container.setDocument(dt);
   }

   public void setDocumentAsText(String document) throws dbXMLException {
      DocumentTable dt = DTSMHelper.textToTable(document, container.getCollection().getSymbols());
      container.setDocument(dt);
   }

   public void setValue(byte[] value) throws dbXMLException {
      container.setValue(new Value(value));
   }
}
