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