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@75: import cz.frantovo.alt2xml.in.Alt2ContentHandler;
franta-hg@2: import java.io.IOException;
franta-hg@4: import java.util.HashMap;
franta-hg@4: import java.util.Map;
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@17: * Recommended base class for all alternative format readers.
franta-hg@2: *
franta-hg@17: * @author Ing. František Kučera (frantovo.cz)
franta-hg@2: */
franta-hg@17: public abstract class AbstractAlt2XmlReader implements XMLReader {
franta-hg@2:
franta-hg@61: private static final String PROPERTY_BASE_URL = "https://alt2xml.globalcode.info/sax-property/";
franta-hg@75: protected Alt2ContentHandler contentHandler;
franta-hg@17: protected ErrorHandler errorHandler;
franta-hg@17: protected DTDHandler dtdHandler;
franta-hg@17: protected EntityResolver entityResolver;
franta-hg@17: protected Map properties = new HashMap<>();
franta-hg@44: protected Map features = new HashMap<>();
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@44: Boolean feature = features.get(name);
franta-hg@44: return feature == null ? false : feature;
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@2: public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@44: /**
franta-hg@44: * TODO: filtrovat – povolit jen náš jmenný prostor
franta-hg@44: */
franta-hg@44: features.put(name, value);
franta-hg@2: }
franta-hg@2:
franta-hg@61: /**
franta-hg@61: *
franta-hg@61: * @param readerId should be [a-z0-9] and same as specific part of the module/project name (e.g.
franta-hg@61: * alt2xml-in-properties → properties)
franta-hg@61: * @param propertyId should be [a-z0-9-] and unique per module/reader
franta-hg@61: * @return URL
franta-hg@61: */
franta-hg@61: protected static String constructPropertyName(String readerId, String propertyId) {
franta-hg@61: return PROPERTY_BASE_URL + readerId + "/" + propertyId;
franta-hg@61: }
franta-hg@61:
franta-hg@2: @Override
franta-hg@2: public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@17: 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@17: 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@75: this.contentHandler = new Alt2ContentHandler(contentHandler);
franta-hg@2: }
franta-hg@2:
franta-hg@2: @Override
franta-hg@75: public Alt2ContentHandler 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: }