1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java Sat Jun 07 22:09:32 2014 +0200
1.3 @@ -0,0 +1,148 @@
1.4 +/**
1.5 + * Alt2XML
1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package cz.frantovo.alt2xml;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.util.HashMap;
1.25 +import java.util.Map;
1.26 +import java.util.Map.Entry;
1.27 +import org.xml.sax.ContentHandler;
1.28 +import org.xml.sax.DTDHandler;
1.29 +import org.xml.sax.EntityResolver;
1.30 +import org.xml.sax.ErrorHandler;
1.31 +import org.xml.sax.InputSource;
1.32 +import org.xml.sax.SAXException;
1.33 +import org.xml.sax.SAXNotRecognizedException;
1.34 +import org.xml.sax.SAXNotSupportedException;
1.35 +import org.xml.sax.XMLReader;
1.36 +
1.37 +/**
1.38 + *
1.39 + * @author Ing. František Kučera (frantovo.cz)
1.40 + */
1.41 +public class SuperReader implements XMLReader {
1.42 +
1.43 + private ContentHandler contentHandler;
1.44 + private ErrorHandler errorHandler;
1.45 + private DTDHandler dtdHandler;
1.46 + private EntityResolver entityResolver;
1.47 + private final Map<String, Object> properties = new HashMap<>();
1.48 + private final Map<String, Boolean> features = new HashMap<>();
1.49 + private final ReaderFinder readerFinder;
1.50 +
1.51 + public SuperReader(ReaderFinder readerFinder) {
1.52 + this.readerFinder = readerFinder;
1.53 + }
1.54 +
1.55 + @Override
1.56 + public void parse(InputSource input) throws IOException, SAXException {
1.57 +
1.58 + input = new AltInputSource(entityResolver, input);
1.59 +
1.60 + XMLReader reader = readerFinder.findReader(input.getSystemId());
1.61 +
1.62 + reader.setContentHandler(contentHandler);
1.63 + reader.setDTDHandler(dtdHandler);
1.64 + reader.setEntityResolver(entityResolver);
1.65 + reader.setErrorHandler(errorHandler);
1.66 +
1.67 + for (Entry<String, Object> e : properties.entrySet()) {
1.68 + reader.setProperty(e.getKey(), e.getValue());
1.69 + }
1.70 +
1.71 + for (Entry<String, Boolean> e : features.entrySet()) {
1.72 + reader.setFeature(e.getKey(), e.getValue());
1.73 + }
1.74 +
1.75 + reader.parse(input);
1.76 + }
1.77 +
1.78 + @Override
1.79 + public void parse(String systemId) throws IOException, SAXException {
1.80 + parse(new InputSource(systemId));
1.81 + }
1.82 +
1.83 + @Override
1.84 + public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
1.85 + /**
1.86 + * TODO:
1.87 + * All XMLReaders are required to recognize
1.88 + * the http://xml.org/sax/features/namespaces
1.89 + * and the http://xml.org/sax/features/namespace-prefixes feature names.
1.90 + */
1.91 + return features.get(name);
1.92 + }
1.93 +
1.94 + @Override
1.95 + public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
1.96 + /**
1.97 + * TODO: filtrovat – povolit jen náš jmenný prostor
1.98 + */
1.99 + features.put(name, value);
1.100 + }
1.101 +
1.102 + @Override
1.103 + public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
1.104 + return properties.get(name);
1.105 + }
1.106 +
1.107 + @Override
1.108 + public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
1.109 + properties.put(name, value);
1.110 + }
1.111 +
1.112 + @Override
1.113 + public void setEntityResolver(EntityResolver entityResolver) {
1.114 + this.entityResolver = entityResolver;
1.115 + }
1.116 +
1.117 + @Override
1.118 + public EntityResolver getEntityResolver() {
1.119 + return entityResolver;
1.120 + }
1.121 +
1.122 + @Override
1.123 + public void setDTDHandler(DTDHandler dtdHandler) {
1.124 + this.dtdHandler = dtdHandler;
1.125 + }
1.126 +
1.127 + @Override
1.128 + public DTDHandler getDTDHandler() {
1.129 + return dtdHandler;
1.130 + }
1.131 +
1.132 + @Override
1.133 + public void setContentHandler(ContentHandler contentHandler) {
1.134 + this.contentHandler = contentHandler;
1.135 + }
1.136 +
1.137 + @Override
1.138 + public ContentHandler getContentHandler() {
1.139 + return contentHandler;
1.140 + }
1.141 +
1.142 + @Override
1.143 + public void setErrorHandler(ErrorHandler errorHandler) {
1.144 + this.errorHandler = errorHandler;
1.145 + }
1.146 +
1.147 + @Override
1.148 + public ErrorHandler getErrorHandler() {
1.149 + return errorHandler;
1.150 + }
1.151 +}