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