01: /*
02: * @(#)openURL.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.io;
10:
11: import java.net.URL;
12: import java.io.File;
13: import java.io.IOException;
14: import pnuts.lang.Context;
15: import pnuts.lang.Runtime;
16: import pnuts.lang.PnutsFunction;
17: import pnuts.lang.PnutsException;
18:
19: public class openURL extends PnutsFunction {
20:
21: public openURL() {
22: super ("openURL");
23: }
24:
25: public boolean defined(int narg) {
26: return (narg == 1);
27: }
28:
29: static URL getURL(Object input) throws IOException {
30: if (input instanceof URL) {
31: return (URL) input;
32: } else if (input instanceof File) {
33: return Runtime.fileToURL((File) input);
34: } else if (input instanceof String) {
35: return new URL((String) input);
36: } else {
37: throw new IllegalArgumentException(String.valueOf(input));
38: }
39: }
40:
41: protected Object exec(Object[] args, Context context) {
42: if (args.length == 1) {
43: try {
44: return getURL(args[0]).openStream();
45: } catch (IOException e) {
46: throw new PnutsException(e, context);
47: }
48: } else {
49: undefined(args, context);
50: return null;
51: }
52: }
53:
54: public String toString() {
55: return "function openURL(URL|String|File)";
56: }
57: }
|