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