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