001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.vfs.WriteStream;
032:
033: import java.io.IOException;
034:
035: /**
036: * JavaScript object
037: */
038: class NativeFile extends Native {
039: static ESId IN = ESId.intern("in");
040: static ESId OUT = ESId.intern("out");
041:
042: static final int WRITE = 2;
043: static final int WRITELN = 3;
044: static final int FLUSH = 4;
045: static final int CLOSE = 5;
046:
047: private NativeFile(String name, int n, int len) {
048: super (name, len);
049:
050: this .n = n;
051: }
052:
053: static void create(Global resin) {
054: put(resin, "write", WRITE, 0, DONT_ENUM);
055: put(resin, "writeln", WRITELN, 0, DONT_ENUM);
056: put(resin, "flush", FLUSH, 0, DONT_ENUM);
057: put(resin, "close", CLOSE, 0, DONT_ENUM);
058: }
059:
060: private static void put(Global resin, String name, int n, int len,
061: int flags) {
062: ESId id = ESId.intern(name);
063:
064: resin.addProperty(id, new NativeFile(name, n, len));
065: }
066:
067: public ESBase call(Call eval, int length) throws Throwable {
068: ESBase evalThis = eval.getArg(-1);
069: WriteStream stream = null;
070:
071: try {
072: stream = (WriteStream) evalThis.toJavaObject();
073: } catch (Exception e) {
074: }
075:
076: if (stream == null) {
077: ESBase out = evalThis.hasProperty(OUT);
078:
079: if (out != null) {
080: eval.setThis(out);
081: return out.call(eval, length, id);
082: }
083: }
084:
085: switch (n) {
086: case WRITE:
087: return write(eval, length);
088:
089: case WRITELN:
090: return writeln(eval, length);
091:
092: case FLUSH:
093: return flush(eval, length);
094:
095: case CLOSE:
096: return close(eval, length);
097:
098: default:
099: throw new ESException("Unknown file function");
100: }
101: }
102:
103: private static WriteStream getWriteStream(Call eval)
104: throws Throwable {
105: ESBase evalThis = eval.getArg(-1);
106: WriteStream stream = null;
107:
108: try {
109: stream = (WriteStream) evalThis.toJavaObject();
110: } catch (Exception e) {
111: }
112:
113: if (stream != null)
114: return stream;
115:
116: ESBase obj = evalThis.hasProperty(OUT);
117:
118: try {
119: stream = (WriteStream) obj.toJavaObject();
120: } catch (Exception e) {
121: }
122:
123: obj = Global.getGlobalProto().getGlobal().hasProperty(OUT);
124:
125: try {
126: stream = (WriteStream) obj.toJavaObject();
127: } catch (Exception e) {
128: }
129:
130: if (stream == null)
131: throw new ESException(
132: "expected file as `this' or as value of `" + OUT
133: + "'");
134:
135: return stream;
136: }
137:
138: static public ESBase write(Call eval, int length) throws Throwable {
139: WriteStream stream = getWriteStream(eval);
140:
141: try {
142: for (int i = 0; i < length; i++)
143: stream.print(eval.getArg(i).toString());
144: } catch (IOException e) {
145: return ESBoolean.FALSE;
146: }
147:
148: return eval.getArg(-1);
149: }
150:
151: static public ESBase writeln(Call eval, int length)
152: throws Throwable {
153: WriteStream stream = getWriteStream(eval);
154:
155: try {
156: for (int i = 0; i < length; i++)
157: stream.print(eval.getArg(i).toString());
158: stream.println();
159: } catch (IOException e) {
160: return ESBoolean.FALSE;
161: }
162:
163: return eval.getArg(-1);
164: }
165:
166: static public ESBase flush(Call eval, int length) throws Throwable {
167: WriteStream stream = getWriteStream(eval);
168:
169: try {
170: stream.flush();
171: } catch (IOException e) {
172: return ESBoolean.FALSE;
173: }
174:
175: return eval.getArg(-1);
176: }
177:
178: static public ESBase close(Call eval, int length) throws Throwable {
179: WriteStream stream = getWriteStream(eval);
180:
181: try {
182: stream.close();
183: } catch (IOException e) {
184: return ESBoolean.FALSE;
185: }
186:
187: return eval.getArg(-1);
188: }
189: }
|