java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 04 Sep 2014 16:28:49 +0200
changeset 67 96ce967c0182
parent 39 a5020340362b
child 99 fadfde5b3e55
permissions -rw-r--r--
cli: support --input-url and multiple names per option (might be used for short variants like -i, -a etc.)
     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, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.alt2xml;
    19 
    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;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
    37 
    38 	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
    39 	/**
    40 	 * @see #DEFAULT_FACTORY_PROPERTY
    41 	 */
    42 	private SAXParserFactory fallbackFactory;
    43 
    44 	/**
    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.
    47 	 */
    48 	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
    49 
    50 	public ParserFactory() {
    51 		super();
    52 		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
    53 			readerFactories.add(f);
    54 		}
    55 
    56 		readerFactories.add(new FallbackReaderFactory());
    57 	}
    58 
    59 	/**
    60 	 * @return factory to be used for XML documents (default/fallback)
    61 	 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
    62 	 * infinite recursion.
    63 	 */
    64 	public SAXParserFactory getFallbackFactory() {
    65 		if (fallbackFactory == null) {
    66 			String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
    67 			if (className == null) {
    68 				fallbackFactory = SAXParserFactory.newInstance();
    69 			} else {
    70 				fallbackFactory = SAXParserFactory.newInstance(className, null);
    71 			}
    72 		}
    73 
    74 		if (fallbackFactory.getClass().equals(getClass())) {
    75 			throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
    76 		} else {
    77 			return fallbackFactory;
    78 		}
    79 	}
    80 
    81 	public void setFallbackFactory(SAXParserFactory fallbackFactory) {
    82 		this.fallbackFactory = fallbackFactory;
    83 	}
    84 
    85 	private class FallbackReaderFactory implements Alt2XmlReaderFactory {
    86 
    87 		@Override
    88 		public boolean canRead(AltInputSource inputSource) {
    89 			return true;
    90 		}
    91 
    92 		@Override
    93 		public XMLReader getReader() throws SAXException {
    94 
    95 			try {
    96 				return getFallbackFactory().newSAXParser().getXMLReader();
    97 			} catch (ParserConfigurationException e) {
    98 				throw new SAXException("Unable to instantiate the fallback factory.", e);
    99 			}
   100 		}
   101 	}
   102 
   103 	@Override
   104 	public XMLReader findReader(AltInputSource inputSource) throws SAXException {
   105 		for (Alt2XmlReaderFactory f : readerFactories) {
   106 			if (f.canRead(inputSource)) {
   107 				return f.getReader();
   108 			}
   109 		}
   110 		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + inputSource);
   111 	}
   112 
   113 	@Override
   114 	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
   115 		return new AltSAXParser(new SuperReader(this));
   116 	}
   117 
   118 	@Override
   119 	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   120 		/**
   121 		 * TODO: feature for disabling default/fallback SAXParserFactory
   122 		 */
   123 		throw new SAXNotSupportedException("Zatím není podporováno.");
   124 	}
   125 
   126 	@Override
   127 	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   128 		throw new SAXNotSupportedException("Zatím není podporováno.");
   129 	}
   130 }