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