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