001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: JAXPHelper.java,v 1.2 2001/08/03 11:00:34 jstrachan Exp $ 008 */ 009 010package org.dom4j.io; 011 012import javax.xml.parsers.DocumentBuilder; 013import javax.xml.parsers.DocumentBuilderFactory; 014import javax.xml.parsers.SAXParser; 015import javax.xml.parsers.SAXParserFactory; 016 017import org.xml.sax.XMLReader; 018 019/** <code>JAXPHelper</code> contains some helper methods for working with 020 * JAXP. These methods are kept in a seperate class to avoid class loading 021 * issues, such that dom4j can work without JAXP on the CLASSPATH 022 * 023 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 024 * @version $Revision: 1.2 $ 025 */ 026class JAXPHelper { 027 028 /** This method attempts to use JAXP to locate the 029 * SAX2 XMLReader implementation. 030 * This method uses reflection to avoid being dependent directly 031 * on the JAXP classes. 032 */ 033 public static XMLReader createXMLReader(boolean validating, boolean namespaceAware) throws Exception { 034 SAXParserFactory factory = SAXParserFactory.newInstance(); 035 factory.setValidating( validating ); 036 factory.setNamespaceAware( namespaceAware ); 037 SAXParser parser = factory.newSAXParser(); 038 return parser.getXMLReader(); 039 } 040 041 public static org.w3c.dom.Document createDocument(boolean validating, boolean namespaceAware) throws Exception { 042 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 043 factory.setValidating( validating ); 044 factory.setNamespaceAware( namespaceAware ); 045 DocumentBuilder builder = factory.newDocumentBuilder(); 046 return builder.newDocument(); 047 } 048} 049 050 051 052 053/* 054 * Redistribution and use of this software and associated documentation 055 * ("Software"), with or without modification, are permitted provided 056 * that the following conditions are met: 057 * 058 * 1. Redistributions of source code must retain copyright 059 * statements and notices. Redistributions must also contain a 060 * copy of this document. 061 * 062 * 2. Redistributions in binary form must reproduce the 063 * above copyright notice, this list of conditions and the 064 * following disclaimer in the documentation and/or other 065 * materials provided with the distribution. 066 * 067 * 3. The name "DOM4J" must not be used to endorse or promote 068 * products derived from this Software without prior written 069 * permission of MetaStuff, Ltd. For written permission, 070 * please contact dom4j-info@metastuff.com. 071 * 072 * 4. Products derived from this Software may not be called "DOM4J" 073 * nor may "DOM4J" appear in their names without prior written 074 * permission of MetaStuff, Ltd. DOM4J is a registered 075 * trademark of MetaStuff, Ltd. 076 * 077 * 5. Due credit should be given to the DOM4J Project 078 * (http://dom4j.org/). 079 * 080 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 081 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 082 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 083 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 084 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 085 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 086 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 087 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 088 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 089 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 090 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 091 * OF THE POSSIBILITY OF SUCH DAMAGE. 092 * 093 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 094 * 095 * $Id: JAXPHelper.java,v 1.2 2001/08/03 11:00:34 jstrachan Exp $ 096 */