001 /*
002 * Copyright (c) 2006 Henri Sivonen
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package fi.iki.hsivonen.xml.checker;
024
025 import org.xml.sax.Attributes;
026 import org.xml.sax.ContentHandler;
027 import org.xml.sax.ErrorHandler;
028 import org.xml.sax.Locator;
029 import org.xml.sax.SAXException;
030 import org.xml.sax.SAXParseException;
031
032 /**
033 * The abstract base class for SAX-based content checkers that listen to
034 * the <code>ContentHandler</code> events and emit errors and warnings to
035 * an <code>ErrorHandler</code>.
036 *
037 * <p>Always delegates <code>ignorableWhitespace()</code> to
038 * <code>characters()</code>. The other <code>ContentHandler</code>
039 * methods here are stubs that do nothing. Subclasses, therefore, never
040 * need to call the superclass methods.
041 *
042 * @version $Id: Checker.java,v 1.6 2006/11/29 18:13:32 hsivonen Exp $
043 * @author hsivonen
044 */
045 public abstract class Checker implements ContentHandler {
046
047 private ErrorHandler errorHandler;
048
049 private Locator locator;
050
051 /**
052 * Constructor.
053 */
054 public Checker() {
055 super();
056 }
057
058 /**
059 * Emit a warning. The locator is used.
060 *
061 * @param message the warning message
062 * @throws SAXException if something goes wrong
063 */
064 public void warn(String message) throws SAXException {
065 if (errorHandler != null) {
066 SAXParseException spe = new SAXParseException(message, locator);
067 errorHandler.warning(spe);
068 }
069 }
070
071 /**
072 * Emit an error. The locator is used.
073 *
074 * @param message the error message
075 * @throws SAXException if something goes wrong
076 */
077 public void err(String message) throws SAXException {
078 if (errorHandler != null) {
079 SAXParseException spe = new SAXParseException(message, locator);
080 errorHandler.error(spe);
081 }
082 }
083
084 /**
085 * Does nothing. Subclasses are expected to override this method with
086 * an implementation that clears the state of the checker and releases
087 * objects the checker might hold references to.
088 */
089 public void reset() {
090 }
091
092 /**
093 * Returns the errorHandler.
094 *
095 * @return the errorHandler
096 */
097 public ErrorHandler getErrorHandler() {
098 return errorHandler;
099 }
100
101 /**
102 * Sets the errorHandler.
103 *
104 * @param errorHandler
105 * the errorHandler to set
106 */
107 public void setErrorHandler(ErrorHandler errorHandler) {
108 this.errorHandler = errorHandler;
109 }
110
111 /**
112 * Returns the locator.
113 *
114 * @return the locator
115 */
116 public Locator getDocumentLocator() {
117 return this.locator;
118 }
119
120 /**
121 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
122 */
123 public void setDocumentLocator(Locator locator) {
124 this.locator = locator;
125 }
126
127 /**
128 * Calls <code>reset()</code>.
129 * @see org.xml.sax.ContentHandler#startDocument()
130 */
131 public void startDocument() throws SAXException {
132 reset();
133 }
134
135 /**
136 * Calls <code>reset()</code>.
137 * @see org.xml.sax.ContentHandler#endDocument()
138 */
139 public void endDocument() throws SAXException {
140 reset();
141 }
142
143 /**
144 * Calls <code>characters()</code>.
145 *
146 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
147 */
148 public final void ignorableWhitespace(char[] ch, int start, int length)
149 throws SAXException {
150 characters(ch, start, length);
151 }
152
153 /**
154 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
155 */
156 public void characters(char[] ch, int start, int length)
157 throws SAXException {
158 }
159
160 /**
161 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
162 * java.lang.String, java.lang.String)
163 */
164 public void endElement(String uri, String localName, String qName)
165 throws SAXException {
166 }
167
168 /**
169 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
170 */
171 public void endPrefixMapping(String prefix) throws SAXException {
172 }
173
174 /**
175 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
176 * java.lang.String)
177 */
178 public void processingInstruction(String target, String data)
179 throws SAXException {
180 }
181
182 /**
183 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
184 */
185 public void skippedEntity(String name) throws SAXException {
186 }
187
188 /**
189 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
190 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
191 */
192 public void startElement(String uri, String localName, String qName,
193 Attributes atts) throws SAXException {
194 }
195
196 /**
197 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
198 * java.lang.String)
199 */
200 public void startPrefixMapping(String prefix, String uri)
201 throws SAXException {
202 }
203 }