3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml;
19 import java.util.Deque;
20 import java.util.HashMap;
21 import java.util.LinkedList;
23 import java.util.Map.Entry;
24 import java.util.ServiceLoader;
25 import javax.xml.parsers.FactoryConfigurationError;
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.parsers.SAXParser;
28 import javax.xml.parsers.SAXParserFactory;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.SAXNotRecognizedException;
31 import org.xml.sax.SAXNotSupportedException;
32 import org.xml.sax.XMLReader;
36 * @author Ing. František Kučera (frantovo.cz)
38 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
40 private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
42 * @see #DEFAULT_FACTORY_PROPERTY
44 private SAXParserFactory fallbackFactory;
47 * System property which contains SAXParserFactory class name for XML.
48 * Will be used as fallback if no alternative factory matches systemId to be parsed.
50 public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
52 private final Map<String, Boolean> features = new HashMap<>();
54 public ParserFactory() {
56 for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
57 readerFactories.add(f);
60 readerFactories.add(new FallbackReaderFactory());
64 * @return factory to be used for XML documents (default/fallback)
65 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
68 public SAXParserFactory getFallbackFactory() {
69 if (fallbackFactory == null) {
70 String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
71 if (className == null) {
72 fallbackFactory = SAXParserFactory.newInstance();
74 fallbackFactory = SAXParserFactory.newInstance(className, null);
78 if (fallbackFactory.getClass().equals(getClass())) {
79 throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
81 return fallbackFactory;
85 public void setFallbackFactory(SAXParserFactory fallbackFactory) {
86 this.fallbackFactory = fallbackFactory;
89 private class FallbackReaderFactory implements Alt2XmlReaderFactory {
92 public boolean canRead(AltInputSource inputSource) {
97 public XMLReader getReader() throws SAXException {
100 return getFallbackFactory().newSAXParser().getXMLReader();
101 } catch (ParserConfigurationException e) {
102 throw new SAXException("Unable to instantiate the fallback factory.", e);
108 public XMLReader findReader(AltInputSource inputSource) throws SAXException {
109 for (Alt2XmlReaderFactory f : readerFactories) {
110 if (f.canRead(inputSource)) {
111 return f.getReader();
114 throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
118 public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
119 SuperReader r = new SuperReader(this);
120 for (Entry<String, Boolean> f : features.entrySet()) {
121 r.setFeature(f.getKey(), f.getValue());
123 return new AltSAXParser(r);
127 public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
128 features.put(name, value);
132 public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
133 return features.get(name);