/*******************************************************************************
$Source: /cvs/repositories/openii3/project/java/source/org/openeai/xml/XmlDocumentReader.java,v $
$Revision: 1.13 $
*******************************************************************************/
/**********************************************************************
This file is part of the OpenEAI Application Foundation or
OpenEAI Message Object API created by Tod Jackson
(tod@openeai.org) and Steve Wheat (steve@openeai.org) at
the University of Illinois Urbana-Champaign.
Copyright (C) 2002 The OpenEAI Software Foundation
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 (at your option) 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
For specific licensing details and examples of how this software
can be used to build commercial integration software or to implement
integrations for your enterprise, visit http://www.OpenEai.org/licensing.
*/
package org.openeai.xml;
import org.openeai.*;
import java.io.*;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;
/**
* This class is used to create a JDOM Document object from the contents of an
* XML file that can be stored in a file (on a web server or local file system)
* or passed in as an in-memory representation of that file (Reader, InputStream).
* <P>
* @author Tod Jackson (tod@openeai.org)
* @author Steve Wheat (steve@openeai.org)
* @version 3.0 - 28 January 2003
*/
public class XmlDocumentReader extends OpenEaiObject {
/**
* Constructor
*/
public XmlDocumentReader() {
}
/**
* Creates a JDOM Document from the specified XML file name.
*<P>
* Example 1: http://www.somexmlserver.com/xml/AnXmlFile.xml
*<P>
* Example 2: file://localhost/xmlfiles/AnXmlFile.xml
*<P>
* Example 3: c:\xmlfiles\AnXmlFile.xml
*<P>
* Example 4: /opt/xmlfiles/AnXmlFile.xml
*<P>
* @return org.jdom.Document
* @param fileName String the file name (can be in either URL format or local file system format)
* @param validate boolean flag indicating whether or not this method should also validate the contents
* of the document from an XML perspective (true=validate, false=don't validate)
*
*/
public final Document initializeDocument(String fileName, boolean validate)
throws XmlDocumentReaderException {
if (fileName == null || fileName.length() == 0) {
logger.debug("XmlDocumentReader: Null Document name passed in, can't initialize.");
return null;
}
Document doc = null;
try {
SAXBuilder builder = new SAXBuilder(false);
if (validate == false) {
logger.debug("Setting the EntityResolver to IgnoreDTDResolver.");
builder.setEntityResolver(new IgnoreDTDResolver());
}
else {
logger.debug("Using the default EntityResolver.");
}
if (fileName.toLowerCase().indexOf("http") == 0 || fileName.toLowerCase().indexOf("file") == 0) {
// build from url
logger.debug("Building document from " + fileName);
java.net.URL url = null;
try {
url = new java.net.URL(fileName);
}
catch (Exception e) {
logger.fatal(e.getMessage(), e);
throw new XmlDocumentReaderException(e.getMessage(), e);
}
// logger.info("Started - SAXBuilder.build for URL " + fileName);
doc = builder.build(url);
// logger.info("Ended - SAXBuilder.build for URL " + fileName);
}
else {
// build from file
// logger.info("Started - SAXBuilder.build for file " + fileName);
doc = builder.build(new File(fileName));
// logger.info("Ended - SAXBuilder.build for file " + fileName);
}
logger.debug("XmlDocumentReader:" + fileName + ":successfully read!");
if (validate) {
XmlValidator xmlValidator = new XmlValidator();
if (xmlValidator.isValid(doc) == false) {
throw new XmlDocumentReaderException("Document is not valid!");
}
}
else {
logger.debug("Validation is turned off.");
}
}
catch (JDOMException e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
catch (Exception e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
return doc;
}
/**
* Creates a JDOM Document from the supplied InputStream.
*<P>
* @return org.jdom.Document
* @param is InputStream contents of the XML file that has been stored in the InputStream.
* @param validate boolean flag indicating whether or not this method should also validate the contents
* of the document from an XML perspective (true=validate, false=don't validate)
*
*/
public final Document initializeDocument(InputStream is, boolean validate)
throws XmlDocumentReaderException {
if (is == null) {
logger.debug("XmlDocumentReader: Null Document name passed in, can't initialize.");
return null;
}
Document doc = null;
try {
SAXBuilder builder = new SAXBuilder(false);
logger.debug("Building document from Reader");
if (validate == false) {
logger.debug("Setting the EntityResolver to IgnoreDTDResolver.");
builder.setEntityResolver(new IgnoreDTDResolver());
}
else {
logger.debug("Using the default EntityResolver.");
}
doc = builder.build(is);
logger.debug("XmlDocumentReader: document successfully read!");
if (validate) {
XmlValidator xmlValidator = new XmlValidator();
if (xmlValidator.isValid(doc) == false) {
throw new XmlDocumentReaderException("Document is not valid!");
}
}
else {
logger.debug("Validation is turned off.");
}
}
catch (JDOMException e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
catch (Exception e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
return doc;
}
/**
* Creates a JDOM Document from the supplied Reader.
*<P>
* @return org.jdom.Document
* @param docAsReader Reader contents of the XML file that has been stored in the Reader.
* @param validate boolean flag indicating whether or not this method should also validate the contents
* of the document from an XML perspective (true=validate, false=don't validate)
*
*/
public final Document initializeDocument(Reader docAsReader, boolean validate)
throws XmlDocumentReaderException {
if (docAsReader == null) {
logger.debug("XmlDocumentReader: Null Document name passed in, can't initialize.");
return null;
}
Document doc = null;
try {
SAXBuilder builder = new SAXBuilder(false);
logger.debug("Building document from Reader");
if (validate == false) {
logger.debug("Setting the EntityResolver to IgnoreDTDResolver.");
builder.setEntityResolver(new IgnoreDTDResolver());
}
else {
logger.debug("Using the default EntityResolver.");
}
doc = builder.build(docAsReader);
logger.debug("XmlDocumentReader: document successfully read!");
if (validate) {
XmlValidator xmlValidator = new XmlValidator();
if (xmlValidator.isValid(doc) == false) {
throw new XmlDocumentReaderException("Document is not valid!");
}
}
else {
logger.debug("Validation is turned off.");
}
}
catch (JDOMException e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
catch (Exception e) {
throw new XmlDocumentReaderException(e.getMessage(), e);
}
return doc;
}
}