java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 15 Nov 2020 20:20:39 +0100
changeset 116 94081a55bf41
parent 111 e4900596abdb
permissions -rw-r--r--
Added tag v0.2 for changeset 96e1125c8500
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.alt2xml;
    18 
    19 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
    20 import java.io.IOException;
    21 import java.util.HashMap;
    22 import java.util.Map;
    23 import org.xml.sax.ContentHandler;
    24 import org.xml.sax.DTDHandler;
    25 import org.xml.sax.EntityResolver;
    26 import org.xml.sax.ErrorHandler;
    27 import org.xml.sax.InputSource;
    28 import org.xml.sax.SAXException;
    29 import org.xml.sax.SAXNotRecognizedException;
    30 import org.xml.sax.SAXNotSupportedException;
    31 import org.xml.sax.XMLReader;
    32 
    33 /**
    34  * Recommended base class for all alternative format readers.
    35  *
    36  * @author Ing. František Kučera (frantovo.cz)
    37  */
    38 public abstract class AbstractAlt2XmlReader implements XMLReader {
    39 
    40 	private static final String PROPERTY_BASE_URL = "https://alt2xml.globalcode.info/sax-property/";
    41 	protected Alt2ContentHandler contentHandler;
    42 	protected ErrorHandler errorHandler;
    43 	protected DTDHandler dtdHandler;
    44 	protected EntityResolver entityResolver;
    45 	protected Map<String, Object> properties = new HashMap<>();
    46 	protected Map<String, Boolean> features = new HashMap<>();
    47 
    48 	@Override
    49 	public void parse(String systemId) throws IOException, SAXException {
    50 		parse(new InputSource(systemId));
    51 	}
    52 
    53 	@Override
    54 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    55 		/**
    56 		 * TODO:
    57 		 * All XMLReaders are required to recognize
    58 		 * the http://xml.org/sax/features/namespaces
    59 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    60 		 */
    61 		Boolean feature = features.get(name);
    62 		return feature == null ? false : feature;
    63 	}
    64 
    65 	@Override
    66 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    67 		/**
    68 		 * TODO: filtrovat – povolit jen náš jmenný prostor
    69 		 */
    70 		features.put(name, value);
    71 	}
    72 
    73 	/**
    74 	 *
    75 	 * @param readerId should be [a-z0-9] and same as specific part of the module/project name (e.g.
    76 	 * alt2xml-in-properties → properties)
    77 	 * @param propertyId should be [a-z0-9-] and unique per module/reader
    78 	 * @return URL
    79 	 */
    80 	protected static String constructPropertyName(String readerId, String propertyId) {
    81 		return PROPERTY_BASE_URL + readerId + "/" + propertyId;
    82 	}
    83 
    84 	@Override
    85 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    86 		return properties.get(name);
    87 	}
    88 
    89 	@Override
    90 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    91 		properties.put(name, value);
    92 	}
    93 
    94 	@Override
    95 	public void setEntityResolver(EntityResolver entityResolver) {
    96 		this.entityResolver = entityResolver;
    97 	}
    98 
    99 	@Override
   100 	public EntityResolver getEntityResolver() {
   101 		return entityResolver;
   102 	}
   103 
   104 	@Override
   105 	public void setDTDHandler(DTDHandler dtdHandler) {
   106 		this.dtdHandler = dtdHandler;
   107 	}
   108 
   109 	@Override
   110 	public DTDHandler getDTDHandler() {
   111 		return dtdHandler;
   112 	}
   113 
   114 	@Override
   115 	public void setContentHandler(ContentHandler contentHandler) {
   116 		this.contentHandler = new Alt2ContentHandler(contentHandler);
   117 	}
   118 
   119 	@Override
   120 	public Alt2ContentHandler getContentHandler() {
   121 		return contentHandler;
   122 	}
   123 
   124 	@Override
   125 	public void setErrorHandler(ErrorHandler errorHandler) {
   126 		this.errorHandler = errorHandler;
   127 	}
   128 
   129 	@Override
   130 	public ErrorHandler getErrorHandler() {
   131 		return errorHandler;
   132 	}
   133 }