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.Parser;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.SAXNotRecognizedException;
30 import org.xml.sax.SAXNotSupportedException;
31 import org.xml.sax.XMLReader;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
39 private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
41 * @see #DEFAULT_FACTORY_PROPERTY
43 private SAXParserFactory fallbackFactory;
46 * System property which contains SAXParserFactory class name for XML.
47 * Will be used as fallback if no alternative factory matches systemId to be parsed.
49 public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
51 public ParserFactory() {
53 for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
54 readerFactories.add(f);
57 readerFactories.add(new FallbackReaderFactory());
61 * @return factory to be used for XML documents (default/fallback)
62 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
65 public SAXParserFactory getFallbackFactory() {
66 if (fallbackFactory == null) {
67 String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
68 if (className == null) {
69 fallbackFactory = SAXParserFactory.newInstance();
71 fallbackFactory = SAXParserFactory.newInstance(className, null);
75 if (fallbackFactory.getClass().equals(getClass())) {
76 throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
78 return fallbackFactory;
82 public void setFallbackFactory(SAXParserFactory fallbackFactory) {
83 this.fallbackFactory = fallbackFactory;
86 private class FallbackReaderFactory implements Alt2XmlReaderFactory {
89 public boolean canRead(String systemId) {
94 public XMLReader getReader() throws SAXException {
97 return getFallbackFactory().newSAXParser().getXMLReader();
98 } catch (ParserConfigurationException e) {
99 throw new SAXException("Unable to instantiate the fallback factory.", e);
105 public XMLReader findReader(String systemId) throws SAXException {
106 for (Alt2XmlReaderFactory f : readerFactories) {
107 if (f.canRead(systemId)) {
108 return f.getReader();
111 throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
115 public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
116 return new AltSAXParser(new SuperReader(this));
120 * TODO: feature for disabling default SAXParserFactory
123 public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
124 throw new SAXNotSupportedException("Zatím není podporováno.");
128 public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
129 throw new SAXNotSupportedException("Zatím není podporováno.");
132 private static class AltSAXParser extends SAXParser {
134 private XMLReader xmlReader;
136 public AltSAXParser(XMLReader xmlReader) {
137 this.xmlReader = xmlReader;
142 public Parser getParser() throws SAXException {
143 throw new SAXException("Není podporováno.");
147 public XMLReader getXMLReader() throws SAXException {
152 public boolean isNamespaceAware() {
157 public boolean isValidating() {
162 public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
163 xmlReader.setProperty(name, value);
167 public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
168 return xmlReader.getProperty(name);