javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/connection/M2GDefaultExternalResourceHandler.java
changeset 80 d6dafc5d983f
parent 56 abc41079b313
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.microedition.m2g.connection;
       
    19 
       
    20 
       
    21 import java.io.*;
       
    22 import javax.microedition.io.*;
       
    23 import javax.microedition.m2g.ExternalResourceHandler;
       
    24 import javax.microedition.m2g.*;
       
    25 
       
    26 
       
    27 /**
       
    28 * <p>This private class is the default resource handler
       
    29 * used to load external resources that are referenced
       
    30 * within a ScalableImage, namely within a SVG document in the case of SVG.
       
    31 * The default resource handler will be used by the platform in the following cases:
       
    32 *   - if the MIDlet has not explicitly set its own handler to load external resources
       
    33 *   - if the resource handler specified by the MIDlet is not able to load the
       
    34 *     external resources (i.e. the MIDlet's SVGResourceHandle.getExternalResource()
       
    35 *     returns null).</p>
       
    36 * @see javax.microedition.m2g.ExternalResourceHandler
       
    37 */
       
    38 public class M2GDefaultExternalResourceHandler implements ExternalResourceHandler
       
    39 {
       
    40 
       
    41     /*
       
    42      * According to RFC 2396, the rules for determening the base URI can be summarized
       
    43      * as follows (highest priority to lowest):
       
    44      * 1.The base URI is embedded in the document's content.
       
    45      * 2.The base URI is that of the encapsulating document.
       
    46      * 3.The base URI is the URI used to retrieve  the entity.
       
    47      * 4.The base URI is defined by the context of the application
       
    48      *
       
    49      * Note that in the case of the M2GDefaultExternalResourceHandler  4. is not
       
    50      * applicable.
       
    51      */
       
    52 
       
    53     //--------------------------------------------------
       
    54     // VARIABLES
       
    55     //--------------------------------------------------
       
    56     private M2GUrlTokenizer iTokenizer = null;
       
    57 
       
    58     //--------------------------------------------------
       
    59     // METHODS
       
    60     //--------------------------------------------------
       
    61     /**
       
    62      * This constructor is used to create a default resource handler that can
       
    63      * load resources from absolute URLs and from URLs that are relative
       
    64      * to the aUrl.
       
    65      * NOTE: This handler will not be allowed to load resources from the application's
       
    66      * jar file.
       
    67      *
       
    68      * @param aUrl The already validated url used to load the svg document
       
    69      */
       
    70     M2GDefaultExternalResourceHandler(String aUrl)
       
    71     {
       
    72         if (aUrl == null || aUrl.equals(""))
       
    73         {
       
    74             iTokenizer = new M2GUrlTokenizer();
       
    75         }
       
    76         else
       
    77         {
       
    78             iTokenizer = new M2GUrlTokenizer(aUrl);
       
    79         }
       
    80     }
       
    81 
       
    82     /**
       
    83      * This constructor is used to create a default resource handler that can
       
    84      * load external resources from fully qualified URLs ONLY. Any relative URL
       
    85      * will be considered to be a link to a resource within the application's jar file.
       
    86      */
       
    87     M2GDefaultExternalResourceHandler()
       
    88     {
       
    89         this(null);
       
    90     }
       
    91 
       
    92     /**
       
    93       * This method is invoked when an external resource is required by the underlying implementation.
       
    94       * When the request is completed by the implementation of this handler, a notification
       
    95       * must be sent to the SVG engine through the requestCompleted() method of <code>ScalableImage</code>.
       
    96       * To get a synchronous behaviour, requestCompleted() can be called in the implementation
       
    97       * of requestResource. If called later, rendering the document before
       
    98       * completing all the requests will just display the currently available content.
       
    99       * Once the request is completed, the image will have to be redrawn to reflect the newly
       
   100       * available data.
       
   101       * For more details on required resources, please refer to the <code>externalResourcesRequired</code>
       
   102       * attribute description in the SVG specification.
       
   103       *
       
   104       * @param image image that originated the external data request
       
   105       * @param URL the an absolute or a relative URL for the external resource + drm mode
       
   106       */
       
   107     public void requestResource(ScalableImage aImage, String aUrl)
       
   108     {
       
   109         M2GConnectionProxy proxy = null;
       
   110 
       
   111         /* IMPLEMENTATION NOTE:
       
   112          * The default resource handler assumes that if there is an embedded base URL within the
       
   113          * document then is upon the svg engine to provide the absolute URL.
       
   114          * If the given URL is a relative URL then this handler will attempt to use as a base URL
       
   115          * to retrieve the containing svg document. However, if the document's base
       
   116          * URL is null then this handler will assume the given relative URL to be a file name
       
   117          * within the application's jar file.
       
   118          */
       
   119         try
       
   120         {
       
   121             // NOTE: if exception is thrown during the resolveExternalResourceUrl() call then
       
   122             // the requestCompleted should be called again with a null input stream.
       
   123             proxy = M2GConnectionFactory.resolveExternalResourceUrl(iTokenizer, aUrl);
       
   124             aImage.requestCompleted(aUrl, proxy.getInputStream());
       
   125         }
       
   126         catch (Exception e)
       
   127         {
       
   128             // e.printStackTrace();
       
   129             // The requestCompleted() failed
       
   130             try
       
   131             {
       
   132                 aImage.requestCompleted(aUrl, null);
       
   133             }
       
   134             catch (Exception ee)
       
   135             {
       
   136                 // ee.printStackTrace();
       
   137             }
       
   138         }
       
   139         // Close streams
       
   140         finally
       
   141         {
       
   142             try
       
   143             {
       
   144                 if (proxy != null)
       
   145                 {
       
   146                     proxy.close();
       
   147                 }
       
   148             }
       
   149             catch (Exception e)
       
   150             {
       
   151                 // e.printStackTrace();
       
   152             }
       
   153         }
       
   154     }
       
   155 }
       
   156 
       
   157