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
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     8  *
     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.
    13  *
    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/>.
    16  */
    17 package cz.frantovo.alt2xml;
    18 
    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;
    25 import java.net.URL;
    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;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class AltInputSource extends InputSource {
    37 
    38 	private final EntityResolver entityResolver;
    39 	private final InputSource originalSource;
    40 
    41 	private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
    42 
    43 	public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
    44 		this.entityResolver = entityResolver;
    45 		this.originalSource = originalSource;
    46 	}
    47 
    48 	@Override
    49 	public InputStream getByteStream() {
    50 		InputStream originalStream = originalSource.getByteStream();
    51 
    52 		if (originalStream == null) {
    53 
    54 			InputSource source = originalSource;
    55 
    56 			if (entityResolver != null) {
    57 				try {
    58 					InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
    59 					if (resolvedSource != null) {
    60 						source = resolvedSource;
    61 					}
    62 				} catch (IOException | SAXException e) {
    63 					log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
    64 				}
    65 			}
    66 
    67 			InputStream resolvedStream = source.getByteStream();
    68 
    69 			if (resolvedStream == null) {
    70 
    71 				try {
    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);
    76 					return null;
    77 				} catch (IOException e) {
    78 					log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
    79 					return null;
    80 				}
    81 			} else {
    82 				return resolvedStream;
    83 			}
    84 
    85 		} else {
    86 			return originalStream;
    87 		}
    88 	}
    89 
    90 	@Override
    91 	public Reader getCharacterStream() {
    92 		Reader originalStream = originalSource.getCharacterStream();
    93 
    94 		if (originalStream == null) {
    95 			String encoding = getEncoding();
    96 
    97 			if (encoding == null) {
    98 				return new InputStreamReader(getByteStream());
    99 			} else {
   100 				try {
   101 					return new InputStreamReader(getByteStream(), encoding);
   102 				} catch (UnsupportedEncodingException e) {
   103 					log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
   104 					return null;
   105 				}
   106 			}
   107 		} else {
   108 			return originalStream;
   109 		}
   110 	}
   111 
   112 	@Override
   113 	public String getEncoding() {
   114 		return originalSource.getEncoding();
   115 	}
   116 
   117 	@Override
   118 	public String getPublicId() {
   119 		return originalSource.getPublicId();
   120 	}
   121 
   122 	@Override
   123 	public String getSystemId() {
   124 		return originalSource.getSystemId();
   125 	}
   126 
   127 	@Override
   128 	public String toString() {
   129 		return "AltInputSource: systemId=" + getSystemId() + " publicId=" + getPublicId();
   130 	}
   131 }