package com.dbxml.db.embedded;

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

import com.dbxml.db.common.fulltext.FullTextQueryResolver;
import com.dbxml.db.common.security.NoSecurityManager;
import com.dbxml.db.common.xpath.XPathQueryResolver;
import com.dbxml.db.common.xslt.XSLTQueryResolver;
import com.dbxml.db.common.xupdate.XUpdateQueryResolver;
import com.dbxml.util.Configuration;
import com.dbxml.xml.dom.DOMHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Document;

/**
 * EmbeddedConfiguration
 */

public final class EmbeddedConfiguration {
   private static final String CONFIG = "config";
   private static final String DBROOT = "dbroot";
   private static final String QUERYENGINE = "queryengine";
   private static final String RESOLVER = "resolver";
   private static final String SECURITY = "security";
   private static final String NAME = "name";
   private static final String CLASS = "class";

   private static final Class DEFAULT_SECURITYMANAGER = NoSecurityManager.class;
   private static final List DEFAULT_RESOLVERS = new ArrayList();
   static {
      DEFAULT_RESOLVERS.add(XPathQueryResolver.class);
      DEFAULT_RESOLVERS.add(XSLTQueryResolver.class);
      DEFAULT_RESOLVERS.add(XUpdateQueryResolver.class);
      DEFAULT_RESOLVERS.add(FullTextQueryResolver.class);
   }

   private String dbName = null;
   private File dbRoot = null;
   private Class security = null;
   private List queryResolvers = null;

   public EmbeddedConfiguration() {
   }

   public EmbeddedConfiguration(String dbName) {
      this.dbName = dbName;
   }

   public EmbeddedConfiguration(String dbName, File dbRoot) {
      this.dbName = dbName;
      this.dbRoot = dbRoot;
   }

   public void setDatabaseName(String dbName) {
      this.dbName = dbName;
   }

   public String getDatabaseName() {
      return dbName;
   }

   public void setDatabaseRoot(File dbRoot) {
      this.dbRoot = dbRoot;
   }

   public File getDatabaseRoot() {
      return dbRoot;
   }

   public void setSecurityManager(Class security) {
      this.security = security;
   }

   public Class getSecurityManager() {
      return security;
   }

   public void addQueryResolver(Class resolver) {
      if ( queryResolvers == null )
         queryResolvers = new ArrayList();

      queryResolvers.add(resolver);
   }

   public Configuration getConfig() {
      Document doc = DOMHelper.newDocument();
      doc.appendChild(doc.createElement(CONFIG));
      Configuration config = new Configuration(doc);

      if ( dbName != null )
         config.setAttribute(NAME, dbName);
      else
         config.setAttribute(NAME, "db");

      if ( dbRoot != null )
         config.setAttribute(DBROOT, dbRoot.getPath());
      else
         config.setAttribute(DBROOT, "./db");

      Configuration securityCfg = config.add(SECURITY);
      if ( security != null )
         securityCfg.setAttribute(CLASS, security.getName());
      else
         securityCfg.setAttribute(CLASS, DEFAULT_SECURITYMANAGER.getName());

      Configuration queryEngineCfg = config.add(QUERYENGINE);
      Iterator iter;
      if ( queryResolvers != null )
         iter = queryResolvers.iterator();
      else
         iter = DEFAULT_RESOLVERS.iterator();

      while ( iter.hasNext() ) {
         Class c = (Class)iter.next();
         Configuration resolverCfg = queryEngineCfg.add(RESOLVER);
         resolverCfg.setAttribute(CLASS, c.getName());
      }

      return config;
   }
}
