diff -r c9f1e497132e -r ecc2731a5a46 java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java Sat Jun 07 22:09:32 2014 +0200 @@ -0,0 +1,148 @@ +/** + * Alt2XML + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.alt2xml; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class SuperReader implements XMLReader { + + private ContentHandler contentHandler; + private ErrorHandler errorHandler; + private DTDHandler dtdHandler; + private EntityResolver entityResolver; + private final Map properties = new HashMap<>(); + private final Map features = new HashMap<>(); + private final ReaderFinder readerFinder; + + public SuperReader(ReaderFinder readerFinder) { + this.readerFinder = readerFinder; + } + + @Override + public void parse(InputSource input) throws IOException, SAXException { + + input = new AltInputSource(entityResolver, input); + + XMLReader reader = readerFinder.findReader(input.getSystemId()); + + reader.setContentHandler(contentHandler); + reader.setDTDHandler(dtdHandler); + reader.setEntityResolver(entityResolver); + reader.setErrorHandler(errorHandler); + + for (Entry e : properties.entrySet()) { + reader.setProperty(e.getKey(), e.getValue()); + } + + for (Entry e : features.entrySet()) { + reader.setFeature(e.getKey(), e.getValue()); + } + + reader.parse(input); + } + + @Override + public void parse(String systemId) throws IOException, SAXException { + parse(new InputSource(systemId)); + } + + @Override + public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { + /** + * TODO: + * All XMLReaders are required to recognize + * the http://xml.org/sax/features/namespaces + * and the http://xml.org/sax/features/namespace-prefixes feature names. + */ + return features.get(name); + } + + @Override + public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { + /** + * TODO: filtrovat – povolit jen náš jmenný prostor + */ + features.put(name, value); + } + + @Override + public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { + return properties.get(name); + } + + @Override + public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { + properties.put(name, value); + } + + @Override + public void setEntityResolver(EntityResolver entityResolver) { + this.entityResolver = entityResolver; + } + + @Override + public EntityResolver getEntityResolver() { + return entityResolver; + } + + @Override + public void setDTDHandler(DTDHandler dtdHandler) { + this.dtdHandler = dtdHandler; + } + + @Override + public DTDHandler getDTDHandler() { + return dtdHandler; + } + + @Override + public void setContentHandler(ContentHandler contentHandler) { + this.contentHandler = contentHandler; + } + + @Override + public ContentHandler getContentHandler() { + return contentHandler; + } + + @Override + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + @Override + public ErrorHandler getErrorHandler() { + return errorHandler; + } +}