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, either version 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 package cz.frantovo.alt2xml;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.UnsupportedEncodingException;
25 import java.net.MalformedURLException;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.xml.sax.EntityResolver;
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class AltInputSource extends InputSource {
39 private final EntityResolver entityResolver;
40 private final InputSource originalSource;
42 private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
44 public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
45 this.entityResolver = entityResolver;
46 this.originalSource = originalSource;
50 public InputStream getByteStream() {
51 InputStream originalStream = originalSource.getByteStream();
53 if (originalStream == null) {
55 InputSource source = originalSource;
57 if (entityResolver != null) {
59 InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
60 if (resolvedSource != null) {
61 source = resolvedSource;
63 } catch (IOException | SAXException e) {
64 log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
68 InputStream resolvedStream = source.getByteStream();
70 if (resolvedStream == null) {
73 URL url = new URL(source.getSystemId());
74 return url.openStream();
75 } catch (MalformedURLException e) {
76 log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
78 } catch (IOException e) {
79 log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
83 return resolvedStream;
87 return originalStream;
92 public Reader getCharacterStream() {
93 Reader originalStream = originalSource.getCharacterStream();
95 if (originalStream == null) {
96 String encoding = getEncoding();
98 if (encoding == null) {
99 return new InputStreamReader(getByteStream());
102 return new InputStreamReader(getByteStream(), encoding);
103 } catch (UnsupportedEncodingException e) {
104 log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
109 return originalStream;
114 public String getEncoding() {
115 return originalSource.getEncoding();
119 public String getPublicId() {
120 return originalSource.getPublicId();
124 public String getSystemId() {
125 return originalSource.getSystemId();