KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > NativeFile


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.vfs.WriteStream;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * JavaScript object
37  */

38 class NativeFile extends Native {
39   static ESId IN = ESId.intern("in");
40   static ESId OUT = ESId.intern("out");
41
42   static final int WRITE = 2;
43   static final int WRITELN = 3;
44   static final int FLUSH = 4;
45   static final int CLOSE = 5;
46
47   private NativeFile(String JavaDoc name, int n, int len)
48   {
49     super(name, len);
50
51     this.n = n;
52   }
53
54   static void create(Global resin)
55   {
56     put(resin, "write", WRITE, 0, DONT_ENUM);
57     put(resin, "writeln", WRITELN, 0, DONT_ENUM);
58     put(resin, "flush", FLUSH, 0, DONT_ENUM);
59     put(resin, "close", CLOSE, 0, DONT_ENUM);
60   }
61   
62   private static void put(Global resin, String JavaDoc name, int n, int len,
63               int flags)
64   {
65     ESId id = ESId.intern(name);
66
67     resin.addProperty(id, new NativeFile(name, n, len));
68   }
69
70   public ESBase call(Call eval, int length) throws Throwable JavaDoc
71   {
72     ESBase evalThis = eval.getArg(-1);
73     WriteStream stream = null;
74
75     try {
76       stream = (WriteStream) evalThis.toJavaObject();
77     } catch (Exception JavaDoc e) {
78     }
79
80     if (stream == null) {
81       ESBase out = evalThis.hasProperty(OUT);
82
83       if (out != null) {
84         eval.setThis(out);
85         return out.call(eval, length, id);
86       }
87     }
88     
89     switch (n) {
90     case WRITE:
91       return write(eval, length);
92
93     case WRITELN:
94       return writeln(eval, length);
95
96     case FLUSH:
97       return flush(eval, length);
98
99     case CLOSE:
100       return close(eval, length);
101
102     default:
103       throw new ESException("Unknown file function");
104     }
105   }
106
107   private static WriteStream getWriteStream(Call eval) throws Throwable JavaDoc
108   {
109     ESBase evalThis = eval.getArg(-1);
110     WriteStream stream = null;
111
112     try {
113       stream = (WriteStream) evalThis.toJavaObject();
114     } catch (Exception JavaDoc e) {
115     }
116
117     if (stream != null)
118       return stream;
119
120     ESBase obj = evalThis.hasProperty(OUT);
121
122     try {
123       stream = (WriteStream) obj.toJavaObject();
124     } catch (Exception JavaDoc e) {
125     }
126
127     obj = Global.getGlobalProto().getGlobal().hasProperty(OUT);
128
129     try {
130       stream = (WriteStream) obj.toJavaObject();
131     } catch (Exception JavaDoc e) {
132     }
133
134     if (stream == null)
135       throw new ESException("expected file as `this' or as value of `" + OUT + "'");
136
137     return stream;
138   }
139
140   static public ESBase write(Call eval, int length) throws Throwable JavaDoc
141   {
142     WriteStream stream = getWriteStream(eval);
143
144     try {
145       for (int i = 0; i < length; i++)
146     stream.print(eval.getArg(i).toString());
147     } catch (IOException JavaDoc e) {
148       return ESBoolean.FALSE;
149     }
150
151     return eval.getArg(-1);
152   }
153
154   static public ESBase writeln(Call eval, int length) throws Throwable JavaDoc
155   {
156     WriteStream stream = getWriteStream(eval);
157
158     try {
159       for (int i = 0; i < length; i++)
160     stream.print(eval.getArg(i).toString());
161       stream.println();
162     } catch (IOException JavaDoc e) {
163       return ESBoolean.FALSE;
164     }
165
166     return eval.getArg(-1);
167   }
168
169   static public ESBase flush(Call eval, int length) throws Throwable JavaDoc
170   {
171     WriteStream stream = getWriteStream(eval);
172
173     try {
174       stream.flush();
175     } catch (IOException JavaDoc e) {
176       return ESBoolean.FALSE;
177     }
178
179     return eval.getArg(-1);
180   }
181
182   static public ESBase close(Call eval, int length) throws Throwable JavaDoc
183   {
184     WriteStream stream = getWriteStream(eval);
185
186     try {
187       stream.close();
188     } catch (IOException JavaDoc e) {
189       return ESBoolean.FALSE;
190     }
191
192     return eval.getArg(-1);
193   }
194 }
195
Popular Tags