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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml;
20 import java.util.Deque;
21 import java.util.LinkedList;
22 import java.util.ServiceLoader;
23 import javax.xml.parsers.FactoryConfigurationError;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.SAXNotRecognizedException;
29 import org.xml.sax.SAXNotSupportedException;
30 import org.xml.sax.XMLReader;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
38 private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
40 * @see #DEFAULT_FACTORY_PROPERTY
42 private SAXParserFactory fallbackFactory;
45 * System property which contains SAXParserFactory class name for XML.
46 * Will be used as fallback if no alternative factory matches systemId to be parsed.
48 public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
50 public ParserFactory() {
52 for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
53 readerFactories.add(f);
56 readerFactories.add(new FallbackReaderFactory());
60 * @return factory to be used for XML documents (default/fallback)
61 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
64 public SAXParserFactory getFallbackFactory() {
65 if (fallbackFactory == null) {
66 String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
67 if (className == null) {
68 fallbackFactory = SAXParserFactory.newInstance();
70 fallbackFactory = SAXParserFactory.newInstance(className, null);
74 if (fallbackFactory.getClass().equals(getClass())) {
75 throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
77 return fallbackFactory;
81 public void setFallbackFactory(SAXParserFactory fallbackFactory) {
82 this.fallbackFactory = fallbackFactory;
85 private class FallbackReaderFactory implements Alt2XmlReaderFactory {
88 public boolean canRead(AltInputSource inputSource) {
93 public XMLReader getReader() throws SAXException {
96 return getFallbackFactory().newSAXParser().getXMLReader();
97 } catch (ParserConfigurationException e) {
98 throw new SAXException("Unable to instantiate the fallback factory.", e);
104 public XMLReader findReader(AltInputSource inputSource) throws SAXException {
105 for (Alt2XmlReaderFactory f : readerFactories) {
106 if (f.canRead(inputSource)) {
107 return f.getReader();
110 throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
114 public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
115 return new AltSAXParser(new SuperReader(this));
119 public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
121 * TODO: feature for disabling default/fallback SAXParserFactory
123 throw new SAXNotSupportedException("Zatím není podporováno.");
127 public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
128 throw new SAXNotSupportedException("Zatím není podporováno.");