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: SAXValidator.java,v 1.3 2001/08/20 14:57:20 jstrachan Exp $ 008 */ 009 010package org.dom4j.io; 011 012import java.io.IOException; 013 014import org.dom4j.Document; 015 016import org.xml.sax.ContentHandler; 017import org.xml.sax.ErrorHandler; 018import org.xml.sax.SAXException; 019import org.xml.sax.XMLReader; 020import org.xml.sax.helpers.DefaultHandler; 021 022/** <p><code>SAXValidator</code> validates an XML document by 023 * writing the document to a text buffer and parsing it with a validating 024 * SAX parser. 025 * This could be implemented much more efficiently by validating against the 026 * dom4j object model directly but at least allows the reuse of existing 027 * SAX based validating parsers.</p> 028 * 029 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 030 * @version $Revision: 1.3 $ 031 */ 032public class SAXValidator { 033 034 /** <code>XMLReader</code> used to parse the SAX events */ 035 private XMLReader xmlReader; 036 037 /** ErrorHandler class to use */ 038 private ErrorHandler errorHandler; 039 040 041 042 public SAXValidator() { 043 } 044 045 public SAXValidator(XMLReader xmlReader) { 046 this.xmlReader = xmlReader; 047 } 048 049 050 051 /** Validates the given <code>Document</code> by writing it to a 052 * validating SAX Parser. 053 * 054 * @param document is the Document to validate 055 * @throw SAXException if a validation error occurs 056 */ 057 public void validate(Document document) throws SAXException { 058 if (document != null) { 059 XMLReader xmlReader = getXMLReader(); 060 if ( errorHandler != null ) { 061 xmlReader.setErrorHandler( errorHandler ); 062 } 063 try { 064 xmlReader.parse( new DocumentInputSource( document ) ); 065 } 066 catch (IOException e) { 067 throw new RuntimeException( 068 "Caught and exception that should never happen: " + e 069 ); 070 } 071 } 072 } 073 074 075 // Properties 076 //------------------------------------------------------------------------- 077 078 /** @return the <code>XMLReader</code> used to parse SAX events 079 */ 080 public XMLReader getXMLReader() throws SAXException { 081 if (xmlReader == null) { 082 xmlReader = createXMLReader(); 083 configureReader(); 084 } 085 return xmlReader; 086 } 087 088 /** Sets the <code>XMLReader</code> used to parse SAX events 089 * 090 * @param xmlReader is the <code>XMLReader</code> to parse SAX events 091 */ 092 public void setXMLReader(XMLReader xmlReader) throws SAXException { 093 this.xmlReader = xmlReader; 094 configureReader(); 095 } 096 097 /** @return the <code>ErrorHandler</code> used by SAX 098 */ 099 public ErrorHandler getErrorHandler() { 100 return errorHandler; 101 } 102 103 /** Sets the <code>ErrorHandler</code> used by the SAX 104 * <code>XMLReader</code>. 105 * 106 * @param errorHandler is the <code>ErrorHandler</code> used by SAX 107 */ 108 public void setErrorHandler(ErrorHandler errorHandler) { 109 this.errorHandler = errorHandler; 110 } 111 112 // Implementation methods 113 //------------------------------------------------------------------------- 114 /** Factory Method to allow alternate methods of 115 * creating and configuring XMLReader objects 116 */ 117 protected XMLReader createXMLReader() throws SAXException { 118 return SAXHelper.createXMLReader( true ); 119 } 120 121 /** Configures the XMLReader before use */ 122 protected void configureReader() throws SAXException { 123 ContentHandler handler = xmlReader.getContentHandler(); 124 if ( handler == null ) { 125 xmlReader.setContentHandler( new DefaultHandler() ); 126 } 127 128 // configure validation support 129 xmlReader.setFeature( 130 "http://xml.org/sax/features/validation", 131 true 132 ); 133 134 // configure namespace support 135 xmlReader.setFeature( 136 "http://xml.org/sax/features/namespaces", 137 true 138 ); 139 xmlReader.setFeature( 140 "http://xml.org/sax/features/namespace-prefixes", 141 false 142 ); 143 } 144 145} 146 147 148 149 150/* 151 * Redistribution and use of this software and associated documentation 152 * ("Software"), with or without modification, are permitted provided 153 * that the following conditions are met: 154 * 155 * 1. Redistributions of source code must retain copyright 156 * statements and notices. Redistributions must also contain a 157 * copy of this document. 158 * 159 * 2. Redistributions in binary form must reproduce the 160 * above copyright notice, this list of conditions and the 161 * following disclaimer in the documentation and/or other 162 * materials provided with the distribution. 163 * 164 * 3. The name "DOM4J" must not be used to endorse or promote 165 * products derived from this Software without prior written 166 * permission of MetaStuff, Ltd. For written permission, 167 * please contact dom4j-info@metastuff.com. 168 * 169 * 4. Products derived from this Software may not be called "DOM4J" 170 * nor may "DOM4J" appear in their names without prior written 171 * permission of MetaStuff, Ltd. DOM4J is a registered 172 * trademark of MetaStuff, Ltd. 173 * 174 * 5. Due credit should be given to the DOM4J Project 175 * (http://dom4j.org/). 176 * 177 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 178 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 179 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 180 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 181 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 182 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 183 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 184 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 185 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 186 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 187 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 188 * OF THE POSSIBILITY OF SUCH DAMAGE. 189 * 190 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 191 * 192 * $Id: SAXValidator.java,v 1.3 2001/08/20 14:57:20 jstrachan Exp $ 193 */