|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SAXValidator.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 3 | * | |
| 4 | * This software is open source. | |
| 5 | * See the bottom of this file for the licence. | |
| 6 | * | |
| 7 | * $Id: SAXValidator.java,v 1.8 2004/06/25 12:34:47 maartenc Exp $ | |
| 8 | */ | |
| 9 | ||
| 10 | package org.dom4j.io; | |
| 11 | ||
| 12 | import java.io.IOException; | |
| 13 | ||
| 14 | import org.dom4j.Document; | |
| 15 | import org.xml.sax.ContentHandler; | |
| 16 | import org.xml.sax.ErrorHandler; | |
| 17 | import org.xml.sax.SAXException; | |
| 18 | import org.xml.sax.XMLReader; | |
| 19 | import org.xml.sax.helpers.DefaultHandler; | |
| 20 | ||
| 21 | /** <p><code>SAXValidator</code> validates an XML document by | |
| 22 | * writing the document to a text buffer and parsing it with a validating | |
| 23 | * SAX parser. | |
| 24 | * This could be implemented much more efficiently by validating against the | |
| 25 | * dom4j object model directly but at least allows the reuse of existing | |
| 26 | * SAX based validating parsers.</p> | |
| 27 | * | |
| 28 | * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> | |
| 29 | * @version $Revision: 1.8 $ | |
| 30 | */ | |
| 31 | public class SAXValidator { | |
| 32 | ||
| 33 | /** <code>XMLReader</code> used to parse the SAX events */ | |
| 34 | private XMLReader xmlReader; | |
| 35 | ||
| 36 | /** ErrorHandler class to use */ | |
| 37 | private ErrorHandler errorHandler; | |
| 38 | ||
| 39 | ||
| 40 | ||
| 41 | 0 | public SAXValidator() { |
| 42 | } | |
| 43 | ||
| 44 | 0 | public SAXValidator(XMLReader xmlReader) { |
| 45 | 0 | this.xmlReader = xmlReader; |
| 46 | } | |
| 47 | ||
| 48 | ||
| 49 | ||
| 50 | /** Validates the given <code>Document</code> by writing it to a | |
| 51 | * validating SAX Parser. | |
| 52 | * | |
| 53 | * @param document is the Document to validate | |
| 54 | * @throws SAXException if a validation error occurs | |
| 55 | */ | |
| 56 | 0 | public void validate(Document document) throws SAXException { |
| 57 | 0 | if (document != null) { |
| 58 | 0 | XMLReader xmlReader = getXMLReader(); |
| 59 | 0 | if ( errorHandler != null ) { |
| 60 | 0 | xmlReader.setErrorHandler( errorHandler ); |
| 61 | } | |
| 62 | 0 | try { |
| 63 | 0 | xmlReader.parse( new DocumentInputSource( document ) ); |
| 64 | } | |
| 65 | catch (IOException e) { | |
| 66 | 0 | throw new RuntimeException( |
| 67 | "Caught and exception that should never happen: " + e | |
| 68 | ); | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | ||
| 74 | // Properties | |
| 75 | //------------------------------------------------------------------------- | |
| 76 | ||
| 77 | /** @return the <code>XMLReader</code> used to parse SAX events | |
| 78 | */ | |
| 79 | 0 | public XMLReader getXMLReader() throws SAXException { |
| 80 | 0 | if (xmlReader == null) { |
| 81 | 0 | xmlReader = createXMLReader(); |
| 82 | 0 | configureReader(); |
| 83 | } | |
| 84 | 0 | return xmlReader; |
| 85 | } | |
| 86 | ||
| 87 | /** Sets the <code>XMLReader</code> used to parse SAX events | |
| 88 | * | |
| 89 | * @param xmlReader is the <code>XMLReader</code> to parse SAX events | |
| 90 | */ | |
| 91 | 0 | public void setXMLReader(XMLReader xmlReader) throws SAXException { |
| 92 | 0 | this.xmlReader = xmlReader; |
| 93 | 0 | configureReader(); |
| 94 | } | |
| 95 | ||
| 96 | /** @return the <code>ErrorHandler</code> used by SAX | |
| 97 | */ | |
| 98 | 0 | public ErrorHandler getErrorHandler() { |
| 99 | 0 | return errorHandler; |
| 100 | } | |
| 101 | ||
| 102 | /** Sets the <code>ErrorHandler</code> used by the SAX | |
| 103 | * <code>XMLReader</code>. | |
| 104 | * | |
| 105 | * @param errorHandler is the <code>ErrorHandler</code> used by SAX | |
| 106 | */ | |
| 107 | 0 | public void setErrorHandler(ErrorHandler errorHandler) { |
| 108 | 0 | this.errorHandler = errorHandler; |
| 109 | } | |
| 110 | ||
| 111 | // Implementation methods | |
| 112 | //------------------------------------------------------------------------- | |
| 113 | /** Factory Method to allow alternate methods of | |
| 114 | * creating and configuring XMLReader objects | |
| 115 | */ | |
| 116 | 0 | protected XMLReader createXMLReader() throws SAXException { |
| 117 | 0 | return SAXHelper.createXMLReader( true ); |
| 118 | } | |
| 119 | ||
| 120 | /** Configures the XMLReader before use */ | |
| 121 | 0 | protected void configureReader() throws SAXException { |
| 122 | 0 | ContentHandler handler = xmlReader.getContentHandler(); |
| 123 | 0 | if ( handler == null ) { |
| 124 | 0 | xmlReader.setContentHandler( new DefaultHandler() ); |
| 125 | } | |
| 126 | ||
| 127 | // configure validation support | |
| 128 | 0 | xmlReader.setFeature( |
| 129 | "http://xml.org/sax/features/validation", | |
| 130 | true | |
| 131 | ); | |
| 132 | ||
| 133 | // configure namespace support | |
| 134 | 0 | xmlReader.setFeature( |
| 135 | "http://xml.org/sax/features/namespaces", | |
| 136 | true | |
| 137 | ); | |
| 138 | 0 | xmlReader.setFeature( |
| 139 | "http://xml.org/sax/features/namespace-prefixes", | |
| 140 | false | |
| 141 | ); | |
| 142 | } | |
| 143 | ||
| 144 | } | |
| 145 | ||
| 146 | ||
| 147 | ||
| 148 | ||
| 149 | /* | |
| 150 | * Redistribution and use of this software and associated documentation | |
| 151 | * ("Software"), with or without modification, are permitted provided | |
| 152 | * that the following conditions are met: | |
| 153 | * | |
| 154 | * 1. Redistributions of source code must retain copyright | |
| 155 | * statements and notices. Redistributions must also contain a | |
| 156 | * copy of this document. | |
| 157 | * | |
| 158 | * 2. Redistributions in binary form must reproduce the | |
| 159 | * above copyright notice, this list of conditions and the | |
| 160 | * following disclaimer in the documentation and/or other | |
| 161 | * materials provided with the distribution. | |
| 162 | * | |
| 163 | * 3. The name "DOM4J" must not be used to endorse or promote | |
| 164 | * products derived from this Software without prior written | |
| 165 | * permission of MetaStuff, Ltd. For written permission, | |
| 166 | * please contact dom4j-info@metastuff.com. | |
| 167 | * | |
| 168 | * 4. Products derived from this Software may not be called "DOM4J" | |
| 169 | * nor may "DOM4J" appear in their names without prior written | |
| 170 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
| 171 | * trademark of MetaStuff, Ltd. | |
| 172 | * | |
| 173 | * 5. Due credit should be given to the DOM4J Project - | |
| 174 | * http://www.dom4j.org | |
| 175 | * | |
| 176 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
| 177 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 178 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 179 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 180 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 181 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 182 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 183 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 184 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 185 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 186 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 187 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 188 | * | |
| 189 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 190 | * | |
| 191 | * $Id: SAXValidator.java,v 1.8 2004/06/25 12:34:47 maartenc Exp $ | |
| 192 | */ |
|
||||||||||