001: /*
002: * $Id: IVUrl.java,v 1.9 2002/07/15 02:15:03 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000-2007 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.url;
052:
053: import java.io.*;
054: import java.net.*;
055: import java.util.*;
056: import org.openlaszlo.iv.flash.api.*;
057: import org.openlaszlo.iv.flash.util.*;
058:
059: /**
060: * Abstract generator url
061: * <p>
062: * Usage example:<br>
063: * <pre><code>
064: * // creates url to file 'data.txt' in the same directory where 'file' is
065: * IVUrl url = IVUrl.newUrl( "data.txt", file );
066: *
067: * // creates url to http://server/getdata.cgi
068: * IVUrl url = IVUrl.newUrl( "http://server/getdata.cgi?parm=text" );
069: *
070: * </code></pre>
071: */
072: public abstract class IVUrl {
073:
074: public static final String ENCODING = "genc";
075: //private static IVVector handlers = new IVVector();
076:
077: protected Hashtable parms;
078:
079: /**
080: * Creates new url from specified string
081: *
082: * @param surl absolute url
083: * @return generator url
084: * @exception IVException
085: */
086: public static IVUrl newUrl(String surl) throws IVException {
087: return newUrl(surl, null);
088: }
089:
090: /**
091: * Creates new url relative from specified file
092: *
093: * @param surl relative or absolute url
094: * @return generator url
095: * @exception IVException
096: */
097: public static IVUrl newUrl(String surl, FlashFile flashFile)
098: throws IVException {
099: if (surl.startsWith("fgjdbc:///")) {
100: return new JDBCUrl(surl);
101: } else if (surl.startsWith("fgjava:///")) {
102: return new JavaUrl(surl);
103: } else if (surl.startsWith("fgjs:///")) {
104: // return new JSUrl( surl, flashFile );
105: try {
106: return (IVUrl) Util.newInstance(
107: "org.openlaszlo.iv.flash.url.JSUrl",
108: new Class[] { String.class, FlashFile.class },
109: new Object[] { surl, flashFile });
110: } catch (Exception e) {
111: throw new IVException(e);
112: }
113: } else if (surl.startsWith("fgfilter:///")) {
114: return new FilterUrl(surl, flashFile);
115: } else {
116: try {
117: URL url = new URL(surl);
118: return new URLUrl(url);
119: } catch (MalformedURLException e) {
120: return new FileUrl(surl, flashFile);
121: }
122: }
123: }
124:
125: /**
126: * Returns name of the url.
127: *
128: * @return name of this url
129: */
130: public abstract String getName();
131:
132: /**
133: * Returns input stream for this url
134: *
135: * @return url input stream
136: * @exception Exception
137: */
138: public abstract InputStream getInputStream() throws IOException;
139:
140: /**
141: * Returns parameter of the url by name
142: * <p>
143: * Parameters are standard url parameters like 'parm' in: http://server/getdata.cgi?parm=text
144: *
145: * @param name parameter name
146: * @return parameter value or null
147: */
148: public String getParameter(String name) {
149: if (parms == null)
150: return null;
151: return (String) parms.get(name.toLowerCase());
152: }
153:
154: /**
155: * Returns timestamp of last time this url was modified
156: *
157: * @return last modified time
158: */
159: public long lastModified() {
160: return 0;
161: }
162:
163: /**
164: * Refreshes this url. Updates last modified.
165: */
166: public void refresh() {
167: }
168:
169: /**
170: * Returns encoding taken from parameters of this url if it
171: * was specified.
172: *
173: * @return specified in this url encoding or null
174: */
175: public String getEncoding() {
176: return getParameter(ENCODING);
177: }
178:
179: /**
180: * Returns true if this url can provide data in name,value format right away
181: * without parsing input stream. If returns true must implement {@link #getData} too.
182: * @see #getData
183: */
184: public boolean hasDataReady() {
185: return false;
186: }
187:
188: /**
189: * Returns data in name,value format ready to be used.
190: * @see #hasDataReady
191: */
192: public String[][] getData() throws IOException {
193: return null;
194: }
195:
196: public String toString() {
197: return getName();
198: }
199:
200: /**
201: * Returns url reference (the part of url after #)
202: *
203: * @return reference
204: */
205: public String getRef() {
206: return null;
207: }
208:
209: /**
210: * Parses parameters of url-string
211: * <p>
212: * Parameters are pairs: name=value, separated by ampersand
213: *
214: * @param s url
215: */
216: protected void parse(String s) throws IVException {
217: int idx = s.indexOf('?');
218: if (idx >= 0)
219: parse(s, idx);
220: }
221:
222: /**
223: * Parses parameters of url-string begining after the specified index (usually index of '?')
224: * <p>
225: * Parameters are pairs: name=value, separated by ampersand
226: *
227: * @param s url
228: */
229: protected void parse(String s, int idx) {
230: parms = Util.parseUrlParms(s, idx);
231: }
232:
233: /**
234: * Converts specified two-dimensional array of strings into InputStream
235: *
236: * @param data specified array
237: * @return input stream
238: */
239: protected InputStream arrayToStream(String[][] data) {
240: StringBuffer sb = new StringBuffer(data.length * data[0].length
241: * 10);
242: for (int i = 0; i < data.length; i++) {
243: int ilen = data[i].length;
244: for (int j = 0; j < ilen; j++) {
245: String s = data[i][j];
246: if (s == null)
247: sb.append("\"\"");
248: else {
249: sb.append("\"");
250: sb.append(s);
251: sb.append("\"");
252: }
253: if (j + 1 < ilen)
254: sb.append(',');
255: }
256: sb.append('\n');
257: }
258: String str = new String(sb);
259: return new ByteArrayInputStream(str.getBytes());
260: }
261:
262: }
|