home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.9 KB | 219 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)RemoteURL.java 0.00 rphall
-
- A RemoteURL is a URL stored at a URL.
-
- Authors:
-
- rphall Rick Hall
- jlee James Lee
-
- Version Ident:
- $Header$
-
- History:
-
- 16-Jan-1996 rphall Initial Creation
- 21-Mar-1996 jlee Modified getURL so that it can process URL's title and URL at the same time.
-
- ---------------------------------------------------------------------------*/
-
- package pj.net;
-
- import pj.net.RemoteString;
-
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.StringTokenizer;
- import java.util.NoSuchElementException;
-
- /**
- * A RemoteURL is a URL stored at a URL. The source
- * implementation assumes the URL is stored in a file
- * and represented as an ANSI string.
- * <P>
- * This class is totally unrelated to RemoteObject and subclasses
- * defined in the sun.tools.debug package.
- * <P>
- * This class is related to RemoteImage and RemoteString defined
- * in this package (pj.net).
- *
- * @see pj.net.RemoteImage
- * @see pj.net.RemoteString
- * @version 0.00 16-Jan-1996
- * @author Rick Hall
- */
- public class RemoteURL
- {
-
- // --- Instance variables
-
- private final int GETURL = 0;
- private final int GETURLTITLE = 1;
-
-
- /**
- * The source URL from which a string representation of a URL is loaded.
- */
- private URL urlSource;
-
- /**
- * A URL resolved from the source url.
- * May be null if the source url or its contents are invalid.
- */
- private URL urlResolved;
-
-
- /**
- * A URL title resolved from the source url.
- * May be null if the source url or its contents are invalid.
- */
- private String urlTitleResolved;
- // --- Public constructors
-
- /**
- * Constructs an RemoteURL based on the specified source URL.
- */
- public RemoteURL(URL u)
- {
- urlSource = u;
- urlResolved = null;
- urlTitleResolved = null;
- }
-
- // --- Public operations
-
- /**
- * @return The source URL of this RemoteURL.
- */
- public URL getSourceURL()
- {
- return urlSource;
- }
-
- /**
- * Get a URL resolved from a source url.
- * <P>
- * This operation does not automatically force a new resolution.
- * If the current URL is not null, the current URL is returned.
- * If the current URL is null, this operation resolves a new URL from
- * the source URL. The returned URL is null if the source URL
- * or its contents are invalid.
- * @return A resolved URL, or null if the source url or its contents
- * are invalid.
- */
- public URL getURL()
- {
- if (urlResolved == null)
- urlResolved = (URL)resolveURL( GETURL );
-
- return urlResolved;
- }
-
- public String getURLTitle()
- {
- if (urlTitleResolved == null)
- urlTitleResolved = (String)resolveURL( GETURLTITLE );
-
- return urlTitleResolved;
- }
-
- // --- Private operations
-
- /**
- * Resolve a URL from the source url.
- * The URL data is loaded immediately.
- * This operation blocks until the url string is finished loading.
- * @return May be null if url or its contents are invalid;
- */
- private Object resolveURL( int type )
- {
- URL urlMoreAd = null;
- String strURLTitle = null;
-
- try {
- switch ( type )
- {
- case GETURL:
- urlMoreAd = (URL)getURLData(urlSource, type);
- return urlMoreAd;
- case GETURLTITLE:
- strURLTitle = (String)getURLData(urlSource, type);
- return strURLTitle;
- default:
- break;
- }
- }
- catch(MalformedURLException e)
- {
- System.out.println("RemoteURL resolveURL, exception = " + e.toString());
- }
-
- return null;
- }
-
- private Object getURLData(URL u, int type) throws MalformedURLException
- {
- URL u_ = null;
- String strURLTitle = null;
- String strURL = null;
- RemoteString rs = new RemoteString(u);
- String s = rs.getString();
- StringTokenizer st = new StringTokenizer( s, "\r\n" );
-
- try {
- strURLTitle = st.nextToken();
- }
- catch (NoSuchElementException e)
- {
- return null;
- }
-
- try {
- strURL = st.nextToken();
- }
- catch (NoSuchElementException e)
- {
- strURL = strURLTitle;
- }
-
-
- switch ( type )
- {
- case GETURL:
- try {
- u_ = new URL( strURL );
- }
- catch ( MalformedURLException e )
- {
- System.out.println("Debug-RemoteURL-getURLData:MalformedURLException" );
- return null;
- }
- return u_;
-
- case GETURLTITLE:
- return strURLTitle;
-
- default:
- break;
- }
-
- return null;
- } // getURLData
-
-
-
- } // RemoteURL
-