java/alt2xml-bin/src/cz/frantovo/alt2xml/SAXTovarna.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 10:38:31 +0200
changeset 15 b890783e4043
parent 11 aaf6648af0aa
permissions -rw-r--r--
META-INF/services, Alt2XmlReaderFactory, ReaderFinder
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@2
    18
package cz.frantovo.alt2xml;
franta-hg@2
    19
franta-hg@3
    20
import cz.frantovo.alt2xml.vstup.SuperReader;
franta-hg@15
    21
import java.util.Deque;
franta-hg@15
    22
import java.util.LinkedList;
franta-hg@15
    23
import java.util.ServiceLoader;
franta-hg@2
    24
import javax.xml.parsers.ParserConfigurationException;
franta-hg@2
    25
import javax.xml.parsers.SAXParser;
franta-hg@2
    26
import javax.xml.parsers.SAXParserFactory;
franta-hg@3
    27
import org.xml.sax.Parser;
franta-hg@2
    28
import org.xml.sax.SAXException;
franta-hg@2
    29
import org.xml.sax.SAXNotRecognizedException;
franta-hg@2
    30
import org.xml.sax.SAXNotSupportedException;
franta-hg@3
    31
import org.xml.sax.XMLReader;
franta-hg@2
    32
franta-hg@2
    33
/**
franta-hg@2
    34
 *
franta-hg@2
    35
 * @author fiki
franta-hg@2
    36
 */
franta-hg@15
    37
public class SAXTovarna extends SAXParserFactory implements ReaderFinder {
franta-hg@15
    38
franta-hg@15
    39
	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
franta-hg@15
    40
franta-hg@15
    41
	public SAXTovarna() {
franta-hg@15
    42
		super();
franta-hg@15
    43
		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
franta-hg@15
    44
			readerFactories.add(f);
franta-hg@15
    45
		}
franta-hg@15
    46
franta-hg@15
    47
	}
franta-hg@15
    48
franta-hg@15
    49
	@Override
franta-hg@15
    50
	public XMLReader findReader(String systemId) throws SAXException {
franta-hg@15
    51
		for (Alt2XmlReaderFactory f : readerFactories) {
franta-hg@15
    52
			if (f.canRead(systemId)) {
franta-hg@15
    53
				return f.getReader();
franta-hg@15
    54
			}
franta-hg@15
    55
		}
franta-hg@15
    56
		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
franta-hg@15
    57
	}
franta-hg@2
    58
franta-hg@2
    59
	@Override
franta-hg@2
    60
	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
franta-hg@15
    61
		return new AltSAXParser(new SuperReader(this));
franta-hg@2
    62
	}
franta-hg@2
    63
franta-hg@2
    64
	@Override
franta-hg@2
    65
	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@4
    66
		throw new SAXNotSupportedException("Zatím není podporováno.");
franta-hg@2
    67
	}
franta-hg@2
    68
franta-hg@2
    69
	@Override
franta-hg@2
    70
	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@4
    71
		throw new SAXNotSupportedException("Zatím není podporováno.");
franta-hg@2
    72
	}
franta-hg@3
    73
franta-hg@11
    74
	private static class AltSAXParser extends SAXParser {
franta-hg@3
    75
franta-hg@3
    76
		private XMLReader xmlReader;
franta-hg@3
    77
franta-hg@11
    78
		public AltSAXParser(XMLReader xmlReader) {
franta-hg@3
    79
			this.xmlReader = xmlReader;
franta-hg@3
    80
		}
franta-hg@3
    81
franta-hg@3
    82
		@Override
franta-hg@11
    83
		@Deprecated
franta-hg@3
    84
		public Parser getParser() throws SAXException {
franta-hg@4
    85
			throw new SAXException("Není podporováno.");
franta-hg@3
    86
		}
franta-hg@3
    87
franta-hg@3
    88
		@Override
franta-hg@3
    89
		public XMLReader getXMLReader() throws SAXException {
franta-hg@3
    90
			return xmlReader;
franta-hg@3
    91
		}
franta-hg@3
    92
franta-hg@3
    93
		@Override
franta-hg@3
    94
		public boolean isNamespaceAware() {
franta-hg@11
    95
			return true;
franta-hg@3
    96
		}
franta-hg@3
    97
franta-hg@3
    98
		@Override
franta-hg@3
    99
		public boolean isValidating() {
franta-hg@3
   100
			return false;
franta-hg@3
   101
		}
franta-hg@3
   102
franta-hg@3
   103
		@Override
franta-hg@3
   104
		public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@4
   105
			xmlReader.setProperty(name, value);
franta-hg@3
   106
		}
franta-hg@3
   107
franta-hg@3
   108
		@Override
franta-hg@3
   109
		public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@4
   110
			return xmlReader.getProperty(name);
franta-hg@3
   111
		}
franta-hg@3
   112
	}
franta-hg@15
   113
}