franta-hg@11: /**
franta-hg@11: * Alt2XML
franta-hg@11: * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11: *
franta-hg@11: * This program is free software: you can redistribute it and/or modify
franta-hg@11: * it under the terms of the GNU General Public License as published by
franta-hg@111: * the Free Software Foundation, version 3 of the License.
franta-hg@11: *
franta-hg@11: * This program is distributed in the hope that it will be useful,
franta-hg@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11: * GNU General Public License for more details.
franta-hg@11: *
franta-hg@11: * You should have received a copy of the GNU General Public License
franta-hg@11: * along with this program. If not, see .
franta-hg@11: */
franta-hg@16: package cz.frantovo.alt2xml;
franta-hg@2:
franta-hg@2: import java.io.IOException;
franta-hg@4: import java.util.HashMap;
franta-hg@4: import java.util.Map;
franta-hg@22: import java.util.Map.Entry;
franta-hg@2: import org.xml.sax.ContentHandler;
franta-hg@2: import org.xml.sax.DTDHandler;
franta-hg@2: import org.xml.sax.EntityResolver;
franta-hg@2: import org.xml.sax.ErrorHandler;
franta-hg@2: import org.xml.sax.InputSource;
franta-hg@2: import org.xml.sax.SAXException;
franta-hg@2: import org.xml.sax.SAXNotRecognizedException;
franta-hg@2: import org.xml.sax.SAXNotSupportedException;
franta-hg@2: import org.xml.sax.XMLReader;
franta-hg@2:
franta-hg@2: /**
franta-hg@2: *
franta-hg@19: * @author Ing. František Kučera (frantovo.cz)
franta-hg@2: */
franta-hg@2: public class SuperReader implements XMLReader {
franta-hg@2:
franta-hg@2: private ContentHandler contentHandler;
franta-hg@4: private ErrorHandler errorHandler;
franta-hg@4: private DTDHandler dtdHandler;
franta-hg@4: private EntityResolver entityResolver;
franta-hg@22: private final Map properties = new HashMap<>();
franta-hg@22: private final Map features = new HashMap<>();
franta-hg@15: private final ReaderFinder readerFinder;
franta-hg@15:
franta-hg@15: public SuperReader(ReaderFinder readerFinder) {
franta-hg@15: this.readerFinder = readerFinder;
franta-hg@15: }
franta-hg@15:
franta-hg@2: @Override
franta-hg@6: public void parse(InputSource input) throws IOException, SAXException {
franta-hg@6:
franta-hg@36: AltInputSource altInput = new AltInputSource(entityResolver, input);
franta-hg@22:
franta-hg@36: XMLReader reader = readerFinder.findReader(altInput);
franta-hg@22:
franta-hg@22: reader.setContentHandler(contentHandler);
franta-hg@22: reader.setDTDHandler(dtdHandler);
franta-hg@22: reader.setEntityResolver(entityResolver);
franta-hg@22: reader.setErrorHandler(errorHandler);
franta-hg@22:
franta-hg@22: for (Entry e : properties.entrySet()) {
franta-hg@22: reader.setProperty(e.getKey(), e.getValue());
franta-hg@22: }
franta-hg@22:
franta-hg@22: for (Entry e : features.entrySet()) {
franta-hg@22: reader.setFeature(e.getKey(), e.getValue());
franta-hg@22: }
franta-hg@22:
franta-hg@36: reader.parse(altInput);
franta-hg@6: }
franta-hg@6:
franta-hg@6: @Override
franta-hg@6: public void parse(String systemId) throws IOException, SAXException {
franta-hg@6: parse(new InputSource(systemId));
franta-hg@6: }
franta-hg@6:
franta-hg@6: @Override
franta-hg@2: public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@7: /**
franta-hg@15: * TODO:
franta-hg@15: * All XMLReaders are required to recognize
franta-hg@15: * the http://xml.org/sax/features/namespaces
franta-hg@7: * and the http://xml.org/sax/features/namespace-prefixes feature names.
franta-hg@7: */
franta-hg@38: Boolean feature = features.get(name);
franta-hg@38: return feature == null ? false : feature;
franta-hg@2: }
franta-hg@38:
franta-hg@2: @Override
franta-hg@2: public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@22: /**
franta-hg@22: * TODO: filtrovat – povolit jen náš jmenný prostor
franta-hg@22: */
franta-hg@22: features.put(name, value);
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21: return properties.get(name);
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21: properties.put(name, value);
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@4: public void setEntityResolver(EntityResolver entityResolver) {
franta-hg@4: this.entityResolver = entityResolver;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public EntityResolver getEntityResolver() {
franta-hg@4: return entityResolver;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@4: public void setDTDHandler(DTDHandler dtdHandler) {
franta-hg@4: this.dtdHandler = dtdHandler;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public DTDHandler getDTDHandler() {
franta-hg@4: return dtdHandler;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@4: public void setContentHandler(ContentHandler contentHandler) {
franta-hg@4: this.contentHandler = contentHandler;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public ContentHandler getContentHandler() {
franta-hg@2: return contentHandler;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@4: public void setErrorHandler(ErrorHandler errorHandler) {
franta-hg@4: this.errorHandler = errorHandler;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public ErrorHandler getErrorHandler() {
franta-hg@4: return errorHandler;
franta-hg@2: }
franta-hg@2: }