/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.webapp.jonasadmin.xml;

import java.io.InputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * A class which provides various utilities to interact with XML files.
 * @author Greg Lapouchnian
 * @author Patrick Smith
 */
public class XMLUtil {

    /**
     * Extract a DOM Document from an XML file located within an archive.
     * @param archiveName the path to the archive to extract from
     * @param xmlFilePath the path within the archive to extract.
     * @return a Document representation of the XML file.
     * @throws Exception if the document does not parse properly.
     */
    public static Document extractXML(String archiveName, String xmlFilePath)
            throws Exception {
        Document result = null;

        //      Input Stream
        InputStream raInputStream = null;
        InputStream jonasRaInputStream = null;
        //ZipEntry
        ZipEntry raZipEntry = null;
        ZipEntry jonasRaZipEntry = null;

        try {
            JarFile rarFile = new JarFile(archiveName);

            //Check the ra entry
            raZipEntry = rarFile.getEntry(xmlFilePath);
            if (raZipEntry != null) {
                //Get the stream
                raInputStream = rarFile.getInputStream(raZipEntry);

                result = XMLUtils.newDocument(raInputStream, xmlFilePath, true,
                        true);

            }
        } catch (SAXException e) {
            throw e;
        }

        catch (Exception e) {
            throw e;
        }

        return result;
    }

    /**
     * Returns a new Document from the XML file given from the file.
     * @param file the path to the XML file.
     * @return a Document representation of the file.
     */
    public static Document extractXML(String file) {
        Document result = null;

        try {
            InputStream is = new java.io.FileInputStream(file);
            result = XMLUtils.newDocument(is, file, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
