1   /*
2    * This is based on XMLProcess.
3    * @author Andy Clark, IBM
4    * @author Arnaud Le Hors, IBM
5    *
6    * Copyright 2000-2002,2004,2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   *
20   */
21  
22  /**
23   * Distribution License:
24   * JSword is free software; you can redistribute it and/or modify it under
25   * the terms of the GNU Lesser General Public License, version 2.1 as published by
26   * the Free Software Foundation. This program is distributed in the hope
27   * that it will be useful, but WITHOUT ANY WARRANTY; without even the
28   * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29   * See the GNU Lesser General Public License for more details.
30   *
31   * The License is available on the internet at:
32   *       http://www.gnu.org/copyleft/lgpl.html
33   * or by writing to:
34   *      Free Software Foundation, Inc.
35   *      59 Temple Place - Suite 330
36   *      Boston, MA 02111-1307, USA
37   *
38   * Copyright: 2005
39   *     The copyright to this program is held by it's authors.
40   *
41   * ID: $Id: XMLHandlerAdapter.java 2012 2010-11-11 23:24:41Z dmsmith $
42   */
43  package org.crosswire.common.xml;
44  
45  import java.io.IOException;
46  
47  import org.xml.sax.Attributes;
48  import org.xml.sax.ContentHandler;
49  import org.xml.sax.DTDHandler;
50  import org.xml.sax.EntityResolver;
51  import org.xml.sax.ErrorHandler;
52  import org.xml.sax.InputSource;
53  import org.xml.sax.Locator;
54  import org.xml.sax.SAXException;
55  import org.xml.sax.SAXParseException;
56  import org.xml.sax.ext.DeclHandler;
57  import org.xml.sax.ext.LexicalHandler;
58  
59  /**
60   * Checks a XML document for problems, reporting line and offset.
61   * 
62   * @see gnu.lgpl.License for license details.<br>
63   *      The copyright to this program is held by it's authors.
64   * @author DM Smith [dmsmith555 at yahoo dot com]
65   */
66  public class XMLHandlerAdapter implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler, DeclHandler, LexicalHandler {
67      /** Default constructor. */
68      public XMLHandlerAdapter() {
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
75       */
76      public void setDocumentLocator(Locator locator) {
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see org.xml.sax.ContentHandler#startDocument()
83       */
84      public void startDocument() throws SAXException {
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
91       * java.lang.String)
92       */
93      public void processingInstruction(String target, String data) throws SAXException {
94      }
95  
96      /*
97       * (non-Javadoc)
98       * 
99       * @see org.xml.sax.ContentHandler#characters(char[], int, int)
100      */
101     public void characters(char[] ch, int offset, int length) throws SAXException {
102     }
103 
104     /*
105      * (non-Javadoc)
106      * 
107      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
108      */
109     public void ignorableWhitespace(char[] ch, int offset, int length) throws SAXException {
110     }
111 
112     /*
113      * (non-Javadoc)
114      * 
115      * @see org.xml.sax.ContentHandler#endDocument()
116      */
117     public void endDocument() throws SAXException {
118     }
119 
120     /*
121      * (non-Javadoc)
122      * 
123      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
124      * java.lang.String)
125      */
126     public void startPrefixMapping(String prefix, String uri) throws SAXException {
127     }
128 
129     /*
130      * (non-Javadoc)
131      * 
132      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
133      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
134      */
135     public void startElement(String uri, String localName, String qname, Attributes attributes) throws SAXException {
136     }
137 
138     /*
139      * (non-Javadoc)
140      * 
141      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
142      * java.lang.String, java.lang.String)
143      */
144     public void endElement(String uri, String localName, String qname) throws SAXException {
145     }
146 
147     /*
148      * (non-Javadoc)
149      * 
150      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
151      */
152     public void endPrefixMapping(String prefix) throws SAXException {
153     }
154 
155     /*
156      * (non-Javadoc)
157      * 
158      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
159      */
160     public void skippedEntity(String name) throws SAXException {
161     }
162 
163     /*
164      * (non-Javadoc)
165      * 
166      * @see org.xml.sax.DTDHandler#notationDecl(java.lang.String,
167      * java.lang.String, java.lang.String)
168      */
169     public void notationDecl(String name, String publicId, String systemId) throws SAXException {
170     }
171 
172     /*
173      * (non-Javadoc)
174      * 
175      * @see org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String,
176      * java.lang.String, java.lang.String, java.lang.String)
177      */
178     public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException {
179     }
180 
181     /*
182      * (non-Javadoc)
183      * 
184      * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String,
185      * java.lang.String, java.lang.String)
186      */
187     public void startDTD(String name, String publicId, String systemId) throws SAXException {
188     }
189 
190     /*
191      * (non-Javadoc)
192      * 
193      * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
194      */
195     public void startEntity(String name) throws SAXException {
196     }
197 
198     /*
199      * (non-Javadoc)
200      * 
201      * @see org.xml.sax.ext.LexicalHandler#startCDATA()
202      */
203     public void startCDATA() throws SAXException {
204     }
205 
206     /*
207      * (non-Javadoc)
208      * 
209      * @see org.xml.sax.ext.LexicalHandler#endCDATA()
210      */
211     public void endCDATA() throws SAXException {
212     }
213 
214     /*
215      * (non-Javadoc)
216      * 
217      * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
218      */
219     public void comment(char[] ch, int offset, int length) throws SAXException {
220     }
221 
222     /*
223      * (non-Javadoc)
224      * 
225      * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
226      */
227     public void endEntity(String name) throws SAXException {
228     }
229 
230     /*
231      * (non-Javadoc)
232      * 
233      * @see org.xml.sax.ext.LexicalHandler#endDTD()
234      */
235     public void endDTD() throws SAXException {
236     }
237 
238     /*
239      * (non-Javadoc)
240      * 
241      * @see org.xml.sax.ext.DeclHandler#elementDecl(java.lang.String,
242      * java.lang.String)
243      */
244     public void elementDecl(String name, String contentModel) throws SAXException {
245     }
246 
247     /*
248      * (non-Javadoc)
249      * 
250      * @see org.xml.sax.ext.DeclHandler#attributeDecl(java.lang.String,
251      * java.lang.String, java.lang.String, java.lang.String, java.lang.String)
252      */
253     public void attributeDecl(String elementName, String attributeName, String type, String valueDefault, String value) throws SAXException {
254     }
255 
256     /*
257      * (non-Javadoc)
258      * 
259      * @see org.xml.sax.ext.DeclHandler#internalEntityDecl(java.lang.String,
260      * java.lang.String)
261      */
262     public void internalEntityDecl(String name, String text) throws SAXException {
263     }
264 
265     /*
266      * (non-Javadoc)
267      * 
268      * @see org.xml.sax.ext.DeclHandler#externalEntityDecl(java.lang.String,
269      * java.lang.String, java.lang.String)
270      */
271     public void externalEntityDecl(String name, String publicId, String systemId) throws SAXException {
272     }
273 
274     /*
275      * (non-Javadoc)
276      * 
277      * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
278      * java.lang.String)
279      */
280     public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
281         return null;
282     }
283 
284     /*
285      * (non-Javadoc)
286      * 
287      * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
288      */
289     public void warning(SAXParseException ex) throws SAXException {
290         printError("Warning", ex);
291     }
292 
293     /*
294      * (non-Javadoc)
295      * 
296      * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
297      */
298     public void error(SAXParseException ex) throws SAXException {
299         printError("Error", ex);
300     }
301 
302     /*
303      * (non-Javadoc)
304      * 
305      * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
306      */
307     public void fatalError(SAXParseException ex) throws SAXException {
308         printError("Fatal Error", ex);
309     }
310 
311     /** Prints the error message. */
312     private void printError(String type, SAXParseException ex) {
313         System.err.print("[");
314         System.err.print(type);
315         System.err.print("] ");
316         String systemId = ex.getSystemId();
317         if (systemId != null) {
318             int index = systemId.lastIndexOf('/');
319             if (index != -1) {
320                 systemId = systemId.substring(index + 1);
321             }
322             System.err.print(systemId);
323         }
324         System.err.print(':');
325         System.err.print(ex.getLineNumber());
326         System.err.print(':');
327         System.err.print(ex.getColumnNumber());
328         System.err.print(": ");
329         System.err.print(ex.getMessage());
330         System.err.println();
331         System.err.flush();
332 
333     }
334 }
335