java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 38 3de77c1ac95e
permissions -rw-r--r--
fix license version: GNU GPLv3
     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 java.io.IOException;
    20 import java.util.HashMap;
    21 import java.util.Map;
    22 import java.util.Map.Entry;
    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  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class SuperReader implements XMLReader {
    38 
    39 	private ContentHandler contentHandler;
    40 	private ErrorHandler errorHandler;
    41 	private DTDHandler dtdHandler;
    42 	private EntityResolver entityResolver;
    43 	private final Map<String, Object> properties = new HashMap<>();
    44 	private final Map<String, Boolean> features = new HashMap<>();
    45 	private final ReaderFinder readerFinder;
    46 
    47 	public SuperReader(ReaderFinder readerFinder) {
    48 		this.readerFinder = readerFinder;
    49 	}
    50 
    51 	@Override
    52 	public void parse(InputSource input) throws IOException, SAXException {
    53 
    54 		AltInputSource altInput = new AltInputSource(entityResolver, input);
    55 
    56 		XMLReader reader = readerFinder.findReader(altInput);
    57 
    58 		reader.setContentHandler(contentHandler);
    59 		reader.setDTDHandler(dtdHandler);
    60 		reader.setEntityResolver(entityResolver);
    61 		reader.setErrorHandler(errorHandler);
    62 
    63 		for (Entry<String, Object> e : properties.entrySet()) {
    64 			reader.setProperty(e.getKey(), e.getValue());
    65 		}
    66 
    67 		for (Entry<String, Boolean> e : features.entrySet()) {
    68 			reader.setFeature(e.getKey(), e.getValue());
    69 		}
    70 
    71 		reader.parse(altInput);
    72 	}
    73 
    74 	@Override
    75 	public void parse(String systemId) throws IOException, SAXException {
    76 		parse(new InputSource(systemId));
    77 	}
    78 
    79 	@Override
    80 	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    81 		/**
    82 		 * TODO:
    83 		 * All XMLReaders are required to recognize
    84 		 * the http://xml.org/sax/features/namespaces
    85 		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
    86 		 */
    87 		Boolean feature = features.get(name);
    88 		return feature == null ? false : feature;
    89 	}
    90 	
    91 	@Override
    92 	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    93 		/**
    94 		 * TODO: filtrovat – povolit jen náš jmenný prostor
    95 		 */
    96 		features.put(name, value);
    97 	}
    98 
    99 	@Override
   100 	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
   101 		return properties.get(name);
   102 	}
   103 
   104 	@Override
   105 	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
   106 		properties.put(name, value);
   107 	}
   108 
   109 	@Override
   110 	public void setEntityResolver(EntityResolver entityResolver) {
   111 		this.entityResolver = entityResolver;
   112 	}
   113 
   114 	@Override
   115 	public EntityResolver getEntityResolver() {
   116 		return entityResolver;
   117 	}
   118 
   119 	@Override
   120 	public void setDTDHandler(DTDHandler dtdHandler) {
   121 		this.dtdHandler = dtdHandler;
   122 	}
   123 
   124 	@Override
   125 	public DTDHandler getDTDHandler() {
   126 		return dtdHandler;
   127 	}
   128 
   129 	@Override
   130 	public void setContentHandler(ContentHandler contentHandler) {
   131 		this.contentHandler = contentHandler;
   132 	}
   133 
   134 	@Override
   135 	public ContentHandler getContentHandler() {
   136 		return contentHandler;
   137 	}
   138 
   139 	@Override
   140 	public void setErrorHandler(ErrorHandler errorHandler) {
   141 		this.errorHandler = errorHandler;
   142 	}
   143 
   144 	@Override
   145 	public ErrorHandler getErrorHandler() {
   146 		return errorHandler;
   147 	}
   148 }