package com.dbxml.xml.sax;

/*
 * 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: SAXHelper.java,v 1.3 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.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

/**
 * SAXHelper
 */

public final class SAXHelper {
   private static SAXParserFactory spf;

   static {
      try {
         spf = SAXParserFactory.newInstance();
         spf.setNamespaceAware(true);
         spf.setValidating(false);
         spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
      }
      catch ( Exception e ) {
         System.err.println("FATAL ERROR: Could not create default SAX Parser Factory");
         e.printStackTrace(System.err);
         System.exit(1);
      }
   }

   private static SAXParser createSAXParser(DefaultHandler dh) throws SAXException {
      try {
         SAXParser sp = spf.newSAXParser();
         if ( dh instanceof LexicalHandler )
            sp.setProperty("http://xml.org/sax/properties/lexical-handler", dh);

         return sp;
      }
      catch ( ParserConfigurationException e ) {
         throw new SAXException(e);
      }
   }

   public static void parse(InputStream is, DefaultHandler dh) throws SAXException, IOException {
      SAXParser sp = createSAXParser(dh);
      sp.parse(is, dh);
   }

   public static void parseText(String text, DefaultHandler dh) throws SAXException, IOException {
      StringReader sr = new StringReader(text);
      parse(new InputSource(sr), dh);
   }

   public static void parse(InputStream is, DefaultHandler dh, String systemId) throws SAXException, IOException {
      SAXParser sp = createSAXParser(dh);
      sp.parse(is, dh, systemId);
   }

   public static void parseURI(String uri, DefaultHandler dh) throws SAXException, IOException {
      SAXParser sp = createSAXParser(dh);
      sp.parse(uri, dh);
   }

   public static void parse(File f, DefaultHandler dh) throws SAXException, IOException {
      SAXParser sp = createSAXParser(dh);
      sp.parse(f, dh);
   }

   public static void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException {
      SAXParser sp = createSAXParser(dh);
      sp.parse(is, dh);
   }
}
