java/alt2xml-lib-input/src/cz/frantovo/alt2xml/ParserFactory.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 07 Jun 2014 23:17:59 +0200
changeset 34 5af9c7693d70
parent 32 ecc2731a5a46
child 36 ad36a104623f
permissions -rw-r--r--
Fallback SAXParserFactory
→ we can parse also pure/standard XML files
→ seamless integration of multiple formats
     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.Parser;
    28 import org.xml.sax.SAXException;
    29 import org.xml.sax.SAXNotRecognizedException;
    30 import org.xml.sax.SAXNotSupportedException;
    31 import org.xml.sax.XMLReader;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class ParserFactory extends SAXParserFactory implements ReaderFinder {
    38 
    39 	private final Deque<Alt2XmlReaderFactory> readerFactories = new LinkedList();
    40 	/**
    41 	 * @see #DEFAULT_FACTORY_PROPERTY
    42 	 */
    43 	private SAXParserFactory fallbackFactory;
    44 
    45 	/**
    46 	 * System property which contains SAXParserFactory class name for XML.
    47 	 * Will be used as fallback if no alternative factory matches systemId to be parsed.
    48 	 */
    49 	public static final String DEFAULT_FACTORY_PROPERTY = "cz.frantovo.alt2xml.fallback.javax.xml.parsers.SAXParserFactory";
    50 
    51 	public ParserFactory() {
    52 		super();
    53 		for (Alt2XmlReaderFactory f : ServiceLoader.load(Alt2XmlReaderFactory.class)) {
    54 			readerFactories.add(f);
    55 		}
    56 
    57 		readerFactories.add(new FallbackReaderFactory());
    58 	}
    59 
    60 	/**
    61 	 * @return factory to be used for XML documents (default/fallback)
    62 	 * @throws FactoryConfigurationError if fallback factory is the same one as this – avoid
    63 	 * infinite recursion.
    64 	 */
    65 	public SAXParserFactory getFallbackFactory() {
    66 		if (fallbackFactory == null) {
    67 			String className = System.getProperty(DEFAULT_FACTORY_PROPERTY);
    68 			if (className == null) {
    69 				fallbackFactory = SAXParserFactory.newInstance();
    70 			} else {
    71 				fallbackFactory = SAXParserFactory.newInstance(className, null);
    72 			}
    73 		}
    74 
    75 		if (fallbackFactory.getClass().equals(getClass())) {
    76 			throw new FactoryConfigurationError("Fallback factory is the same class as this one – avoid infinite recursion: " + getClass());
    77 		} else {
    78 			return fallbackFactory;
    79 		}
    80 	}
    81 
    82 	public void setFallbackFactory(SAXParserFactory fallbackFactory) {
    83 		this.fallbackFactory = fallbackFactory;
    84 	}
    85 
    86 	private class FallbackReaderFactory implements Alt2XmlReaderFactory {
    87 
    88 		@Override
    89 		public boolean canRead(String systemId) {
    90 			return true;
    91 		}
    92 
    93 		@Override
    94 		public XMLReader getReader() throws SAXException {
    95 
    96 			try {
    97 				return getFallbackFactory().newSAXParser().getXMLReader();
    98 			} catch (ParserConfigurationException e) {
    99 				throw new SAXException("Unable to instantiate the fallback factory.", e);
   100 			}
   101 		}
   102 	}
   103 
   104 	@Override
   105 	public XMLReader findReader(String systemId) throws SAXException {
   106 		for (Alt2XmlReaderFactory f : readerFactories) {
   107 			if (f.canRead(systemId)) {
   108 				return f.getReader();
   109 			}
   110 		}
   111 		throw new SAXException("Iterated over " + readerFactories.size() + " and was unable to find XMLReader for SystemId: " + systemId);
   112 	}
   113 
   114 	@Override
   115 	public SAXParser newSAXParser() throws ParserConfigurationException, SAXException {
   116 		return new AltSAXParser(new SuperReader(this));
   117 	}
   118 
   119 	/**
   120 	 * TODO: feature for disabling default SAXParserFactory
   121 	 */
   122 	@Override
   123 	public void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   124 		throw new SAXNotSupportedException("Zatím není podporováno.");
   125 	}
   126 
   127 	@Override
   128 	public boolean getFeature(String name) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException {
   129 		throw new SAXNotSupportedException("Zatím není podporováno.");
   130 	}
   131 
   132 	private static class AltSAXParser extends SAXParser {
   133 
   134 		private XMLReader xmlReader;
   135 
   136 		public AltSAXParser(XMLReader xmlReader) {
   137 			this.xmlReader = xmlReader;
   138 		}
   139 
   140 		@Override
   141 		@Deprecated
   142 		public Parser getParser() throws SAXException {
   143 			throw new SAXException("Není podporováno.");
   144 		}
   145 
   146 		@Override
   147 		public XMLReader getXMLReader() throws SAXException {
   148 			return xmlReader;
   149 		}
   150 
   151 		@Override
   152 		public boolean isNamespaceAware() {
   153 			return true;
   154 		}
   155 
   156 		@Override
   157 		public boolean isValidating() {
   158 			return false;
   159 		}
   160 
   161 		@Override
   162 		public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
   163 			xmlReader.setProperty(name, value);
   164 		}
   165 
   166 		@Override
   167 		public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
   168 			return xmlReader.getProperty(name);
   169 		}
   170 	}
   171 }