FileDocCategorySizeDatePackage
DefaultImageLoader.javaAPI DocphoneME MR2 API (J2ME)14173Wed May 02 18:00:36 BST 2007com.sun.perseus.model

DefaultImageLoader

public class DefaultImageLoader extends Object implements ImageLoader
Default implementation of the ImageLoader interface.
version
$Id: DefaultImageLoader.java,v 1.4 2006/06/29 10:47:30 ln156897 Exp $

Fields Summary
protected Hashtable
cache
Simple hashtable used to cache image objects.
protected static com.sun.perseus.util.RunnableQueue
loadingQueue
RunnableQueue used to load image asynchronously
protected com.sun.perseus.j2d.ImageLoaderUtil
loaderUtil
ImageLoaderUtil contains helper methods which make this implementation easier.
Constructors Summary
public DefaultImageLoader()
Default constructor


                 
     
        loadingQueue =  RunnableQueue.createRunnableQueue(null);
        loadingQueue.resumeExecution();
    
    
Methods Summary
public voidaddRasterImageConsumer(java.lang.String absoluteURI, RasterImageConsumer imageNode)
In cases where the ImageLoader may update the images associated to a URI, RasterImageConsumer interested in updates need to register their interest throught this method.

param
absoluteURI the URI the RasterImageConsumer is interested in.
param
imageNode the RasterImageConsumer interested in the URI.

    
public booleanallowsRelativeURI()
Determines whether this ImageLoader can handle relative uri's

return
true if this ImageLoader can handle relative uri's; false otherwise.

        return false;
    
public voiddocumentLoaded(DocumentNode doc)
Some ImageLoader implementations may wish to wait until the end of the Document load to start retrieving resources. This method notifies the implementation that the DocumentNode completed loading successfully.

param
doc the DocumentNode which just finised loading.

        // Do nothing. The DefaultImageLoader implementation loads image
        // as soon as they are notified in needsURI calls.
    
public com.sun.perseus.j2d.RasterImagegetBrokenImage()
Returns the image that should be used to represent an image which could not be loaded.

return
the image to represent broken uris or content.

        return loaderUtil.getBrokenImage();
    
public com.sun.perseus.j2d.RasterImagegetImageAndWait(java.lang.String uri)
Requests the given image. This call blocks until an image is returned.

param
uri the requested URI. Should be a resolved URI returned from an earlier call to needsURI.
return
the image after it has been loaded. If the image could not be loaded, this returns the same image as returned by a call to getBrokenImage.

        // If we are dealing with a data URI, decode the image
        // now. Data URIs do not go in the cache.
        if (loaderUtil.isDataURI(uri)) {
            return loaderUtil.getEmbededImage(uri);
        }

        // We are dealing with a regular URI which requires IO.
        // The image might already be in the loading queue if
        // a call to needsURI was made. 
        synchronized (cache) {
            RasterImage img = (RasterImage) cache.get(uri);
            if (img != null) {
                return img;
            }
        }

        // The URI has not been retrieved at all or the 
        // ImageLoadRunnable has not completed yet. We
        // simply preempt a new ImageLoadRunnable. When that
        // one complete, we will be sure the image is in
        // the cache.
        ImageLoadRunnable loader = 
            new ImageLoadRunnable(uri);

        try {
            loadingQueue.preemptAndWait(loader, null);
        } catch (InterruptedException ie) {
            // We were interrupted while waiting for the image.
            // Return brokenImage
            return loaderUtil.getBrokenImage();
        }

        synchronized (cache) {
            return (RasterImage) cache.get(uri);
        }
    
public voidgetImageLater(java.lang.String uri, RasterImageConsumer imageNode)
Requests the given image. This call returns immediately and the image is set on the input ImageNode when the image becomes available.

param
uri the requested URI. Should be a resolved URI returned from an earlier call to needsURI.
param
rasterImageConsumer the ImageNode whose image member should be set as a result of loading the image.

        // Only load later images which have not been loaded yet
        // and which are not data URIs.
        if (loaderUtil.isDataURI(uri)) {
            imageNode.setImage(loaderUtil.getEmbededImage(uri), uri);
            return;
        }

        RasterImage img = null;
        synchronized (cache) {
            img = (RasterImage) cache.get(uri);
        }
        
        if (img != null) {
            imageNode.setImage(img, uri);
            return;
        }

        ImageLoadRunnable loader = new ImageLoadRunnable(uri, imageNode);
        loadingQueue.invokeLater(loader, null);
    
public com.sun.perseus.j2d.RasterImagegetLoadingImage()
Returns the image that should be used to represent an image which is loading.

return
the image to use to represent a pending loading.

        return loaderUtil.getLoadingImage();
    
public voidneedsURI(java.lang.String absoluteURI)
Notifies the URILoader that the given uri will be needed.

param
absoluteURI the requested URI content.

        // Do not load base64 images as we do not want to 
        // store the base64 string in the cache, because it
        // might be huge.
        //
        // Correct content should not have the same base64 
        // string duplicated. Rather, the same image element
        // can be referenced by a use element.
        if (!loaderUtil.isDataURI(absoluteURI)) {
            // Check if the image is currently loaded
            synchronized (cache) {
                if (cache.get(absoluteURI) == null) {
                    // Start loading the image now so that it is ready when
                    // we need it for rendering.
                    loadingQueue.invokeLater
                        (new ImageLoadRunnable(absoluteURI), null);
                }
            }
        }
    
public voidremoveRasterImageConsumer(java.lang.String absoluteURI, RasterImageConsumer imageNode)
In cases where the ImageLoader may update the images associated to a URI, RasterImageConsumer interested in updates need to de-register their interest throught this method.

param
absoluteURI the URI the RasterImageConsumer is interested in.
param
imageNode the RasterImageConsumer interested in the URI.

    
public java.lang.StringresolveURI(java.lang.String uri, java.lang.String baseURI)
Resolves the input relative and base URI into an absolute URI which can be used in subsequent calls to needsURI, getImageAndWait or getImageLater calls.

param
uri the requested URI content.
param
baseURI the base URI. Needed in case uri is relative.
return
the resolved URI that should be requested in follow on needsURI, getImageAndWait or getImageLater calls or null if the URI cannot be resolved.

        if (uri == null) {
            return null;
        }

        // Do not load base64 images as we do not want to 
        // store the base64 string in the cache, because it
        // might be huge.
        if (loaderUtil.isDataURI(uri)) {
            return uri;
        }

        try {
            return URLResolver.resolve(baseURI, uri);
        } catch (IllegalArgumentException iae) {
            return null;
        }
    
public voidwaitForAll()
Utility method. Used to wait until all pending load operations are complete.

throws
InterruptedException if the thread is interrupted while waiting in this method call.

        loadingQueue.invokeAndWait(new Runnable() { public void run() { } }, 
                                   null);