franta-hg@22: /** franta-hg@22: * Alt2XML franta-hg@22: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@22: * franta-hg@22: * This program is free software: you can redistribute it and/or modify franta-hg@22: * it under the terms of the GNU General Public License as published by franta-hg@111: * the Free Software Foundation, version 3 of the License. franta-hg@22: * franta-hg@22: * This program is distributed in the hope that it will be useful, franta-hg@22: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@22: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@22: * GNU General Public License for more details. franta-hg@22: * franta-hg@22: * You should have received a copy of the GNU General Public License franta-hg@22: * along with this program. If not, see . franta-hg@22: */ franta-hg@22: package cz.frantovo.alt2xml; franta-hg@22: franta-hg@22: import java.io.IOException; franta-hg@22: import java.io.InputStream; franta-hg@22: import java.io.InputStreamReader; franta-hg@22: import java.io.Reader; franta-hg@22: import java.io.UnsupportedEncodingException; franta-hg@22: import java.net.MalformedURLException; franta-hg@22: import java.net.URL; franta-hg@22: import java.util.logging.Level; franta-hg@22: import java.util.logging.Logger; franta-hg@22: import org.xml.sax.EntityResolver; franta-hg@22: import org.xml.sax.InputSource; franta-hg@22: import org.xml.sax.SAXException; franta-hg@22: franta-hg@22: /** franta-hg@22: * franta-hg@22: * @author Ing. František Kučera (frantovo.cz) franta-hg@22: */ franta-hg@22: public class AltInputSource extends InputSource { franta-hg@22: franta-hg@22: private final EntityResolver entityResolver; franta-hg@22: private final InputSource originalSource; franta-hg@22: franta-hg@22: private static final Logger log = Logger.getLogger(AltInputSource.class.getName()); franta-hg@22: franta-hg@22: public AltInputSource(EntityResolver entityResolver, InputSource originalSource) { franta-hg@22: this.entityResolver = entityResolver; franta-hg@22: this.originalSource = originalSource; franta-hg@22: } franta-hg@22: franta-hg@22: @Override franta-hg@22: public InputStream getByteStream() { franta-hg@22: InputStream originalStream = originalSource.getByteStream(); franta-hg@22: franta-hg@22: if (originalStream == null) { franta-hg@22: franta-hg@22: InputSource source = originalSource; franta-hg@22: franta-hg@22: if (entityResolver != null) { franta-hg@22: try { franta-hg@22: InputSource resolvedSource = entityResolver.resolveEntity(originalSource.getSystemId(), originalSource.getSystemId()); franta-hg@22: if (resolvedSource != null) { franta-hg@22: source = resolvedSource; franta-hg@22: } franta-hg@22: } catch (IOException | SAXException e) { franta-hg@22: log.log(Level.WARNING, "Error while resolving InputSource – publicId: " + originalSource.getPublicId() + " systemId: " + originalSource.getSystemId(), e); franta-hg@22: } franta-hg@22: } franta-hg@22: franta-hg@22: InputStream resolvedStream = source.getByteStream(); franta-hg@22: franta-hg@22: if (resolvedStream == null) { franta-hg@22: franta-hg@22: try { franta-hg@22: URL url = new URL(source.getSystemId()); franta-hg@22: return url.openStream(); franta-hg@22: } catch (MalformedURLException e) { franta-hg@22: log.log(Level.WARNING, "Invalid SystemId URL syntax: " + originalSource.getSystemId(), e); franta-hg@22: return null; franta-hg@22: } catch (IOException e) { franta-hg@22: log.log(Level.WARNING, "Unable to open stream for systemId: " + originalSource.getSystemId(), e); franta-hg@22: return null; franta-hg@22: } franta-hg@22: } else { franta-hg@22: return resolvedStream; franta-hg@22: } franta-hg@22: franta-hg@22: } else { franta-hg@22: return originalStream; franta-hg@22: } franta-hg@22: } franta-hg@22: franta-hg@22: @Override franta-hg@22: public Reader getCharacterStream() { franta-hg@22: Reader originalStream = originalSource.getCharacterStream(); franta-hg@22: franta-hg@22: if (originalStream == null) { franta-hg@22: String encoding = getEncoding(); franta-hg@22: franta-hg@22: if (encoding == null) { franta-hg@22: return new InputStreamReader(getByteStream()); franta-hg@22: } else { franta-hg@22: try { franta-hg@22: return new InputStreamReader(getByteStream(), encoding); franta-hg@22: } catch (UnsupportedEncodingException e) { franta-hg@22: log.log(Level.WARNING, "Invalid encoding: " + encoding, e); franta-hg@22: return null; franta-hg@22: } franta-hg@22: } franta-hg@22: } else { franta-hg@22: return originalStream; franta-hg@22: } franta-hg@22: } franta-hg@22: franta-hg@22: @Override franta-hg@22: public String getEncoding() { franta-hg@22: return originalSource.getEncoding(); franta-hg@22: } franta-hg@22: franta-hg@22: @Override franta-hg@22: public String getPublicId() { franta-hg@22: return originalSource.getPublicId(); franta-hg@22: } franta-hg@22: franta-hg@22: @Override franta-hg@22: public String getSystemId() { franta-hg@22: return originalSource.getSystemId(); franta-hg@22: } franta-hg@36: franta-hg@36: @Override franta-hg@36: public String toString() { franta-hg@36: return "AltInputSource: systemId=" + getSystemId() + " publicId=" + getPublicId(); franta-hg@36: } franta-hg@22: }