1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AltInputSource.java Sat Jun 07 22:09:32 2014 +0200
1.3 @@ -0,0 +1,127 @@
1.4 +/**
1.5 + * Alt2XML
1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package cz.frantovo.alt2xml;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.io.InputStream;
1.25 +import java.io.InputStreamReader;
1.26 +import java.io.Reader;
1.27 +import java.io.UnsupportedEncodingException;
1.28 +import java.net.MalformedURLException;
1.29 +import java.net.URL;
1.30 +import java.util.logging.Level;
1.31 +import java.util.logging.Logger;
1.32 +import org.xml.sax.EntityResolver;
1.33 +import org.xml.sax.InputSource;
1.34 +import org.xml.sax.SAXException;
1.35 +
1.36 +/**
1.37 + *
1.38 + * @author Ing. František Kučera (frantovo.cz)
1.39 + */
1.40 +public class AltInputSource extends InputSource {
1.41 +
1.42 + private final EntityResolver entityResolver;
1.43 + private final InputSource originalSource;
1.44 +
1.45 + private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
1.46 +
1.47 + public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
1.48 + this.entityResolver = entityResolver;
1.49 + this.originalSource = originalSource;
1.50 + }
1.51 +
1.52 + @Override
1.53 + public InputStream getByteStream() {
1.54 + InputStream originalStream = originalSource.getByteStream();
1.55 +
1.56 + if (originalStream == null) {
1.57 +
1.58 + InputSource source = originalSource;
1.59 +
1.60 + if (entityResolver != null) {
1.61 + try {
1.62 + InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
1.63 + if (resolvedSource != null) {
1.64 + source = resolvedSource;
1.65 + }
1.66 + } catch (IOException | SAXException e) {
1.67 + log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
1.68 + }
1.69 + }
1.70 +
1.71 + InputStream resolvedStream = source.getByteStream();
1.72 +
1.73 + if (resolvedStream == null) {
1.74 +
1.75 + try {
1.76 + URL url = new URL(source.getSystemId());
1.77 + return url.openStream();
1.78 + } catch (MalformedURLException e) {
1.79 + log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e);
1.80 + return null;
1.81 + } catch (IOException e) {
1.82 + log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
1.83 + return null;
1.84 + }
1.85 + } else {
1.86 + return resolvedStream;
1.87 + }
1.88 +
1.89 + } else {
1.90 + return originalStream;
1.91 + }
1.92 + }
1.93 +
1.94 + @Override
1.95 + public Reader getCharacterStream() {
1.96 + Reader originalStream = originalSource.getCharacterStream();
1.97 +
1.98 + if (originalStream == null) {
1.99 + String encoding = getEncoding();
1.100 +
1.101 + if (encoding == null) {
1.102 + return new InputStreamReader(getByteStream());
1.103 + } else {
1.104 + try {
1.105 + return new InputStreamReader(getByteStream(), encoding);
1.106 + } catch (UnsupportedEncodingException e) {
1.107 + log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
1.108 + return null;
1.109 + }
1.110 + }
1.111 + } else {
1.112 + return originalStream;
1.113 + }
1.114 + }
1.115 +
1.116 + @Override
1.117 + public String getEncoding() {
1.118 + return originalSource.getEncoding();
1.119 + }
1.120 +
1.121 + @Override
1.122 + public String getPublicId() {
1.123 + return originalSource.getPublicId();
1.124 + }
1.125 +
1.126 + @Override
1.127 + public String getSystemId() {
1.128 + return originalSource.getSystemId();
1.129 + }
1.130 +}