java/alt2xml-lib-input/src/cz/frantovo/alt2xml/AltInputSource.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 08 Jun 2014 09:53:51 +0200
changeset 36 ad36a104623f
parent 32 ecc2731a5a46
child 111 e4900596abdb
permissions -rw-r--r--
use AltInputSource instead of just systemId in canRead() and findReader()
     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, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.alt2xml;
    19 
    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;
    26 import java.net.URL;
    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;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class AltInputSource extends InputSource {
    38 
    39 	private final EntityResolver entityResolver;
    40 	private final InputSource originalSource;
    41 
    42 	private static final Logger log = Logger.getLogger(AltInputSource.class.getName());
    43 
    44 	public AltInputSource(EntityResolver entityResolver, InputSource originalSource) {
    45 		this.entityResolver = entityResolver;
    46 		this.originalSource = originalSource;
    47 	}
    48 
    49 	@Override
    50 	public InputStream getByteStream() {
    51 		InputStream originalStream = originalSource.getByteStream();
    52 
    53 		if (originalStream == null) {
    54 
    55 			InputSource source = originalSource;
    56 
    57 			if (entityResolver != null) {
    58 				try {
    59 					InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId());
    60 					if (resolvedSource != null) {
    61 						source = resolvedSource;
    62 					}
    63 				} catch (IOException | SAXException e) {
    64 					log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e);
    65 				}
    66 			}
    67 
    68 			InputStream resolvedStream = source.getByteStream();
    69 
    70 			if (resolvedStream == null) {
    71 
    72 				try {
    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);
    77 					return null;
    78 				} catch (IOException e) {
    79 					log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e);
    80 					return null;
    81 				}
    82 			} else {
    83 				return resolvedStream;
    84 			}
    85 
    86 		} else {
    87 			return originalStream;
    88 		}
    89 	}
    90 
    91 	@Override
    92 	public Reader getCharacterStream() {
    93 		Reader originalStream = originalSource.getCharacterStream();
    94 
    95 		if (originalStream == null) {
    96 			String encoding = getEncoding();
    97 
    98 			if (encoding == null) {
    99 				return new InputStreamReader(getByteStream());
   100 			} else {
   101 				try {
   102 					return new InputStreamReader(getByteStream(), encoding);
   103 				} catch (UnsupportedEncodingException e) {
   104 					log.log(Level.WARNING, "Invalid encoding: " + encoding, e);
   105 					return null;
   106 				}
   107 			}
   108 		} else {
   109 			return originalStream;
   110 		}
   111 	}
   112 
   113 	@Override
   114 	public String getEncoding() {
   115 		return originalSource.getEncoding();
   116 	}
   117 
   118 	@Override
   119 	public String getPublicId() {
   120 		return originalSource.getPublicId();
   121 	}
   122 
   123 	@Override
   124 	public String getSystemId() {
   125 		return originalSource.getSystemId();
   126 	}
   127 
   128 	@Override
   129 	public String toString() {
   130 		return "AltInputSource: systemId=" + getSystemId() + " publicId=" + getPublicId();
   131 	}
   132 }