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

import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.sdk.modules.SetContentHandler;

/**
 * XMLResourceImpl
 */

public final class XMLResourceImpl implements XMLResource {
   public static final String TYPE = "XMLResource";

	private SAXParserFactory spf;
   private CollectionImpl parent;
   private String docID;
   private String id;
   private String xml;

   public XMLResourceImpl(CollectionImpl parent, String docID, String id, String xml) {
      this.parent = parent;
      this.docID = docID;
      this.id = id;
      this.xml = xml;
   }

   public XMLResourceImpl(CollectionImpl parent) {
      this.parent = parent;
   }

   public String getDocumentId() throws XMLDBException {
      return docID;
   }

   public Collection getParentCollection() throws XMLDBException {
      return parent;
   }

   public Node getContentAsDOM() throws XMLDBException {
      try {
         return DOMHelper.parseText(xml);
      }
      catch ( Exception e ) {
         throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage());
      }
   }

   public String getId() throws XMLDBException {
      return id;
   }

   public void setContentAsDOM(Node content) throws XMLDBException {
      xml = TextWriter.toString(content);
   }

   public String getResourceType() throws XMLDBException {
      return TYPE;
   }

	private void checkSAXFactory() throws SAXNotRecognizedException, SAXNotSupportedException {
		if ( spf == null ) {
			try {
				spf = SAXParserFactory.newInstance();
				spf.setNamespaceAware(true);
				spf.setValidating(false);
				spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
			}
			catch ( ParserConfigurationException e ) {
				throw new SAXNotSupportedException(e.getMessage());
			}
		}
	}
	
	public void setSAXFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
		checkSAXFactory();
		try {
			spf.setFeature(name, value);
		}
		catch ( ParserConfigurationException e ) {
			throw new SAXNotRecognizedException(e.getMessage());
		}
	}
	
	public boolean getSAXFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
		checkSAXFactory();
		try {
			return spf.getFeature(name);
		}
		catch ( ParserConfigurationException e ) {
			throw new SAXNotRecognizedException(e.getMessage());
		}
	}
	
	public void getContentAsSAX(ContentHandler handler) throws XMLDBException {
		if ( xml == null )
			throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No content has been set");
		
		try {
			checkSAXFactory();
			SAXParser sp = spf.newSAXParser();
			
			XMLReader xr = sp.getXMLReader();
			xr.setContentHandler(handler);
			
			InputSource is = new InputSource(new StringReader(xml));
			
			xr.parse(is);
		}
		catch ( Exception e ) {
			throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
		}
   }

   public ContentHandler setContentAsSAX() throws XMLDBException {
		return new SetContentHandler(this);
   }

   public Object getContent() throws XMLDBException {
      return xml;
   }
	
   public void setContent(Object value) throws XMLDBException {
      if ( value instanceof Node )
         setContentAsDOM((Node)value);
      else if ( value instanceof String )
         xml = (String)value;
      else if ( value != null )
         throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE, "Setting content as '" + value.getClass().getName() + "' is not supported");
      else
         throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE, "Setting content as null is not supported");
   }
}
