/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.ims;

import org.apache.log4j.Logger;
import org.bodington.logging.LoggingUtils;

import java.net.*;
import java.io.*;

import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.FactoryConfigurationError;  
import javax.xml.parsers.ParserConfigurationException;
 
import javax.xml.parsers.DocumentBuilder;  
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;  

import org.w3c.dom.*;


public class PackageLoader
	{
    
    private static Logger log = Logger.getLogger(PackageLoader.class);
    
    public static Document openDocument( InputStream manifest )
   		{
   		
   		try
   			{
   			Class.forName( "javax.xml.parsers.DocumentBuilderFactory" );
   			}
   		catch ( Throwable th )
   			{
			log.error(th.getMessage(),th);
   			}
   		
   		
   		
   		Document document = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           document = builder.parse( manifest );

        } catch (SAXParseException spe) {
            LoggingUtils.logSAXException(log,spe);
        } catch (SAXException sxe) {
            // Error generated by this application
            // (or a parser-initialization error)
            if (sxe.getException() != null)
                log.error(sxe.getException().getMessage(), sxe.getException() );
            else
                log.error(sxe.getMessage(),  sxe );	       
            
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            log.error( pce.getMessage(), pce );	       
            
        } catch (Exception e) {
            // I/O error
            log.error(e.getMessage(), e);	       
        }

			return document;
    } // main


}
