java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AltInputSource.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 36 ad36a104623f
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@22
     1
/**
franta-hg@22
     2
 * Alt2XML
franta-hg@22
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@22
     4
 *
franta-hg@22
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@22
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@22
     8
 *
franta-hg@22
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@22
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@22
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@22
    12
 * GNU General Public License for more details.
franta-hg@22
    13
 *
franta-hg@22
    14
 * You should have received a copy of the GNU General Public License
franta-hg@22
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@22
    16
 */
franta-hg@22
    17
package cz.frantovo.alt2xml;
franta-hg@22
    18
franta-hg@22
    19
import java.io.IOException;
franta-hg@22
    20
import java.io.InputStream;
franta-hg@22
    21
import java.io.InputStreamReader;
franta-hg@22
    22
import java.io.Reader;
franta-hg@22
    23
import java.io.UnsupportedEncodingException;
franta-hg@22
    24
import java.net.MalformedURLException;
franta-hg@22
    25
import java.net.URL;
franta-hg@22
    26
import java.util.logging.Level;
franta-hg@22
    27
import java.util.logging.Logger;
franta-hg@22
    28
import org.xml.sax.EntityResolver;
franta-hg@22
    29
import org.xml.sax.InputSource;
franta-hg@22
    30
import org.xml.sax.SAXException;
franta-hg@22
    31
franta-hg@22
    32
/**
franta-hg@22
    33
 *
franta-hg@22
    34
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@22
    35
 */
franta-hg@22
    36
public class AltInputSource extends InputSource {
franta-hg@22
    37
franta-hg@22
    38
	private final EntityResolver entityResolver;
franta-hg@22
    39
	private final InputSource originalSource;
franta-hg@22
    40
franta-hg@22
    41
	private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
franta-hg@22
    42
franta-hg@22
    43
	public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
franta-hg@22
    44
		this.entityResolver = entityResolver;
franta-hg@22
    45
		this.originalSource = originalSource;
franta-hg@22
    46
	}
franta-hg@22
    47
franta-hg@22
    48
	@Override
franta-hg@22
    49
	public InputStream getByteStream() {
franta-hg@22
    50
		InputStream originalStream = originalSource.getByteStream();
franta-hg@22
    51
franta-hg@22
    52
		if (originalStream == null) {
franta-hg@22
    53
franta-hg@22
    54
			InputSource source = originalSource;
franta-hg@22
    55
franta-hg@22
    56
			if (entityResolver != null) {
franta-hg@22
    57
				try {
franta-hg@22
    58
					InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
franta-hg@22
    59
					if (resolvedSource != null) {
franta-hg@22
    60
						source = resolvedSource;
franta-hg@22
    61
					}
franta-hg@22
    62
				} catch (IOException | SAXException e) {
franta-hg@22
    63
					log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
franta-hg@22
    64
				}
franta-hg@22
    65
			}
franta-hg@22
    66
franta-hg@22
    67
			InputStream resolvedStream = source.getByteStream();
franta-hg@22
    68
franta-hg@22
    69
			if (resolvedStream == null) {
franta-hg@22
    70
franta-hg@22
    71
				try {
franta-hg@22
    72
					URL url = new URL(source.getSystemId());
franta-hg@22
    73
					return url.openStream();
franta-hg@22
    74
				} catch (MalformedURLException e) {
franta-hg@22
    75
					log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
franta-hg@22
    76
					return null;
franta-hg@22
    77
				} catch (IOException e) {
franta-hg@22
    78
					log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
franta-hg@22
    79
					return null;
franta-hg@22
    80
				}
franta-hg@22
    81
			} else {
franta-hg@22
    82
				return resolvedStream;
franta-hg@22
    83
			}
franta-hg@22
    84
franta-hg@22
    85
		} else {
franta-hg@22
    86
			return originalStream;
franta-hg@22
    87
		}
franta-hg@22
    88
	}
franta-hg@22
    89
franta-hg@22
    90
	@Override
franta-hg@22
    91
	public Reader getCharacterStream() {
franta-hg@22
    92
		Reader originalStream = originalSource.getCharacterStream();
franta-hg@22
    93
franta-hg@22
    94
		if (originalStream == null) {
franta-hg@22
    95
			String encoding = getEncoding();
franta-hg@22
    96
franta-hg@22
    97
			if (encoding == null) {
franta-hg@22
    98
				return new InputStreamReader(getByteStream());
franta-hg@22
    99
			} else {
franta-hg@22
   100
				try {
franta-hg@22
   101
					return new InputStreamReader(getByteStream(), encoding);
franta-hg@22
   102
				} catch (UnsupportedEncodingException e) {
franta-hg@22
   103
					log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
franta-hg@22
   104
					return null;
franta-hg@22
   105
				}
franta-hg@22
   106
			}
franta-hg@22
   107
		} else {
franta-hg@22
   108
			return originalStream;
franta-hg@22
   109
		}
franta-hg@22
   110
	}
franta-hg@22
   111
franta-hg@22
   112
	@Override
franta-hg@22
   113
	public String getEncoding() {
franta-hg@22
   114
		return originalSource.getEncoding();
franta-hg@22
   115
	}
franta-hg@22
   116
franta-hg@22
   117
	@Override
franta-hg@22
   118
	public String getPublicId() {
franta-hg@22
   119
		return originalSource.getPublicId();
franta-hg@22
   120
	}
franta-hg@22
   121
franta-hg@22
   122
	@Override
franta-hg@22
   123
	public String getSystemId() {
franta-hg@22
   124
		return originalSource.getSystemId();
franta-hg@22
   125
	}
franta-hg@36
   126
franta-hg@36
   127
	@Override
franta-hg@36
   128
	public String toString() {
franta-hg@36
   129
		return "AltInputSource: systemId=" + getSystemId() + " publicId=" + getPublicId();
franta-hg@36
   130
	}
franta-hg@22
   131
}