java/alt2xml-lib-input/src/cz/frantovo/alt2xml/SuperReader.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 23:34:53 +0200
changeset 93 0d8e8903d638
parent 38 3de77c1ac95e
child 111 e4900596abdb
permissions -rw-r--r--
in-ini: re-eble whitespace in entry keys – but trim spaces before „=“ to support:
„ a b c = d“ → „<a-b-c>…“ instead of „<a-b-c->…“
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@36
    55
		AltInputSource altInput = new AltInputSource(entityResolver, input);
franta-hg@22
    56
franta-hg@36
    57
		XMLReader reader = readerFinder.findReader(altInput);
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@36
    72
		reader.parse(altInput);
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@38
    88
		Boolean feature = features.get(name);
franta-hg@38
    89
		return feature == null ? false : feature;
franta-hg@2
    90
	}
franta-hg@38
    91
	
franta-hg@2
    92
	@Override
franta-hg@2
    93
	public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@22
    94
		/**
franta-hg@22
    95
		 * TODO: filtrovat – povolit jen náš jmenný prostor
franta-hg@22
    96
		 */
franta-hg@22
    97
		features.put(name, value);
franta-hg@2
    98
	}
franta-hg@2
    99
franta-hg@2
   100
	@Override
franta-hg@2
   101
	public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21
   102
		return properties.get(name);
franta-hg@2
   103
	}
franta-hg@2
   104
franta-hg@2
   105
	@Override
franta-hg@2
   106
	public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@21
   107
		properties.put(name, value);
franta-hg@2
   108
	}
franta-hg@2
   109
franta-hg@2
   110
	@Override
franta-hg@4
   111
	public void setEntityResolver(EntityResolver entityResolver) {
franta-hg@4
   112
		this.entityResolver = entityResolver;
franta-hg@2
   113
	}
franta-hg@2
   114
franta-hg@2
   115
	@Override
franta-hg@2
   116
	public EntityResolver getEntityResolver() {
franta-hg@4
   117
		return entityResolver;
franta-hg@2
   118
	}
franta-hg@2
   119
franta-hg@2
   120
	@Override
franta-hg@4
   121
	public void setDTDHandler(DTDHandler dtdHandler) {
franta-hg@4
   122
		this.dtdHandler = dtdHandler;
franta-hg@2
   123
	}
franta-hg@2
   124
franta-hg@2
   125
	@Override
franta-hg@2
   126
	public DTDHandler getDTDHandler() {
franta-hg@4
   127
		return dtdHandler;
franta-hg@2
   128
	}
franta-hg@2
   129
franta-hg@2
   130
	@Override
franta-hg@4
   131
	public void setContentHandler(ContentHandler contentHandler) {
franta-hg@4
   132
		this.contentHandler = contentHandler;
franta-hg@2
   133
	}
franta-hg@2
   134
franta-hg@2
   135
	@Override
franta-hg@2
   136
	public ContentHandler getContentHandler() {
franta-hg@2
   137
		return contentHandler;
franta-hg@2
   138
	}
franta-hg@2
   139
franta-hg@2
   140
	@Override
franta-hg@4
   141
	public void setErrorHandler(ErrorHandler errorHandler) {
franta-hg@4
   142
		this.errorHandler = errorHandler;
franta-hg@2
   143
	}
franta-hg@2
   144
franta-hg@2
   145
	@Override
franta-hg@2
   146
	public ErrorHandler getErrorHandler() {
franta-hg@4
   147
		return errorHandler;
franta-hg@2
   148
	}
franta-hg@2
   149
}