3 * Copyright © 2014 František Kučera (frantovo.cz)
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.net.MalformedURLException;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.xml.sax.EntityResolver;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class AltInputSource extends InputSource {
38 private final EntityResolver entityResolver;
39 private final InputSource originalSource;
41 private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
43 public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
44 this.entityResolver = entityResolver;
45 this.originalSource = originalSource;
49 public InputStream getByteStream() {
50 InputStream originalStream = originalSource.getByteStream();
52 if (originalStream == null) {
54 InputSource source = originalSource;
56 if (entityResolver != null) {
58 InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
59 if (resolvedSource != null) {
60 source = resolvedSource;
62 } catch (IOException | SAXException e) {
63 log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
67 InputStream resolvedStream = source.getByteStream();
69 if (resolvedStream == null) {
72 URL url = new URL(source.getSystemId());
73 return url.openStream();
74 } catch (MalformedURLException e) {
75 log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
77 } catch (IOException e) {
78 log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
82 return resolvedStream;
86 return originalStream;
91 public Reader getCharacterStream() {
92 Reader originalStream = originalSource.getCharacterStream();
94 if (originalStream == null) {
95 String encoding = getEncoding();
97 if (encoding == null) {
98 return new InputStreamReader(getByteStream());
101 return new InputStreamReader(getByteStream(), encoding);
102 } catch (UnsupportedEncodingException e) {
103 log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
108 return originalStream;
113 public String getEncoding() {
114 return originalSource.getEncoding();
118 public String getPublicId() {
119 return originalSource.getPublicId();
123 public String getSystemId() {
124 return originalSource.getSystemId();
128 public String toString() {
129 return "AltInputSource: systemId=" + getSystemId() + " publicId=" + getPublicId();