package org.dbxml.core;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: DocumentCache.java,v 1.27 2001/08/05 07:22:36 tom Exp $
 */
 
import java.util.*;

import org.dbxml.server.util.*;
import org.dbxml.core.data.*;
import org.dbxml.xml.*;
import org.dbxml.xml.dom.*;
import org.w3c.dom.*;

/**
 * DocumentCache implements a simple Document caching system for
 * Collections.
 */

public final class DocumentCache {
   private Map table = new WeakHashMap();

   public Document getDocument(Collection col, Key key) {
      Object v = table.get(new CacheKey(col, key));
      Document doc = null;
      if ( v instanceof Document )
         doc = (Document)v;
      else if ( v instanceof byte[] ) {
         try {
            SymbolTable s = col.getSymbols();
            NodeSource ns = new NodeSource(col, key);
            doc = new DocumentImpl((byte[])v, s, ns);
         }
         catch ( Exception e ) {
         }
      }
      return doc;
   }
   
   public void putDocument(Collection col, Key key, byte[] bytes) {
      CacheKey ckey = new CacheKey(col, key);
      table.put(ckey, bytes);
   }
   
   public void putDocument(Collection col, Key key, Document doc) {
      CacheKey ckey = new CacheKey(col, key);
      table.put(ckey, doc);
   }

   public void removeDocument(Collection col, Key key) {
      table.remove(new CacheKey(col, key));
   }

   public static int getCacheControl(Document doc) {
      String cache = DBDocument.CACHE;
      NodeList childNodes = doc.getChildNodes();
      int size = childNodes.getLength();
      for ( int i = 0; i < size; i++ ) {
         Node n = childNodes.item(i);
         if ( n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
           && n.getNodeName().equals(DBDocument.CACHE_CONTROL) ) {
            cache = n.getNodeValue().trim();
            break;
         }
      }

      if ( cache != null ) {
         if ( cache.equals(DBDocument.CACHE) )
            return -1;
         else if ( cache.equals(DBDocument.NOCACHE) )
            return 0;
         else
            return Integer.parseInt(cache);
      }
      else
         return -1;
   }


   /**
    * CacheKey
    */
   
   private class CacheKey {
      private Collection col;
      private String strVal;
      private Key key;

      public CacheKey(Collection col, Key key) {
         this.col = col;
         this.key = key;
      }

      public Collection getCollection() {
         return col;
      }

      public Key getKey() {
         return key;
      }

      public String toString() {
         if ( strVal == null )
            strVal = col.getCanonicalDocumentName(key);
         return strVal;
      }

      public int hashCode() {
         if ( strVal == null )
            strVal = col.getCanonicalDocumentName(key);
         return strVal.hashCode();
      }

      public boolean equals(Object o) {
         CacheKey comp = (CacheKey)o;
         if ( col != comp.col )
            return false;
         return key.equals(comp.key );
      }
   }
}

