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 org.whattf.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 206 2007-10-15 07:51:34Z hsivonen $
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 a warning with specified locator.
073 *
074 * @param message the warning message
075 * @throws SAXException if something goes wrong
076 */
077 public void warn(String message, Locator overrideLocator) throws SAXException {
078 if (errorHandler != null) {
079 SAXParseException spe = new SAXParseException(message, overrideLocator);
080 errorHandler.warning(spe);
081 }
082 }
083
084 /**
085 * Emit an error with specified locator.
086 *
087 * @param message the error message
088 * @throws SAXException if something goes wrong
089 */
090 public void err(String message, Locator overrideLocator) throws SAXException {
091 if (errorHandler != null) {
092 SAXParseException spe = new SAXParseException(message, overrideLocator);
093 errorHandler.error(spe);
094 }
095 }
096
097 /**
098 * Emit an error. The locator is used.
099 *
100 * @param message the error message
101 * @throws SAXException if something goes wrong
102 */
103 public void err(String message) throws SAXException {
104 if (errorHandler != null) {
105 SAXParseException spe = new SAXParseException(message, locator);
106 errorHandler.error(spe);
107 }
108 }
109
110 /**
111 * Does nothing. Subclasses are expected to override this method with
112 * an implementation that clears the state of the checker and releases
113 * objects the checker might hold references to.
114 */
115 public void reset() {
116 }
117
118 /**
119 * Returns the errorHandler.
120 *
121 * @return the errorHandler
122 */
123 public ErrorHandler getErrorHandler() {
124 return errorHandler;
125 }
126
127 /**
128 * Sets the errorHandler.
129 *
130 * @param errorHandler
131 * the errorHandler to set
132 */
133 public void setErrorHandler(ErrorHandler errorHandler) {
134 this.errorHandler = errorHandler;
135 }
136
137 /**
138 * Returns the locator.
139 *
140 * @return the locator
141 */
142 public Locator getDocumentLocator() {
143 return this.locator;
144 }
145
146 /**
147 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
148 */
149 public void setDocumentLocator(Locator locator) {
150 this.locator = locator;
151 }
152
153 /**
154 * Calls <code>reset()</code>.
155 * @see org.xml.sax.ContentHandler#startDocument()
156 */
157 public void startDocument() throws SAXException {
158 reset();
159 }
160
161 /**
162 * Calls <code>reset()</code>.
163 * @see org.xml.sax.ContentHandler#endDocument()
164 */
165 public void endDocument() throws SAXException {
166 reset();
167 }
168
169 /**
170 * Calls <code>characters()</code>.
171 *
172 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
173 */
174 public final void ignorableWhitespace(char[] ch, int start, int length)
175 throws SAXException {
176 characters(ch, start, length);
177 }
178
179 /**
180 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
181 */
182 public void characters(char[] ch, int start, int length)
183 throws SAXException {
184 }
185
186 /**
187 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
188 * java.lang.String, java.lang.String)
189 */
190 public void endElement(String uri, String localName, String qName)
191 throws SAXException {
192 }
193
194 /**
195 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
196 */
197 public void endPrefixMapping(String prefix) throws SAXException {
198 }
199
200 /**
201 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
202 * java.lang.String)
203 */
204 public void processingInstruction(String target, String data)
205 throws SAXException {
206 }
207
208 /**
209 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
210 */
211 public void skippedEntity(String name) throws SAXException {
212 }
213
214 /**
215 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
216 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
217 */
218 public void startElement(String uri, String localName, String qName,
219 Attributes atts) throws SAXException {
220 }
221
222 /**
223 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
224 * java.lang.String)
225 */
226 public void startPrefixMapping(String prefix, String uri)
227 throws SAXException {
228 }
229 }