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
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package cz.frantovo.alt2xml;
    18 
    19 import java.util.Deque;
    20 import java.util.HashMap;
    21 import java.util.LinkedList;
    22 import java.util.Map;
    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;
    33 
    34 /**
    35  *
    36  * @author Ing. František Kučera (frantovo.cz)
    37  */
    38 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
    39 
    40 	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
    41 	/**
    42 	 * @see #DEFAULT_FACTORY_PROPERTY
    43 	 */
    44 	private SAXParserFactory fallbackFactory;
    45 
    46 	/**
    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.
    49 	 */
    50 	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
    51 
    52 	private final Map<String, Boolean> features = new HashMap<>();
    53 
    54 	public ParserFactory() {
    55 		super();
    56 		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
    57 			readerFactories.add(f);
    58 		}
    59 
    60 		readerFactories.add(new FallbackReaderFactory());
    61 	}
    62 
    63 	/**
    64 	 * @return factory to be used for XML documents (default/fallback)
    65 	 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
    66 	 * infinite recursion.
    67 	 */
    68 	public SAXParserFactory getFallbackFactory() {
    69 		if (fallbackFactory == null) {
    70 			String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
    71 			if (className == null) {
    72 				fallbackFactory = SAXParserFactory.newInstance();
    73 			} else {
    74 				fallbackFactory = SAXParserFactory.newInstance(className, null);
    75 			}
    76 		}
    77 
    78 		if (fallbackFactory.getClass().equals(getClass())) {
    79 			throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
    80 		} else {
    81 			return fallbackFactory;
    82 		}
    83 	}
    84 
    85 	public void setFallbackFactory(SAXParserFactory fallbackFactory) {
    86 		this.fallbackFactory = fallbackFactory;
    87 	}
    88 
    89 	private class FallbackReaderFactory implements Alt2XmlReaderFactory {
    90 
    91 		@Override
    92 		public boolean canRead(AltInputSource inputSource) {
    93 			return true;
    94 		}
    95 
    96 		@Override
    97 		public XMLReader getReader() throws SAXException {
    98 
    99 			try {
   100 				return getFallbackFactory().newSAXParser().getXMLReader();
   101 			} catch (ParserConfigurationException e) {
   102 				throw new SAXException("Unable to instantiate the fallback factory.", e);
   103 			}
   104 		}
   105 	}
   106 
   107 	@Override
   108 	public XMLReader findReader(AltInputSource inputSource) throws SAXException {
   109 		for (Alt2XmlReaderFactory f : readerFactories) {
   110 			if (f.canRead(inputSource)) {
   111 				return f.getReader();
   112 			}
   113 		}
   114 		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
   115 	}
   116 
   117 	@Override
   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());
   122 		}
   123 		return new AltSAXParser(r);
   124 	}
   125 
   126 	@Override
   127 	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   128 		features.put(name, value);
   129 	}
   130 
   131 	@Override
   132 	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   133 		return features.get(name);
   134 	}
   135 }