|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SimpleHandler.java | 50% | 100% | 100% | 95% |
|
||||||||||||||
| 1 | /* $Id: SimpleHandler.java,v 1.11 2008/03/27 05:16:29 m31 Exp $ | |
| 2 | * | |
| 3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
| 4 | * | |
| 5 | * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>. | |
| 6 | * | |
| 7 | * "Hilbert II" is free software; you can redistribute | |
| 8 | * it and/or modify it under the terms of the GNU General Public | |
| 9 | * License as published by the Free Software Foundation; either | |
| 10 | * version 2 of the License, or (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | */ | |
| 17 | package org.qedeq.kernel.xml.parser; | |
| 18 | ||
| 19 | import java.lang.reflect.Method; | |
| 20 | ||
| 21 | import org.qedeq.kernel.trace.Trace; | |
| 22 | import org.xml.sax.Locator; | |
| 23 | import org.xml.sax.helpers.DefaultHandler; | |
| 24 | ||
| 25 | ||
| 26 | /** | |
| 27 | * SAX handler that remembers {@link org.xml.sax.Locator} and possibly | |
| 28 | * encoding of XML document. | |
| 29 | * | |
| 30 | * @version $Revision: 1.11 $ | |
| 31 | * @author Michael Meyling | |
| 32 | */ | |
| 33 | public class SimpleHandler extends DefaultHandler { | |
| 34 | ||
| 35 | /** This class. */ | |
| 36 | private static final Class CLASS = SimpleHandler.class; | |
| 37 | ||
| 38 | /** Locator for current row and column information. */ | |
| 39 | private Locator locator; | |
| 40 | ||
| 41 | /** Which encoding was used to parse the document? */ | |
| 42 | private String encoding; | |
| 43 | ||
| 44 | /** | |
| 45 | * Constructor. | |
| 46 | */ | |
| 47 | 33060 | public SimpleHandler() { |
| 48 | 33060 | super(); |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * Receive a Locator object for document events. | |
| 53 | * Store the locator for use with other document events. | |
| 54 | * | |
| 55 | * @param locator A locator for all SAX document events. | |
| 56 | * @see org.xml.sax.ContentHandler#setDocumentLocator | |
| 57 | * @see org.xml.sax.Locator | |
| 58 | */ | |
| 59 | 33034 | public void setDocumentLocator(final Locator locator) { |
| 60 | 33034 | this.locator = locator; |
| 61 | 33034 | this.encoding = getEncoding(locator); |
| 62 | 33034 | Trace.paramInfo(CLASS, this, "setDocumentLocator(locator)", "encoding", encoding); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Which encoding was used to parse the document? | |
| 67 | * | |
| 68 | * @return Encoding for parsed document. Maybe <code>null</code>. | |
| 69 | */ | |
| 70 | 155 | public String getEncoding() { |
| 71 | 155 | return this.encoding; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * There is no way in SAX 2.0.0 or 2.0.1 to get information about the encoding; the SAX | |
| 76 | * Locator2 interface from the 1.1 extensions does provide methods to accomplish this, | |
| 77 | * Assuming <code>Locator</code> is an instance of the SAX Locator interface that supports | |
| 78 | * <code>Locator2</code> call we can get the information. | |
| 79 | * | |
| 80 | * @param locator Locator. | |
| 81 | * @return Encoding. Maybe <code>null</code>. | |
| 82 | */ | |
| 83 | 33034 | private static String getEncoding(final Locator locator) { |
| 84 | 33034 | String encoding = null; |
| 85 | 33034 | Method getEncoding = null; |
| 86 | 33034 | try { |
| 87 | 33034 | getEncoding = locator.getClass().getMethod("getEncoding", new Class[]{}); |
| 88 | 33034 | if (getEncoding != null) { |
| 89 | 33034 | encoding = (String) getEncoding.invoke(locator, null); |
| 90 | } | |
| 91 | } catch (Exception e) { | |
| 92 | // either this locator object doesn't have this | |
| 93 | // method, or we're on an old JDK | |
| 94 | } | |
| 95 | 33034 | return encoding; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Get document locator. This value set is set during parsing. | |
| 100 | * | |
| 101 | * @return Locator. Maybe <code>null</code>. | |
| 102 | */ | |
| 103 | 137301356 | protected Locator getLocator() { |
| 104 | 137301356 | return locator; |
| 105 | } | |
| 106 | ||
| 107 | } |
|
||||||||||