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: DOMParser.java,v 1.11 2001/08/16 17:38:49 tom Exp $
 */

import org.dbxml.core.*;
import org.dbxml.core.data.*;
import org.dbxml.xml.*;
import org.dbxml.server.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;

/**
 * DOMParser is a custom override of the Xerces DOM Parser that produces a
 * dbXML DOM.
 */

public final class DOMParser extends org.apache.xerces.parsers.DOMParser {

   public DOMParser() throws dbXMLException {
      initHandlers(false, this, this);
      init();
      try {
         setDocumentClassName("org.dbxml.xml.dom.DocumentImpl");
         setIncludeIgnorableWhitespace(true);
         setDeferNodeExpansion(false);
         setCreateEntityReferenceNodes(false);
         setLoadDTDGrammar(false);
         setValidation(false);
         setNamespaces(true);
      }
      catch ( Exception e ) {
         throw new dbXMLException("Error creating DOM Parser", e);
      }
   }

   /**
    * Constructor that takes a SAX InputSource to immediately parse the
    * Document.
    *
    * @param source SAX InputSource
    */
   public DOMParser(InputSource source) throws dbXMLException {
      this();
      try {
         parse(source);
      }
      catch ( Exception e ) {
         throw new dbXMLException("Error parsing Document", e);
      }
   }

   /**
    * Constructor that takes an InputStream to immediately parse the Document.
    *
    * @param input The InputStream
    */
   public DOMParser(InputStream input) throws dbXMLException {
      this(new InputSource(input));
   }

   /**
    * Constructor that takes a Reader to immediately parse the Document.
    *
    * @param input The Reader
    */
   public DOMParser(Reader input) throws dbXMLException {
      this(new InputSource(input));
   }

   /**
    * Constructor that takes a String and immediately parses it as a Document.
    *
    * @param xml XML Document as a String
    */
   public DOMParser(String xml) throws dbXMLException {
      this(new StringReader(xml));
   }

   /**
    * Constructor that takes a byte array and immediately parses it as a
    * Document.
    *
    * @param xml The XML as a byte array
    */
   public DOMParser(byte[] xml) throws dbXMLException {
      this(new ByteArrayInputStream(xml));
   }

   /**
    * Constructor that takes a dbXML Value and immediately parses it as a
    * Document.
    *
    * @param value The XML as a Value
    */
   public DOMParser(Value value) throws dbXMLException {
      this(value.getData());
   }

   /**
    * Parses an InputSource into a DOM Document.
    *
    * @param is The InputSource
    * @return A DOM Document
    */
   public static Document toDocument(InputSource is) throws dbXMLException {
      return new DOMParser(is).getDocument();
   }

   /**
    * Parses an InputStream into a DOM Document.
    *
    * @param is The InputStream
    * @return A DOM Document
    */
   public static Document toDocument(InputStream is) throws dbXMLException {
      if ( is instanceof BufferedInputStream )
         return new DOMParser(is).getDocument();
      else
         return new DOMParser(new BufferedInputStream(is, 4096)).getDocument();
   }

   /**
    * Parses a Reader into a DOM Document.
    *
    * @param input The Reader
    * @return A DOM Document
    */
   public static Document toDocument(Reader input) throws dbXMLException {
      if ( input instanceof BufferedReader )
         return new DOMParser(input).getDocument();
      else
         return new DOMParser(new BufferedReader(input, 4096)).getDocument();
   }

   /**
    * Parses a String into a DOM Document.
    *
    * @param xml The String
    * @return A DOM Document
    */
   public static Document toDocument(String xml) throws dbXMLException {
      return new DOMParser(xml).getDocument();
   }

   /**
    * Parses a byte array into a DOM Document.
    *
    * @param xml The byte array
    * @return A DOM Document
    */
   public static Document toDocument(byte[] xml) throws dbXMLException {
      return new DOMParser(xml).getDocument();
   }

   /**
    * Parses a dbXML Value into a DOM Document.
    *
    * @param value The Value
    * @return A DOM Document
    */
   public static Document toDocument(Value value) throws dbXMLException {
      return new DOMParser(value).getDocument();
   }
}
