java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 99 fadfde5b3e55
permissions -rw-r--r--
fix license version: GNU GPLv3
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@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@11
     8
 *
franta-hg@11
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    12
 * GNU General Public License for more details.
franta-hg@11
    13
 *
franta-hg@11
    14
 * You should have received a copy of the GNU General Public License
franta-hg@11
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    16
 */
franta-hg@2
    17
package cz.frantovo.alt2xml;
franta-hg@2
    18
franta-hg@15
    19
import java.util.Deque;
franta-hg@99
    20
import java.util.HashMap;
franta-hg@15
    21
import java.util.LinkedList;
franta-hg@99
    22
import java.util.Map;
franta-hg@99
    23
import java.util.Map.Entry;
franta-hg@15
    24
import java.util.ServiceLoader;
franta-hg@34
    25
import javax.xml.parsers.FactoryConfigurationError;
franta-hg@2
    26
import javax.xml.parsers.ParserConfigurationException;
franta-hg@2
    27
import javax.xml.parsers.SAXParser;
franta-hg@2
    28
import javax.xml.parsers.SAXParserFactory;
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@3
    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@16
    38
public class ParserFactory extends SAXParserFactory implements ReaderFinder {
franta-hg@15
    39
franta-hg@15
    40
	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
franta-hg@34
    41
	/**
franta-hg@34
    42
	 * @see #DEFAULT_FACTORY_PROPERTY
franta-hg@34
    43
	 */
franta-hg@34
    44
	private SAXParserFactory fallbackFactory;
franta-hg@34
    45
franta-hg@34
    46
	/**
franta-hg@34
    47
	 * System property which contains SAXParserFactory class name for XML.
franta-hg@34
    48
	 * Will be used as fallback if no alternative factory matches systemId to be parsed.
franta-hg@34
    49
	 */
franta-hg@34
    50
	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
franta-hg@15
    51
franta-hg@99
    52
	private final Map<String, Boolean> features = new HashMap<>();
franta-hg@99
    53
franta-hg@16
    54
	public ParserFactory() {
franta-hg@15
    55
		super();
franta-hg@15
    56
		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
franta-hg@15
    57
			readerFactories.add(f);
franta-hg@15
    58
		}
franta-hg@15
    59
franta-hg@34
    60
		readerFactories.add(new FallbackReaderFactory());
franta-hg@34
    61
	}
franta-hg@34
    62
franta-hg@34
    63
	/**
franta-hg@34
    64
	 * @return factory to be used for XML documents (default/fallback)
franta-hg@34
    65
	 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
franta-hg@34
    66
	 * infinite recursion.
franta-hg@34
    67
	 */
franta-hg@34
    68
	public SAXParserFactory getFallbackFactory() {
franta-hg@34
    69
		if (fallbackFactory == null) {
franta-hg@34
    70
			String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
franta-hg@34
    71
			if (className == null) {
franta-hg@34
    72
				fallbackFactory = SAXParserFactory.newInstance();
franta-hg@34
    73
			} else {
franta-hg@34
    74
				fallbackFactory = SAXParserFactory.newInstance(className, null);
franta-hg@34
    75
			}
franta-hg@34
    76
		}
franta-hg@34
    77
franta-hg@34
    78
		if (fallbackFactory.getClass().equals(getClass())) {
franta-hg@34
    79
			throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
franta-hg@34
    80
		} else {
franta-hg@34
    81
			return fallbackFactory;
franta-hg@34
    82
		}
franta-hg@34
    83
	}
franta-hg@34
    84
franta-hg@34
    85
	public void setFallbackFactory(SAXParserFactory fallbackFactory) {
franta-hg@34
    86
		this.fallbackFactory = fallbackFactory;
franta-hg@34
    87
	}
franta-hg@34
    88
franta-hg@34
    89
	private class FallbackReaderFactory implements Alt2XmlReaderFactory {
franta-hg@34
    90
franta-hg@34
    91
		@Override
franta-hg@36
    92
		public boolean canRead(AltInputSource inputSource) {
franta-hg@34
    93
			return true;
franta-hg@34
    94
		}
franta-hg@34
    95
franta-hg@34
    96
		@Override
franta-hg@34
    97
		public XMLReader getReader() throws SAXException {
franta-hg@34
    98
franta-hg@34
    99
			try {
franta-hg@34
   100
				return getFallbackFactory().newSAXParser().getXMLReader();
franta-hg@34
   101
			} catch (ParserConfigurationException e) {
franta-hg@34
   102
				throw new SAXException("Unable to instantiate the fallback factory.", e);
franta-hg@34
   103
			}
franta-hg@34
   104
		}
franta-hg@15
   105
	}
franta-hg@15
   106
franta-hg@15
   107
	@Override
franta-hg@36
   108
	public XMLReader findReader(AltInputSource inputSource) throws SAXException {
franta-hg@15
   109
		for (Alt2XmlReaderFactory f : readerFactories) {
franta-hg@36
   110
			if (f.canRead(inputSource)) {
franta-hg@15
   111
				return f.getReader();
franta-hg@15
   112
			}
franta-hg@15
   113
		}
franta-hg@36
   114
		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
franta-hg@15
   115
	}
franta-hg@2
   116
franta-hg@2
   117
	@Override
franta-hg@2
   118
	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
franta-hg@99
   119
		SuperReader r = new SuperReader(this);
franta-hg@99
   120
		for (Entry<String, Boolean> f : features.entrySet()) {
franta-hg@99
   121
			r.setFeature(f.getKey(), f.getValue());
franta-hg@99
   122
		}
franta-hg@99
   123
		return new AltSAXParser(r);
franta-hg@2
   124
	}
franta-hg@2
   125
franta-hg@2
   126
	@Override
franta-hg@2
   127
	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@99
   128
		features.put(name, value);
franta-hg@2
   129
	}
franta-hg@2
   130
franta-hg@2
   131
	@Override
franta-hg@2
   132
	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@99
   133
		return features.get(name);
franta-hg@2
   134
	}
franta-hg@15
   135
}