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