1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java Sat Jun 07 22:09:32 2014 +0200
1.3 @@ -0,0 +1,112 @@
1.4 +/**
1.5 + * Alt2XML
1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package cz.frantovo.alt2xml;
1.22 +
1.23 +import java.util.Deque;
1.24 +import java.util.LinkedList;
1.25 +import java.util.ServiceLoader;
1.26 +import javax.xml.parsers.ParserConfigurationException;
1.27 +import javax.xml.parsers.SAXParser;
1.28 +import javax.xml.parsers.SAXParserFactory;
1.29 +import org.xml.sax.Parser;
1.30 +import org.xml.sax.SAXException;
1.31 +import org.xml.sax.SAXNotRecognizedException;
1.32 +import org.xml.sax.SAXNotSupportedException;
1.33 +import org.xml.sax.XMLReader;
1.34 +
1.35 +/**
1.36 + *
1.37 + * @author Ing. František Kučera (frantovo.cz)
1.38 + */
1.39 +public class ParserFactory extends SAXParserFactory implements ReaderFinder {
1.40 +
1.41 + private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
1.42 +
1.43 + public ParserFactory() {
1.44 + super();
1.45 + for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
1.46 + readerFactories.add(f);
1.47 + }
1.48 +
1.49 + }
1.50 +
1.51 + @Override
1.52 + public XMLReader findReader(String systemId) throws SAXException {
1.53 + for (Alt2XmlReaderFactory f : readerFactories) {
1.54 + if (f.canRead(systemId)) {
1.55 + return f.getReader();
1.56 + }
1.57 + }
1.58 + throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
1.59 + }
1.60 +
1.61 + @Override
1.62 + public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
1.63 + return new AltSAXParser(new SuperReader(this));
1.64 + }
1.65 +
1.66 + @Override
1.67 + public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
1.68 + throw new SAXNotSupportedException("Zatím není podporováno.");
1.69 + }
1.70 +
1.71 + @Override
1.72 + public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
1.73 + throw new SAXNotSupportedException("Zatím není podporováno.");
1.74 + }
1.75 +
1.76 + private static class AltSAXParser extends SAXParser {
1.77 +
1.78 + private XMLReader xmlReader;
1.79 +
1.80 + public AltSAXParser(XMLReader xmlReader) {
1.81 + this.xmlReader = xmlReader;
1.82 + }
1.83 +
1.84 + @Override
1.85 + @Deprecated
1.86 + public Parser getParser() throws SAXException {
1.87 + throw new SAXException("Není podporováno.");
1.88 + }
1.89 +
1.90 + @Override
1.91 + public XMLReader getXMLReader() throws SAXException {
1.92 + return xmlReader;
1.93 + }
1.94 +
1.95 + @Override
1.96 + public boolean isNamespaceAware() {
1.97 + return true;
1.98 + }
1.99 +
1.100 + @Override
1.101 + public boolean isValidating() {
1.102 + return false;
1.103 + }
1.104 +
1.105 + @Override
1.106 + public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
1.107 + xmlReader.setProperty(name, value);
1.108 + }
1.109 +
1.110 + @Override
1.111 + public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
1.112 + return xmlReader.getProperty(name);
1.113 + }
1.114 + }
1.115 +}