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