1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
4 | * |
5 | * "Hilbert II" is free software; you can redistribute |
6 | * it and/or modify it under the terms of the GNU General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | */ |
15 | package org.qedeq.kernel.xml.handler.common; |
16 | |
17 | import java.lang.reflect.Method; |
18 | |
19 | import org.qedeq.base.trace.Trace; |
20 | import org.xml.sax.Locator; |
21 | import org.xml.sax.helpers.DefaultHandler; |
22 | |
23 | |
24 | /** |
25 | * SAX handler that remembers {@link org.xml.sax.Locator} and possibly |
26 | * encoding of XML document. |
27 | * |
28 | * @author Michael Meyling |
29 | */ |
30 | public class SimpleHandler extends DefaultHandler { |
31 | |
32 | /** This class. */ |
33 | private static final Class CLASS = SimpleHandler.class; |
34 | |
35 | /** Locator for current row and column information. */ |
36 | private Locator locator; |
37 | |
38 | /** Which encoding was used to parse the document? */ |
39 | private String encoding; |
40 | |
41 | /** File that is parsed. */ |
42 | private String url; |
43 | |
44 | /** |
45 | * Constructor. |
46 | */ |
47 | public SimpleHandler() { |
48 | 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 | public void setDocumentLocator(final Locator locator) { |
60 | this.locator = locator; |
61 | this.encoding = getEncoding(locator); |
62 | 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 | public String getEncoding() { |
71 | 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 | private static String getEncoding(final Locator locator) { |
84 | String encoding = null; |
85 | Method getEncoding = null; |
86 | try { |
87 | getEncoding = locator.getClass().getMethod("getEncoding", new Class[]{}); |
88 | if (getEncoding != null) { |
89 | 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 | 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 | 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 | } |