java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 Jun 2014 15:00:19 +0200
changeset 39 a5020340362b
parent 37 03f940508c72
child 99 fadfde5b3e55
permissions -rw-r--r--
LexicalHandler (for comments etc.); currnetly not supported
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@15
    21
import java.util.LinkedList;
franta-hg@15
    22
import java.util.ServiceLoader;
franta-hg@34
    23
import javax.xml.parsers.FactoryConfigurationError;
franta-hg@2
    24
import javax.xml.parsers.ParserConfigurationException;
franta-hg@2
    25
import javax.xml.parsers.SAXParser;
franta-hg@2
    26
import javax.xml.parsers.SAXParserFactory;
franta-hg@2
    27
import org.xml.sax.SAXException;
franta-hg@2
    28
import org.xml.sax.SAXNotRecognizedException;
franta-hg@2
    29
import org.xml.sax.SAXNotSupportedException;
franta-hg@3
    30
import org.xml.sax.XMLReader;
franta-hg@2
    31
franta-hg@2
    32
/**
franta-hg@2
    33
 *
franta-hg@19
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    35
 */
franta-hg@16
    36
public class ParserFactory extends SAXParserFactory implements ReaderFinder {
franta-hg@15
    37
franta-hg@15
    38
	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
franta-hg@34
    39
	/**
franta-hg@34
    40
	 * @see #DEFAULT_FACTORY_PROPERTY
franta-hg@34
    41
	 */
franta-hg@34
    42
	private SAXParserFactory fallbackFactory;
franta-hg@34
    43
franta-hg@34
    44
	/**
franta-hg@34
    45
	 * System property which contains SAXParserFactory class name for XML.
franta-hg@34
    46
	 * Will be used as fallback if no alternative factory matches systemId to be parsed.
franta-hg@34
    47
	 */
franta-hg@34
    48
	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
franta-hg@15
    49
franta-hg@16
    50
	public ParserFactory() {
franta-hg@15
    51
		super();
franta-hg@15
    52
		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
franta-hg@15
    53
			readerFactories.add(f);
franta-hg@15
    54
		}
franta-hg@15
    55
franta-hg@34
    56
		readerFactories.add(new FallbackReaderFactory());
franta-hg@34
    57
	}
franta-hg@34
    58
franta-hg@34
    59
	/**
franta-hg@34
    60
	 * @return factory to be used for XML documents (default/fallback)
franta-hg@34
    61
	 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
franta-hg@34
    62
	 * infinite recursion.
franta-hg@34
    63
	 */
franta-hg@34
    64
	public SAXParserFactory getFallbackFactory() {
franta-hg@34
    65
		if (fallbackFactory == null) {
franta-hg@34
    66
			String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
franta-hg@34
    67
			if (className == null) {
franta-hg@34
    68
				fallbackFactory = SAXParserFactory.newInstance();
franta-hg@34
    69
			} else {
franta-hg@34
    70
				fallbackFactory = SAXParserFactory.newInstance(className, null);
franta-hg@34
    71
			}
franta-hg@34
    72
		}
franta-hg@34
    73
franta-hg@34
    74
		if (fallbackFactory.getClass().equals(getClass())) {
franta-hg@34
    75
			throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
franta-hg@34
    76
		} else {
franta-hg@34
    77
			return fallbackFactory;
franta-hg@34
    78
		}
franta-hg@34
    79
	}
franta-hg@34
    80
franta-hg@34
    81
	public void setFallbackFactory(SAXParserFactory fallbackFactory) {
franta-hg@34
    82
		this.fallbackFactory = fallbackFactory;
franta-hg@34
    83
	}
franta-hg@34
    84
franta-hg@34
    85
	private class FallbackReaderFactory implements Alt2XmlReaderFactory {
franta-hg@34
    86
franta-hg@34
    87
		@Override
franta-hg@36
    88
		public boolean canRead(AltInputSource inputSource) {
franta-hg@34
    89
			return true;
franta-hg@34
    90
		}
franta-hg@34
    91
franta-hg@34
    92
		@Override
franta-hg@34
    93
		public XMLReader getReader() throws SAXException {
franta-hg@34
    94
franta-hg@34
    95
			try {
franta-hg@34
    96
				return getFallbackFactory().newSAXParser().getXMLReader();
franta-hg@34
    97
			} catch (ParserConfigurationException e) {
franta-hg@34
    98
				throw new SAXException("Unable to instantiate the fallback factory.", e);
franta-hg@34
    99
			}
franta-hg@34
   100
		}
franta-hg@15
   101
	}
franta-hg@15
   102
franta-hg@15
   103
	@Override
franta-hg@36
   104
	public XMLReader findReader(AltInputSource inputSource) throws SAXException {
franta-hg@15
   105
		for (Alt2XmlReaderFactory f : readerFactories) {
franta-hg@36
   106
			if (f.canRead(inputSource)) {
franta-hg@15
   107
				return f.getReader();
franta-hg@15
   108
			}
franta-hg@15
   109
		}
franta-hg@36
   110
		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
franta-hg@15
   111
	}
franta-hg@2
   112
franta-hg@2
   113
	@Override
franta-hg@2
   114
	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
franta-hg@15
   115
		return new AltSAXParser(new SuperReader(this));
franta-hg@2
   116
	}
franta-hg@2
   117
franta-hg@2
   118
	@Override
franta-hg@2
   119
	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@37
   120
		/**
franta-hg@39
   121
		 * TODO: feature for disabling default/fallback SAXParserFactory
franta-hg@37
   122
		 */
franta-hg@4
   123
		throw new SAXNotSupportedException("Zatím není podporováno.");
franta-hg@2
   124
	}
franta-hg@2
   125
franta-hg@2
   126
	@Override
franta-hg@2
   127
	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
franta-hg@4
   128
		throw new SAXNotSupportedException("Zatím není podporováno.");
franta-hg@2
   129
	}
franta-hg@15
   130
}