SimpleHandler.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 package org.qedeq.kernel.xml.handler.common;
016 
017 import java.lang.reflect.Method;
018 
019 import org.qedeq.base.trace.Trace;
020 import org.xml.sax.Locator;
021 import org.xml.sax.helpers.DefaultHandler;
022 
023 
024 /**
025  * SAX handler that remembers {@link org.xml.sax.Locator} and possibly
026  * encoding of XML document.
027  *
028  @author  Michael Meyling
029  */
030 public class SimpleHandler extends DefaultHandler {
031 
032     /** This class. */
033     private static final Class CLASS = SimpleHandler.class;
034 
035     /** Locator for current row and column information. */
036     private Locator locator;
037 
038     /** Which encoding was used to parse the document? */
039     private String encoding;
040 
041     /** File that is parsed. */
042     private String url;
043 
044     /**
045      * Constructor.
046      */
047     public SimpleHandler() {
048         super();
049     }
050 
051     /**
052      * Receive a Locator object for document events.
053      * Store the locator for use with other document events.
054      *
055      @param   locator A locator for all SAX document events.
056      @see     org.xml.sax.ContentHandler#setDocumentLocator
057      @see     org.xml.sax.Locator
058      */
059     public void setDocumentLocator(final Locator locator) {
060         this.locator = locator;
061         this.encoding = getEncoding(locator);
062         Trace.paramInfo(CLASS, this, "setDocumentLocator(locator)""encoding", encoding);
063     }
064 
065     /**
066      * Which encoding was used to parse the document?
067      *
068      @return  Encoding for parsed document. Maybe <code>null</code>.
069      */
070     public String getEncoding() {
071         return this.encoding;
072     }
073 
074     /**
075      * There is no way in SAX 2.0.0 or 2.0.1 to get information about the encoding; the SAX
076      * Locator2 interface from the 1.1 extensions does provide methods to accomplish this,
077      * Assuming <code>Locator</code> is an instance of the SAX Locator interface that supports
078      <code>Locator2</code> call we can get the information.
079      *
080      @param   locator Locator.
081      @return  Encoding. Maybe <code>null</code>.
082      */
083     private static String getEncoding(final Locator locator) {
084         String encoding = null;
085         Method getEncoding = null;
086         try {
087             getEncoding = locator.getClass().getMethod("getEncoding"new Class[]{});
088             if (getEncoding != null) {
089                 encoding = (StringgetEncoding.invoke(locator, null);
090             }
091         catch (Exception e) {
092             // either this locator object doesn't have this
093             // method, or we're on an old JDK
094         }
095         return encoding;
096     }
097 
098     /**
099      * Get document locator. This value set is set during parsing.
100      *
101      @return  Locator. Maybe <code>null</code>.
102      */
103     protected Locator getLocator() {
104         return locator;
105     }
106 
107     /**
108      * Set original file URL.
109      *
110      @param   url     Data from this source is parsed. This URL is only for information. The
111      *                  actual parsed data might be a local copy.
112      */
113     public final void setUrl(final String url) {
114         this.url = url;
115     }
116 
117     /**
118      * Get original file URL.
119      *
120      @return  Data from this source is parsed. This URL is only for information. The
121      *          actual parsed data might be a local copy.
122      */
123     public final String getUrl() {
124         return url;
125     }
126 
127 }