home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / net / remoteurl.java < prev   
Encoding:
Java Source  |  1996-08-14  |  5.9 KB  |  219 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)RemoteURL.java  0.00 rphall
  15.  
  16.     A RemoteURL is a URL stored at a URL.
  17.  
  18.     Authors:
  19.  
  20.     rphall          Rick Hall
  21.     jlee            James Lee
  22.  
  23.     Version Ident:
  24.     $Header$
  25.  
  26.     History:
  27.  
  28.     16-Jan-1996 rphall     Initial Creation
  29.     21-Mar-1996 jlee       Modified getURL so that it can process URL's title and URL at the same time.
  30.  
  31. ---------------------------------------------------------------------------*/
  32.  
  33. package pj.net;
  34.  
  35. import pj.net.RemoteString;
  36.  
  37. import java.net.MalformedURLException;
  38. import java.net.URL;
  39. import java.util.StringTokenizer;
  40. import java.util.NoSuchElementException;
  41.  
  42. /**
  43.  * A RemoteURL is a URL stored at a URL.  The source
  44.  * implementation assumes the URL is stored in a file
  45.  * and represented as an ANSI string.
  46.  * <P>
  47.  * This class is totally unrelated to RemoteObject and subclasses
  48.  * defined in the sun.tools.debug package.
  49.  * <P>
  50.  * This class is related to RemoteImage and RemoteString defined
  51.  * in this package (pj.net).
  52.  *
  53.  * @see pj.net.RemoteImage
  54.  * @see pj.net.RemoteString
  55.  * @version 0.00 16-Jan-1996
  56.  * @author Rick Hall
  57. */
  58. public class RemoteURL
  59.     {
  60.  
  61.     // --- Instance variables
  62.  
  63.     private final int GETURL = 0;
  64.     private final int GETURLTITLE = 1;
  65.  
  66.     
  67.     /**
  68.      * The source URL from which a string representation of a URL is loaded.
  69.     */
  70.     private URL urlSource;
  71.  
  72.     /**
  73.      * A URL resolved from the source url.
  74.      * May be null if the source url or its contents are invalid.
  75.     */
  76.     private URL urlResolved;
  77.  
  78.  
  79.     /**
  80.      * A URL title resolved from the source url.
  81.      * May be null if the source url or its contents are invalid.
  82.     */
  83.     private String urlTitleResolved;
  84.     // --- Public constructors
  85.  
  86.     /**
  87.      * Constructs an RemoteURL based on the specified source URL.
  88.     */
  89.     public RemoteURL(URL u)
  90.         {
  91.         urlSource = u;
  92.         urlResolved = null;
  93.         urlTitleResolved = null;
  94.         }
  95.  
  96.     // --- Public operations
  97.  
  98.     /**
  99.      * @return The source URL of this RemoteURL.
  100.     */
  101.     public URL getSourceURL()
  102.         { 
  103.         return urlSource; 
  104.         }
  105.  
  106.     /**
  107.      * Get a URL resolved from a source url.
  108.      * <P>
  109.      * This operation does not automatically force a new resolution.
  110.      * If the current URL is not null, the current URL is returned.
  111.      * If the current URL is null, this operation resolves a new URL from
  112.      * the source URL.  The returned URL is null if the source URL
  113.      * or its contents are invalid.
  114.      * @return A resolved URL, or null if the source url or its contents
  115.      * are invalid.
  116.     */
  117.     public URL getURL()
  118.         {
  119.         if (urlResolved == null)
  120.             urlResolved = (URL)resolveURL( GETURL );
  121.  
  122.         return urlResolved;
  123.         }
  124.  
  125.     public String getURLTitle()
  126.         {
  127.         if (urlTitleResolved == null)
  128.             urlTitleResolved = (String)resolveURL( GETURLTITLE );
  129.  
  130.         return urlTitleResolved;
  131.         }
  132.  
  133.     // --- Private operations
  134.  
  135.     /**
  136.      * Resolve a URL from the source url.
  137.      * The URL data is loaded immediately.
  138.      * This operation blocks until the url string is finished loading.
  139.      * @return May be null if url or its contents are invalid;
  140.     */
  141.     private Object resolveURL( int type )
  142.         {
  143.         URL     urlMoreAd = null;
  144.         String  strURLTitle = null;
  145.  
  146.         try {
  147.             switch ( type )
  148.                 {
  149.                 case GETURL:
  150.                         urlMoreAd = (URL)getURLData(urlSource, type);
  151.                     return urlMoreAd;
  152.                 case GETURLTITLE:
  153.                     strURLTitle = (String)getURLData(urlSource, type);
  154.                     return strURLTitle;
  155.                 default:
  156.                     break;
  157.                 }
  158.             }
  159.         catch(MalformedURLException e)
  160.             {
  161.             System.out.println("RemoteURL resolveURL, exception = " + e.toString());
  162.             }
  163.  
  164.         return null;
  165.         }
  166.  
  167.     private Object getURLData(URL u, int type) throws MalformedURLException
  168.         {
  169.         URL             u_ = null;
  170.         String          strURLTitle = null;
  171.         String          strURL = null;
  172.         RemoteString    rs = new RemoteString(u);
  173.         String          s = rs.getString();
  174.         StringTokenizer st = new StringTokenizer( s, "\r\n" );
  175.  
  176.         try {
  177.             strURLTitle = st.nextToken();
  178.             }
  179.         catch (NoSuchElementException e)
  180.             {
  181.             return null;
  182.             }
  183.  
  184.         try {
  185.             strURL = st.nextToken();
  186.             }
  187.         catch (NoSuchElementException e)
  188.             {
  189.             strURL = strURLTitle;
  190.             }
  191.  
  192.  
  193.         switch ( type )
  194.             {
  195.             case GETURL:
  196.                 try {
  197.                     u_ = new URL( strURL );
  198.                     }
  199.                 catch ( MalformedURLException e )
  200.                     {
  201.                     System.out.println("Debug-RemoteURL-getURLData:MalformedURLException" );
  202.                     return null;
  203.                     }
  204.                 return u_;
  205.  
  206.             case GETURLTITLE:
  207.                 return strURLTitle;
  208.  
  209.             default:
  210.                 break;
  211.             }
  212.  
  213.         return null;
  214.         } // getURLData
  215.  
  216.     
  217.  
  218.     } // RemoteURL
  219.