KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > url > IVUrl


1 /*
2  * $Id: IVUrl.java,v 1.9 2002/07/15 02:15:03 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.url;
52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import org.openlaszlo.iv.flash.api.*;
57 import org.openlaszlo.iv.flash.util.*;
58
59 /**
60  * Abstract generator url
61  * <p>
62  * Usage example:<br>
63  * <pre><code>
64  * // creates url to file 'data.txt' in the same directory where 'file' is
65  * IVUrl url = IVUrl.newUrl( "data.txt", file );
66  *
67  * // creates url to http://server/getdata.cgi
68  * IVUrl url = IVUrl.newUrl( "http://server/getdata.cgi?parm=text" );
69  *
70  * </code></pre>
71  */

72 public abstract class IVUrl {
73
74     public static final String JavaDoc ENCODING = "genc";
75     //private static IVVector handlers = new IVVector();
76

77     protected Hashtable parms;
78
79     /**
80      * Creates new url from specified string
81      *
82      * @param surl absolute url
83      * @return generator url
84      * @exception IVException
85      */

86     public static IVUrl newUrl( String JavaDoc surl ) throws IVException {
87         return newUrl( surl, null );
88     }
89
90     /**
91      * Creates new url relative from specified file
92      *
93      * @param surl relative or absolute url
94      * @return generator url
95      * @exception IVException
96      */

97     public static IVUrl newUrl( String JavaDoc surl, FlashFile flashFile ) throws IVException {
98         if( surl.startsWith("fgjdbc:///") ) {
99             return new JDBCUrl( surl );
100         } else if( surl.startsWith("fgjava:///") ) {
101             return new JavaUrl( surl );
102         } else if( surl.startsWith("fgjs:///") ) {
103             // return new JSUrl( surl, flashFile );
104
try {
105                 return (IVUrl) Util.newInstance(
106                                 "org.openlaszlo.iv.flash.url.JSUrl",
107                                 new Class JavaDoc[] {String JavaDoc.class, FlashFile.class},
108                                 new Object JavaDoc[] {surl, flashFile});
109             } catch( Exception JavaDoc e ) {
110                 throw new IVException(e);
111             }
112         } else if( surl.startsWith("fgfilter:///") ) {
113             return new FilterUrl( surl, flashFile );
114         } else if ( surl.startsWith("ftp://") || surl.startsWith("fgftp://") ) {
115             try {
116                 return (IVUrl) Util.newInstance(
117                                "org.openlaszlo.iv.flash.url.FTPUrl",
118                                 new Class JavaDoc[] { String JavaDoc.class, Boolean JavaDoc.class },
119                                 new Object JavaDoc[] { surl, new Boolean JavaDoc(surl.startsWith("fgftp://")) });
120             } catch( Exception JavaDoc e ) {
121                 throw new IVException(e);
122             }
123         } else {
124             try {
125                 URL url = new URL(surl);
126                 return new URLUrl( url );
127             } catch( MalformedURLException e ) {
128                 return new FileUrl( surl, flashFile );
129             }
130         }
131     }
132
133     /**
134      * Returns name of the url.
135      *
136      * @return name of this url
137      */

138     public abstract String JavaDoc getName();
139
140     /**
141      * Returns input stream for this url
142      *
143      * @return url input stream
144      * @exception Exception
145      */

146     public abstract InputStream getInputStream() throws IOException;
147
148     /**
149      * Returns parameter of the url by name
150      * <p>
151      * Parameters are standard url parameters like 'parm' in: http://server/getdata.cgi?parm=text
152      *
153      * @param name parameter name
154      * @return parameter value or null
155      */

156     public String JavaDoc getParameter( String JavaDoc name ) {
157         if( parms == null ) return null;
158         return (String JavaDoc) parms.get(name.toLowerCase());
159     }
160
161     /**
162      * Returns timestamp of last time this url was modified
163      *
164      * @return last modified time
165      */

166     public long lastModified() {
167         return 0;
168     }
169
170     /**
171      * Refreshes this url. Updates last modified.
172      */

173     public void refresh() {
174     }
175
176     /**
177      * Returns encoding taken from parameters of this url if it
178      * was specified.
179      *
180      * @return specified in this url encoding or null
181      */

182     public String JavaDoc getEncoding() {
183         return getParameter(ENCODING);
184     }
185
186     /**
187      * Returns true if this url can provide data in name,value format right away
188      * without parsing input stream. If returns true must implement {@link #getData} too.
189      * @see #getData
190      */

191     public boolean hasDataReady() {
192         return false;
193     }
194
195     /**
196      * Returns data in name,value format ready to be used.
197      * @see #hasDataReady
198      */

199     public String JavaDoc[][] getData() throws IOException {
200         return null;
201     }
202
203     public String JavaDoc toString() {
204         return getName();
205     }
206
207     /**
208      * Returns url reference (the part of url after #)
209      *
210      * @return reference
211      */

212     public String JavaDoc getRef() {
213         return null;
214     }
215
216     /**
217      * Parses parameters of url-string
218      * <p>
219      * Parameters are pairs: name=value, separated by ampersand
220      *
221      * @param s url
222      */

223     protected void parse( String JavaDoc s ) throws IVException {
224         int idx = s.indexOf('?');
225         if( idx >= 0 ) parse(s, idx);
226     }
227
228     /**
229      * Parses parameters of url-string begining after the specified index (usually index of '?')
230      * <p>
231      * Parameters are pairs: name=value, separated by ampersand
232      *
233      * @param s url
234      */

235     protected void parse( String JavaDoc s, int idx ) {
236         parms = Util.parseUrlParms(s, idx);
237     }
238
239     /**
240      * Converts specified two-dimensional array of strings into InputStream
241      *
242      * @param data specified array
243      * @return input stream
244      */

245     protected InputStream arrayToStream( String JavaDoc[][] data ) {
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( data.length*data[0].length*10 );
247         for( int i=0; i<data.length; i++ ) {
248             int ilen = data[i].length;
249             for( int j=0; j<ilen; j++ ) {
250                 String JavaDoc s = data[i][j];
251                 if( s == null ) sb.append("\"\"");
252                 else {
253                     sb.append("\"");
254                     sb.append(s);
255                     sb.append("\"");
256                 }
257                 if( j+1 < ilen ) sb.append(',');
258             }
259             sb.append('\n');
260         }
261         String JavaDoc str = new String JavaDoc( sb );
262         return new ByteArrayInputStream( str.getBytes() );
263     }
264
265 }
266
Popular Tags