3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml;
20 import java.io.IOException;
21 import java.util.HashMap;
23 import java.util.Map.Entry;
24 import org.xml.sax.ContentHandler;
25 import org.xml.sax.DTDHandler;
26 import org.xml.sax.EntityResolver;
27 import org.xml.sax.ErrorHandler;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.SAXNotRecognizedException;
31 import org.xml.sax.SAXNotSupportedException;
32 import org.xml.sax.XMLReader;
36 * @author Ing. František Kučera (frantovo.cz)
38 public class SuperReader implements XMLReader {
40 private ContentHandler contentHandler;
41 private ErrorHandler errorHandler;
42 private DTDHandler dtdHandler;
43 private EntityResolver entityResolver;
44 private final Map<String, Object> properties = new HashMap<>();
45 private final Map<String, Boolean> features = new HashMap<>();
46 private final ReaderFinder readerFinder;
48 public SuperReader(ReaderFinder readerFinder) {
49 this.readerFinder = readerFinder;
53 public void parse(InputSource input) throws IOException, SAXException {
55 input = new AltInputSource(entityResolver, input);
57 System.err.println("SystemId: " + input.getSystemId());
59 XMLReader reader = readerFinder.findReader(input.getSystemId());
61 reader.setContentHandler(contentHandler);
62 reader.setDTDHandler(dtdHandler);
63 reader.setEntityResolver(entityResolver);
64 reader.setErrorHandler(errorHandler);
66 for (Entry<String, Object> e : properties.entrySet()) {
67 reader.setProperty(e.getKey(), e.getValue());
70 for (Entry<String, Boolean> e : features.entrySet()) {
71 reader.setFeature(e.getKey(), e.getValue());
78 public void parse(String systemId) throws IOException, SAXException {
79 parse(new InputSource(systemId));
83 public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
86 * All XMLReaders are required to recognize
87 * the http://xml.org/sax/features/namespaces
88 * and the http://xml.org/sax/features/namespace-prefixes feature names.
90 return features.get(name);
94 public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
96 * TODO: filtrovat – povolit jen náš jmenný prostor
98 features.put(name, value);
102 public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
103 return properties.get(name);
107 public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
108 properties.put(name, value);
112 public void setEntityResolver(EntityResolver entityResolver) {
113 this.entityResolver = entityResolver;
117 public EntityResolver getEntityResolver() {
118 return entityResolver;
122 public void setDTDHandler(DTDHandler dtdHandler) {
123 this.dtdHandler = dtdHandler;
127 public DTDHandler getDTDHandler() {
132 public void setContentHandler(ContentHandler contentHandler) {
133 this.contentHandler = contentHandler;
137 public ContentHandler getContentHandler() {
138 return contentHandler;
142 public void setErrorHandler(ErrorHandler errorHandler) {
143 this.errorHandler = errorHandler;
147 public ErrorHandler getErrorHandler() {