if (!(source instanceof StreamSource) && !(source instanceof SAXSource)) {
//Return any non-stream Sources and let the ImageLoaders deal with them
return source;
}
ImageSource imageSource = null;
String resolvedURI = source.getSystemId();
URL url;
try {
url = new URL(resolvedURI);
} catch (MalformedURLException e) {
url = null;
}
File f = /*FileUtils.*/toFile(url);
if (f != null) {
boolean directFileAccess = true;
assert (source instanceof StreamSource) || (source instanceof SAXSource);
InputStream in = ImageUtil.getInputStream(source);
if (in == null) {
try {
in = new java.io.FileInputStream(f);
} catch (FileNotFoundException fnfe) {
log.error("Error while opening file."
+ " Could not load image from system identifier '"
+ source.getSystemId() + "' (" + fnfe.getMessage() + ")");
return null;
}
}
if (in != null) {
in = ImageUtil.decorateMarkSupported(in);
try {
if (ImageUtil.isGZIPCompressed(in)) {
//GZIPped stream are not seekable, so buffer/cache like other URLs
directFileAccess = false;
}
} catch (IOException ioe) {
log.error("Error while checking the InputStream for GZIP compression."
+ " Could not load image from system identifier '"
+ source.getSystemId() + "' (" + ioe.getMessage() + ")");
return null;
}
}
if (directFileAccess) {
//Close as the file is reopened in a more optimal way
IOUtils.closeQuietly(in);
try {
// We let the OS' file system cache do the caching for us
// --> lower Java memory consumption, probably no speed loss
final ImageInputStream newInputStream = ImageIO
.createImageInputStream(f);
if (newInputStream == null) {
log.error("Unable to create ImageInputStream for local file "
+ f
+ " from system identifier '"
+ source.getSystemId() + "'");
return null;
} else {
imageSource = new ImageSource(newInputStream,
resolvedURI, true);
}
} catch (IOException ioe) {
log.error("Unable to create ImageInputStream for local file"
+ " from system identifier '"
+ source.getSystemId() + "' (" + ioe.getMessage() + ")");
}
}
}
if (imageSource == null) {
if (ImageUtil.hasReader(source) && !ImageUtil.hasInputStream(source)) {
//We don't handle Reader instances here so return the Source unchanged
return source;
}
// Got a valid source, obtain an InputStream from it
InputStream in = ImageUtil.getInputStream(source);
if (in == null && url != null) {
try {
in = url.openStream();
} catch (Exception ex) {
log.error("Unable to obtain stream from system identifier '"
+ source.getSystemId() + "'");
}
}
if (in == null) {
log.error("The Source that was returned from URI resolution didn't contain"
+ " an InputStream for URI: " + uri);
return null;
}
try {
//Buffer and uncompress if necessary
in = ImageUtil.autoDecorateInputStream(in);
imageSource = new ImageSource(
createImageInputStream(in), source.getSystemId(), false);
} catch (IOException ioe) {
log.error("Unable to create ImageInputStream for InputStream"
+ " from system identifier '"
+ source.getSystemId() + "' (" + ioe.getMessage() + ")");