java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AbstractAlt2XmlReader.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Jun 2014 11:11:01 +0200
changeset 44 7bd195a5fa2a
parent 32 ecc2731a5a46
child 61 1980a333904c
permissions -rw-r--r--
AbstractAlt2XmlReader: support features
     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 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 	protected ContentHandler contentHandler;
    41 	protected ErrorHandler errorHandler;
    42 	protected DTDHandler dtdHandler;
    43 	protected EntityResolver entityResolver;
    44 	protected Map<String, Object> properties = new HashMap<>();
    45 	protected Map<String, Boolean> features = new HashMap<>();
    46 
    47 	@Override
    48 	public void parse(String systemId) throws IOException, SAXException {
    49 		parse(new InputSource(systemId));
    50 	}
    51 
    52 	@Override
    53 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    54 		/**
    55 		 * TODO:
    56 		 * All XMLReaders are required to recognize
    57 		 * the http://xml.org/sax/features/namespaces
    58 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    59 		 */
    60 		Boolean feature = features.get(name);
    61 		return feature == null ? false : feature;
    62 	}
    63 
    64 	@Override
    65 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    66 		/**
    67 		 * TODO: filtrovat – povolit jen náš jmenný prostor
    68 		 */
    69 		features.put(name, value);
    70 	}
    71 
    72 	@Override
    73 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    74 		return properties.get(name);
    75 	}
    76 
    77 	@Override
    78 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
    79 		properties.put(name, value);
    80 	}
    81 
    82 	@Override
    83 	public void setEntityResolver(EntityResolver entityResolver) {
    84 		this.entityResolver = entityResolver;
    85 	}
    86 
    87 	@Override
    88 	public EntityResolver getEntityResolver() {
    89 		return entityResolver;
    90 	}
    91 
    92 	@Override
    93 	public void setDTDHandler(DTDHandler dtdHandler) {
    94 		this.dtdHandler = dtdHandler;
    95 	}
    96 
    97 	@Override
    98 	public DTDHandler getDTDHandler() {
    99 		return dtdHandler;
   100 	}
   101 
   102 	@Override
   103 	public void setContentHandler(ContentHandler contentHandler) {
   104 		this.contentHandler = contentHandler;
   105 	}
   106 
   107 	@Override
   108 	public ContentHandler getContentHandler() {
   109 		return contentHandler;
   110 	}
   111 
   112 	@Override
   113 	public void setErrorHandler(ErrorHandler errorHandler) {
   114 		this.errorHandler = errorHandler;
   115 	}
   116 
   117 	@Override
   118 	public ErrorHandler getErrorHandler() {
   119 		return errorHandler;
   120 	}
   121 }