001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: XMLUtil.java 7521 2005-10-19 02:14:21Z pasmith $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin.xml;
025:
026: import java.io.InputStream;
027: import java.util.jar.JarFile;
028: import java.util.zip.ZipEntry;
029:
030: import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
031: import org.w3c.dom.Document;
032: import org.xml.sax.SAXException;
033:
034: /**
035: * A class which provides various utilities to interact with XML files.
036: * @author Greg Lapouchnian
037: * @author Patrick Smith
038: */
039: public class XMLUtil {
040:
041: /**
042: * Extract a DOM Document from an XML file located within an archive.
043: * @param archiveName the path to the archive to extract from
044: * @param xmlFilePath the path within the archive to extract.
045: * @return a Document representation of the XML file.
046: * @throws Exception if the document does not parse properly.
047: */
048: public static Document extractXML(String archiveName,
049: String xmlFilePath) throws Exception {
050: Document result = null;
051:
052: // Input Stream
053: InputStream raInputStream = null;
054: InputStream jonasRaInputStream = null;
055: //ZipEntry
056: ZipEntry raZipEntry = null;
057: ZipEntry jonasRaZipEntry = null;
058:
059: try {
060: JarFile rarFile = new JarFile(archiveName);
061:
062: //Check the ra entry
063: raZipEntry = rarFile.getEntry(xmlFilePath);
064: if (raZipEntry != null) {
065: //Get the stream
066: raInputStream = rarFile.getInputStream(raZipEntry);
067:
068: result = XMLUtils.newDocument(raInputStream,
069: xmlFilePath, true, true);
070:
071: }
072: } catch (SAXException e) {
073: throw e;
074: }
075:
076: catch (Exception e) {
077: throw e;
078: }
079:
080: return result;
081: }
082:
083: /**
084: * Returns a new Document from the XML file given from the file.
085: * @param file the path to the XML file.
086: * @return a Document representation of the file.
087: */
088: public static Document extractXML(String file) {
089: Document result = null;
090:
091: try {
092: InputStream is = new java.io.FileInputStream(file);
093: result = XMLUtils.newDocument(is, file, false, false);
094: } catch (Exception e) {
095: e.printStackTrace();
096: }
097:
098: return result;
099: }
100: }
|