public Source createSource()

in src/main/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java [286:379]


        public Source createSource(Source source, String uri) {
            if (source == null) {
                if (log.isDebugEnabled()) {
                    log.debug("URI could not be resolved: " + uri);
                }
                return null;
            }
            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 = XmlSourceUtil.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;
                    }
                }
                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 (XmlSourceUtil.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 = XmlSourceUtil.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;
                }
                return createImageSource(in, source);
            }
            return imageSource;
        }