package org.dbxml.xml.dom;

/*
 * 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: DOMImplementationImpl.java,v 1.8 2001/09/04 02:46:29 tom Exp $
 */

import org.w3c.dom.*;

/**
 * DOMImplementationImpl
 */

public final class DOMImplementationImpl implements DOMImplementation {
   private static final String[][] FEATURES = {
      {"XML", "1.0"},
      {"XML", "2.0"},
      {"Traversal", "2.0"},
      {"dbXML", "1.0"},     // dbXML DBNode and DBDocument
      {"Compressed", "1.0"} // CompressedNode and CompressedDocument
   };

   private static DOMImplementation domImpl = new DOMImplementationImpl();
   
   private DOMImplementationImpl() {
   }

   public static DOMImplementation getInstance() {
      return domImpl;
   }
   
   public static boolean HasFeature(String feature, String version) {
      for ( int i = 0; i < FEATURES.length; i++ )
         if ( FEATURES[i][0].equals(feature)
           && FEATURES[i][1].equals(version) )
            return true;
      return false;
   }

   /**
    * Test if the DOM implementation implements a specific feature.
    * @param feature The package name of the feature to test. In Level 1, the
    *   legal values are "HTML" and "XML" (case-insensitive).
    * @param version This is the version number of the package name to test.
    *   In Level 1, this is the string "1.0". If the version is not specified,
    *   supporting any version of the  feature will cause the method to return
    *   <code>true</code>.
    * @return <code>true</code> if the feature is implemented in the specified
    *   version, <code>false</code> otherwise.
    */
   public boolean hasFeature(String feature, String version) {
      return HasFeature(feature, version);
   }

    /**
     * Creates an empty <code>DocumentType</code> node. Entity declarations
     * and notations are not made available. Entity reference expansions and
     * default attribute additions do not occur. It is expected that a
     * future version of the DOM will provide a way for populating a
     * <code>DocumentType</code>.
     * <br>HTML-only DOM implementations do not need to implement this method.
     * @param qualifiedNameThe qualified name of the document type to be
     *   created.
     * @param publicIdThe external subset public identifier.
     * @param systemIdThe external subset system identifier.
     * @return A new <code>DocumentType</code> node with
     *   <code>Node.ownerDocument</code> set to <code>null</code>.
     * @exception DOMException
     *   INVALID_CHARACTER_ERR: Raised if the specified qualified name
     *   contains an illegal character.
     *   <br>NAMESPACE_ERR: Raised if the <code>qualifiedName</code> is
     *   malformed.
     * @since DOM Level 2
     */
   public DocumentType createDocumentType(String qualifiedName, String publicId, String systemId) throws DOMException {
      return new DocumentTypeImpl();
   }

    /**
     * Creates an XML <code>Document</code> object of the specified type with
     * its document element. HTML-only DOM implementations do not need to
     * implement this method.
     * @param namespaceURIThe namespace URI of the document element to create.
     * @param qualifiedNameThe qualified name of the document element to be
     *   created.
     * @param doctypeThe type of document to be created or <code>null</code>.
     *   When <code>doctype</code> is not <code>null</code>, its
     *   <code>Node.ownerDocument</code> attribute is set to the document
     *   being created.
     * @return A new <code>Document</code> object.
     * @exception DOMException
     *   INVALID_CHARACTER_ERR: Raised if the specified qualified name
     *   contains an illegal character.
     *   <br>NAMESPACE_ERR: Raised if the <code>qualifiedName</code> is
     *   malformed, if the <code>qualifiedName</code> has a prefix and the
     *   <code>namespaceURI</code> is <code>null</code>, or if the
     *   <code>qualifiedName</code> has a prefix that is "xml" and the
     *   <code>namespaceURI</code> is different from "
     *   http://www.w3.org/XML/1998/namespace" .
     *   <br>WRONG_DOCUMENT_ERR: Raised if <code>doctype</code> has already
     *   been used with a different document or was created from a different
     *   implementation.
     * @since DOM Level 2
     */
   public Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype) throws DOMException {
      DocumentImpl doc = new DocumentImpl();
      Element elem = doc.createElement(qualifiedName);
      doc.appendChild(elem);
      if ( namespaceURI != null ) {
         if ( qualifiedName.indexOf(":") != -1 )
            elem.setAttribute("xmlns:"+qualifiedName.substring(0, qualifiedName.indexOf(":")), namespaceURI);
         else
            elem.setAttribute("xmlns", namespaceURI);
      }
      if ( doctype != null ) {
         if ( doctype.getOwnerDocument() != null
           || !(doctype instanceof DocumentTypeImpl) )
            throw NodeImpl.EX_WRONG_DOCUMENT;
         doc.setDoctype(doctype);
      }
      return doc;
   }
}

