java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 Jun 2014 00:01:29 +0200
changeset 35 106c5d280835
parent 32 ecc2731a5a46
child 36 ad36a104623f
permissions -rw-r--r--
zapomenutý .jar
franta-hg@11
     1
/**
franta-hg@11
     2
 * Alt2XML
franta-hg@11
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11
     4
 *
franta-hg@11
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@11
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@11
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@11
     8
 * (at your option) any later version.
franta-hg@11
     9
 *
franta-hg@11
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    13
 * GNU General Public License for more details.
franta-hg@11
    14
 *
franta-hg@11
    15
 * You should have received a copy of the GNU General Public License
franta-hg@11
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    17
 */
franta-hg@16
    18
package cz.frantovo.alt2xml;
franta-hg@2
    19
franta-hg@2
    20
import java.io.IOException;
franta-hg@4
    21
import java.util.HashMap;
franta-hg@4
    22
import java.util.Map;
franta-hg@22
    23
import java.util.Map.Entry;
franta-hg@2
    24
import org.xml.sax.ContentHandler;
franta-hg@2
    25
import org.xml.sax.DTDHandler;
franta-hg@2
    26
import org.xml.sax.EntityResolver;
franta-hg@2
    27
import org.xml.sax.ErrorHandler;
franta-hg@2
    28
import org.xml.sax.InputSource;
franta-hg@2
    29
import org.xml.sax.SAXException;
franta-hg@2
    30
import org.xml.sax.SAXNotRecognizedException;
franta-hg@2
    31
import org.xml.sax.SAXNotSupportedException;
franta-hg@2
    32
import org.xml.sax.XMLReader;
franta-hg@2
    33
franta-hg@2
    34
/**
franta-hg@2
    35
 *
franta-hg@19
    36
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    37
 */
franta-hg@2
    38
public class SuperReader implements XMLReader {
franta-hg@2
    39
franta-hg@2
    40
	private ContentHandler contentHandler;
franta-hg@4
    41
	private ErrorHandler errorHandler;
franta-hg@4
    42
	private DTDHandler dtdHandler;
franta-hg@4
    43
	private EntityResolver entityResolver;
franta-hg@22
    44
	private final Map<String, Object> properties = new HashMap<>();
franta-hg@22
    45
	private final Map<String, Boolean> features = new HashMap<>();
franta-hg@15
    46
	private final ReaderFinder readerFinder;
franta-hg@15
    47
franta-hg@15
    48
	public SuperReader(ReaderFinder readerFinder) {
franta-hg@15
    49
		this.readerFinder = readerFinder;
franta-hg@15
    50
	}
franta-hg@15
    51
franta-hg@2
    52
	@Override
franta-hg@6
    53
	public void parse(InputSource input) throws IOException, SAXException {
franta-hg@6
    54
franta-hg@22
    55
		input = new AltInputSource(entityResolver, input);
franta-hg@22
    56
franta-hg@21
    57
		XMLReader reader = readerFinder.findReader(input.getSystemId());
franta-hg@22
    58
franta-hg@22
    59
		reader.setContentHandler(contentHandler);
franta-hg@22
    60
		reader.setDTDHandler(dtdHandler);
franta-hg@22
    61
		reader.setEntityResolver(entityResolver);
franta-hg@22
    62
		reader.setErrorHandler(errorHandler);
franta-hg@22
    63
franta-hg@22
    64
		for (Entry<String, Object> e : properties.entrySet()) {
franta-hg@22
    65
			reader.setProperty(e.getKey(), e.getValue());
franta-hg@22
    66
		}
franta-hg@22
    67
franta-hg@22
    68
		for (Entry<String, Boolean> e : features.entrySet()) {
franta-hg@22
    69
			reader.setFeature(e.getKey(), e.getValue());
franta-hg@22
    70
		}
franta-hg@22
    71
franta-hg@21
    72
		reader.parse(input);
franta-hg@6
    73
	}
franta-hg@6
    74
franta-hg@6
    75
	@Override
franta-hg@6
    76
	public void parse(String systemId) throws IOException, SAXException {
franta-hg@6
    77
		parse(new InputSource(systemId));
franta-hg@6
    78
	}
franta-hg@6
    79
franta-hg@6
    80
	@Override
franta-hg@2
    81
	public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@7
    82
		/**
franta-hg@15
    83
		 * TODO:
franta-hg@15
    84
		 * All XMLReaders are required to recognize
franta-hg@15
    85
		 * the http://xml.org/sax/features/namespaces
franta-hg@7
    86
		 * and the http://xml.org/sax/features/namespace-prefixes feature names.
franta-hg@7
    87
		 */
franta-hg@22
    88
		return features.get(name);
franta-hg@2
    89
	}
franta-hg@2
    90
franta-hg@2
    91
	@Override
franta-hg@2
    92
	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@22
    93
		/**
franta-hg@22
    94
		 * TODO: filtrovat – povolit jen náš jmenný prostor
franta-hg@22
    95
		 */
franta-hg@22
    96
		features.put(name, value);
franta-hg@2
    97
	}
franta-hg@2
    98
franta-hg@2
    99
	@Override
franta-hg@2
   100
	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21
   101
		return properties.get(name);
franta-hg@2
   102
	}
franta-hg@2
   103
franta-hg@2
   104
	@Override
franta-hg@2
   105
	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21
   106
		properties.put(name, value);
franta-hg@2
   107
	}
franta-hg@2
   108
franta-hg@2
   109
	@Override
franta-hg@4
   110
	public void setEntityResolver(EntityResolver entityResolver) {
franta-hg@4
   111
		this.entityResolver = entityResolver;
franta-hg@2
   112
	}
franta-hg@2
   113
franta-hg@2
   114
	@Override
franta-hg@2
   115
	public EntityResolver getEntityResolver() {
franta-hg@4
   116
		return entityResolver;
franta-hg@2
   117
	}
franta-hg@2
   118
franta-hg@2
   119
	@Override
franta-hg@4
   120
	public void setDTDHandler(DTDHandler dtdHandler) {
franta-hg@4
   121
		this.dtdHandler = dtdHandler;
franta-hg@2
   122
	}
franta-hg@2
   123
franta-hg@2
   124
	@Override
franta-hg@2
   125
	public DTDHandler getDTDHandler() {
franta-hg@4
   126
		return dtdHandler;
franta-hg@2
   127
	}
franta-hg@2
   128
franta-hg@2
   129
	@Override
franta-hg@4
   130
	public void setContentHandler(ContentHandler contentHandler) {
franta-hg@4
   131
		this.contentHandler = contentHandler;
franta-hg@2
   132
	}
franta-hg@2
   133
franta-hg@2
   134
	@Override
franta-hg@2
   135
	public ContentHandler getContentHandler() {
franta-hg@2
   136
		return contentHandler;
franta-hg@2
   137
	}
franta-hg@2
   138
franta-hg@2
   139
	@Override
franta-hg@4
   140
	public void setErrorHandler(ErrorHandler errorHandler) {
franta-hg@4
   141
		this.errorHandler = errorHandler;
franta-hg@2
   142
	}
franta-hg@2
   143
franta-hg@2
   144
	@Override
franta-hg@2
   145
	public ErrorHandler getErrorHandler() {
franta-hg@4
   146
		return errorHandler;
franta-hg@2
   147
	}
franta-hg@2
   148
}