Java Source Code: org.pnuts.io.open


   1: /*
   2:  * @(#)open.java 1.2 04/12/06
   3:  *
   4:  * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
   5:  *
   6:  * See the file "LICENSE.txt" for information on usage and redistribution
   7:  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
   8:  */
   9: package org.pnuts.io;
  10: 
  11: import pnuts.lang.Context;
  12: import pnuts.lang.PnutsFunction;
  13: import pnuts.lang.PnutsException;
  14: import org.pnuts.lib.PathHelper;
  15: import java.io.InputStream;
  16: import java.io.OutputStream;
  17: import java.io.BufferedInputStream;
  18: import java.io.BufferedOutputStream;
  19: import java.io.IOException;
  20: import java.io.FileInputStream;
  21: import java.io.FileOutputStream;
  22: import java.io.File;
  23: import java.net.URL;
  24: import java.net.URLConnection;
  25: 
  26: /*
  27:  * function open(input {, mode })
  28:  */
  29:	  public class open extends PnutsFunction {
  30:
  31:	      public open(){
  32:        super("open");
  33:    }
  34:
  35:	      public boolean defined(int nargs){
  36:        return nargs == 1 || nargs == 2;
  37:    }
  38:
  39:	      static File getFile(Object input, Context context){
  40:        File file;
  41:	          if (input instanceof String){
  42:            file = PathHelper.getFile((String)input, context);
  43:        } else if (input instanceof File){
  44:            file = (File)input;
  45:        } else {
  46:            throw new IllegalArgumentException(String.valueOf(input));
  47:        }
  48:	          if (file.getPath().length() == 0){
  49:            throw new PnutsException("pnuts.io.errors", "empty.fileName", new Object[]{}, context);
  50:        }
  51:        return file;
  52:    }
  53:    
  54:	      protected Object exec(Object[] args, Context context){
  55:        int nargs = args.length;
  56:	          try {
  57:	              if (nargs == 1){
  58:                Object arg0 = args[0];
  59:	                  if (arg0 instanceof InputStream){
  60:                    return arg0;
  61:                } else if (arg0 instanceof URL){
  62:                    return ((URL)arg0).openStream();
  63:                } else {
  64:                    File file = getFile(args[0], context);
  65:                    return new BufferedInputStream(new FileInputStream(file));
  66:                }
  67:            } else if (nargs == 2){
  68:                Object input = args[0];
  69:                Object mode = args[1];
  70:	                  if ("r".equals(mode) || "R".equals(mode)){
  71:	                      if (input instanceof InputStream){
  72:                        return input;
  73:                    } else if (input instanceof URL){
  74:                        return ((URL)input).openStream();
  75:                    } else {
  76:                        File file = getFile(input, context);
  77:                        return new BufferedInputStream(new FileInputStream(file));
  78:                    }
  79:                } else if ("w".equals(mode) || "W".equals(mode)){
  80:	                      if (input instanceof OutputStream){
  81:                        return input;
  82:                    } else if (input instanceof URL){
  83:                        URLConnection con = ((URL)input).openConnection();
  84:                        con.setDoOutput(true);
  85:                        return con.getOutputStream();
  86:                    } else {
  87:                        File file = getFile(input, context);
  88:                        PathHelper.ensureBaseDirectory(file);
  89:                        return new BufferedOutputStream(new FileOutputStream(file));
  90:                    }
  91:                } else if ("a".equals(mode) || "A".equals(mode)){
  92:	                      if (input instanceof URL){
  93:                        throw new IllegalArgumentException(String.valueOf(input));
  94:                    } else {
  95:                        File file = getFile(input, context);
  96:                        PathHelper.ensureBaseDirectory(file);
  97:                        return new BufferedOutputStream(new FileOutputStream(file.toString(), true));
  98:                    }
  99:                } else {
 100:                    throw new IllegalArgumentException(String.valueOf(mode));
 101:                }
 102:            } else {
 103:                undefined(args, context);
 104:                return null;
 105:            }
 106:        } catch (IOException e){
 107:            throw new PnutsException(e, context);
 108:        }
 109:    }
 110:
 111:	      public String toString(){
 112:        return "function open((String|File|URL) {, (\"r\"|\"w\"|\"a\") } )";
 113:    }
 114:}