KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ExtractorURLStreamHandler


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  *
15  * FIXME:
16  */

17
18
19 // ExtractorURLStreamHandler.java
20

21 // Description:
22
// A class to implement a network URL protocol for a self extracting
23
// archive.
24
//
25
// Author:
26
// Peter Pilgrim
27
// Thu Mar 11 00:06:57 GMT 1999
28
//
29
// RCS HEADER
30
//
31
// $Author$
32
// $Date$
33
// $Source$
34
// $Revision$ $State$ $Locker$
35
//
36
// History
37
// ================================================================================
38
// $Log$
39

40
41 import java.io.*; // for File I/O Classes
42
import java.net.*; // Network/Internet classes
43
import java.util.*; // for Vector/Hashtable
44

45
46 /**
47  * A class to handle the URL stream <B>extractor:</B> protocol.
48  * The protocol is the interaction between the client and server
49  * objects.
50  *
51  * The universal resource locator (URL) example
52  *
53  * <B>protocol</B> : &lt;TYPECODE&gt; / + / <I>&lt;FILENAME&gt;</I>
54  *
55  * @author Peter Pilgrim
56  * @version 1.0 , Mon Feb 22 23:07:13 GMT 1999
57  *
58  */

59 public class ExtractorURLStreamHandler extends URLStreamHandler {
60     
61     /**
62      * Opens a connection to the object referenced by the URL
63      * argument. This method should be overridden by a subclass.
64      *
65      * @param u the URL that this stream handler connects to.
66      * @return the <CODE>URLConnection</CODE> object.
67      * @exception IOException if the connection cannot be opened.
68      */

69     public URLConnection openConnection( URL u )
70     throws IOException
71     {
72     return new ExtractorURLConnection( u );
73     }
74
75     /*
76      * Parses the string representation of a <code>URL</code> into a
77      * Extractor <code>URL</code> object.
78      *
79      * For example
80      * <PRE>
81      * extractor:/CFUM99/+/obi/wan/kenobi.gif
82      * </PRE>
83      *
84      * @param u the <code>URL</code> to receive the result of parsing
85      * the spec.
86      *
87      * @param spec the <code>String</code> representing the URL that
88      * must be parsed.
89      *
90      * @param start the character index at which to begin
91      * parsing. This is just past the '<code>:</code>' (if there is
92      * one) that specifies the determination of the protocol name.
93      *
94      * @param limit the character position to stop parsing at. This is
95      * the end of the string or the position of the "<code>#</code>"
96      * character, if present. All information after the sharp sign
97      * indicates an anchor.
98      */

99     protected void parseURL(URL u, String JavaDoc spec, int start, int limit)
100     {
101     System.out.println( "ExtractorURLStreamHandler.parseURL()" );
102
103     String JavaDoc protocol = u.getProtocol();
104     String JavaDoc host = u.getHost();
105     int port = u.getPort();
106     String JavaDoc file = u.getFile();
107     String JavaDoc ref = u.getRef();
108
109     String JavaDoc temp = spec.substring(start);
110     int i = temp.indexOf(":");
111     if ( i >= 0 ) {
112         // Read the protocol and skip the `:'
113
protocol = temp.substring( 0, i+1 );
114         temp = temp.substring( i+1 ) ;
115     }
116
117     start=0;
118     if ( temp.charAt(start) == '/' )
119         ++start;
120     i = temp.indexOf( "/", start );
121     if ( i >= start ) {
122         // Read the host name.
123
host = temp.substring( start, i );
124         start = i+1;
125     }
126
127     // Read the '+' and the next '/'
128
if ( temp.charAt(start) == '+' && temp.charAt(start+1) == '/' ) {
129         // Read the file
130
start += 2;
131         file = temp.substring( start );
132     }
133
134     System.out.println( "protocol:" + protocol );
135     System.out.println( "host:" + host );
136     System.out.println( "file:" + file );
137     setURL( u, protocol, host, port, file, ref );
138     }
139
140     /**
141      * Converts an <CODE>URL</CODE> of the extractor protocol to a
142      * nice readable <CODE>String</CODE>.
143      *
144      * @param u the URL.
145      * @return a string representation of the <code>URL</code> argument.
146      */

147     protected String JavaDoc toExternalForm(URL u) {
148     String JavaDoc result = u.getProtocol() + ":";
149     if ((u.getHost() != null) && (u.getHost().length() > 0)) {
150         result = result + "/" + u.getHost();
151     }
152     result += "/+/" + u.getFile();
153
154     System.out.println( "ExtractorURLStreamHandler.toExternal() result:"+result );
155     return result;
156     }
157
158 }
159
160 // fini
161
Popular Tags