package com.dbxml.xml.dom;

/*
 * 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: DOMHelper.java,v 1.4 2004/02/12 00:17:58 bradford Exp $
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * DOMHelper
 */

public final class DOMHelper {
   private static DocumentBuilderFactory dbf;
   private static ThreadLocal db;

   static {
      try {
         dbf = DocumentBuilderFactory.newInstance();
         dbf.setExpandEntityReferences(true);
         dbf.setIgnoringComments(false);
         dbf.setIgnoringElementContentWhitespace(false);
         dbf.setNamespaceAware(true);
         dbf.setValidating(false);

         db = new ThreadLocal() {
            protected synchronized Object initialValue() {
               try {
                  return dbf.newDocumentBuilder();
               }
               catch ( ParserConfigurationException e ) {
                  throw new RuntimeException(e);
               }
            }
         };
      }
      catch ( Exception e ) {
         System.err.println("FATAL ERROR: Could not create default Document Builder");
         e.printStackTrace(System.err);
         System.exit(1);
      }
   }

   private DOMHelper() {
   }

   private static DocumentBuilder getDocumentBuilder() {
      return (DocumentBuilder)db.get();
   }

   public static DOMImplementation getDOMImplementation() {
      return getDocumentBuilder().getDOMImplementation();
   }

   public static boolean isNamespaceAware() {
      return getDocumentBuilder().isNamespaceAware();
   }

   public static boolean isValidating() {
      return getDocumentBuilder().isValidating();
   }

   public static Document newDocument() {
      return getDocumentBuilder().newDocument();
   }

   public static Document parse(InputSource is) throws SAXException, IOException {
      return getDocumentBuilder().parse(is);
   }

   public static Document parse(File f) throws SAXException, IOException {
      return getDocumentBuilder().parse(f);
   }

   public static Document parse(InputStream is) throws SAXException, IOException {
      return getDocumentBuilder().parse(is);
   }

   public static Document parse(InputStream is, String systemId) throws SAXException, IOException {
      return getDocumentBuilder().parse(is, systemId);
   }

   public static Document parseText(String text) throws SAXException, IOException {
      StringReader sr = new StringReader(text);
      return getDocumentBuilder().parse(new InputSource(sr));
   }

   public static Document parseURI(String uri) throws SAXException, IOException {
      return getDocumentBuilder().parse(uri);
   }
}
