001: /*
002: * TclTemplateChannel.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: cstevens.
018: * Portions created by cstevens are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.4
024: * Created by cstevens on 99/10/19
025: * Last modified by suhler on 00/05/31 13:52:17
026: */
027:
028: package tcl.lang;
029:
030: import sunlabs.brazil.template.RewriteContext;
031:
032: import java.io.IOException;
033:
034: /**
035: * This class is used to replace the stdout for the Tcl Interp.
036: * Everything written to stdout will instead be appended to the HTML
037: * document.
038: * <p>
039: * This class is in the <code>tcl.lang</code> package because
040: * <code>tcl.lang.Channel</code> and <code>tcl.lang.TclIO</code>
041: * aren't public but we need to access those classes to define new
042: * channels.
043: *
044: * @author Colin Stevens (colin.stevens@sun.com)
045: * @version 1.4, 00/05/31
046: */
047: public class TclTemplateChannel extends Channel {
048: RewriteContext hr;
049: Interp interp;
050:
051: /**
052: * Creates a replacement for stdout in this interp.
053: * <p>
054: * For each call to this procedure, there should eventually be a call
055: * to <code>unregister</code> to restore the original stdout for this
056: * interp and to release the resources associated with the given
057: * <code>RewriteContext</code>.
058: *
059: * @param interp
060: * The interp in which to replace stdout.
061: *
062: * @param hr
063: * The RewriteContext to which all data written to stdout will
064: * be appended.
065: */
066: public TclTemplateChannel(Interp interp, RewriteContext hr) {
067: this .interp = interp;
068: this .hr = hr;
069:
070: /*
071: * Unlike the standard C implementation of Tcl, you cannot actually
072: * close "stdout" and then open another channel which will become the
073: * new "stdout", because in JACL registering a channel does not
074: * automatically take over the next available closed standard stream.
075: * However, if you register a new channel called "file1", it does
076: * replace the standard "stdout".
077: */
078:
079: setChanName("file1");
080: TclIO.registerChannel(interp, this );
081: }
082:
083: /**
084: * Removes this channel from the interp. After calling this, the
085: * original stdout for this interp will be restored.
086: */
087: public void unregister() {
088: TclIO.registerChannel(interp, TclIO
089: .getStdChannel(StdChannel.STDOUT));
090: }
091:
092: public String read(Interp interp, int type, int numBytes)
093: throws IOException, TclException {
094: throw new TclException(interp, "channel \"" + getChanName()
095: + "\" wasn't opened for reading");
096: }
097:
098: public void write(Interp interp, String s) throws IOException,
099: TclException {
100: hr.append(s);
101: }
102:
103: public void close() throws IOException {
104: }
105:
106: public void flush(Interp interp) throws IOException, TclException {
107: }
108:
109: public void seek(long offset, int mode) throws IOException {
110: }
111:
112: public long tell() throws IOException {
113: return ((long) -1);
114: }
115:
116: public boolean eof() {
117: return true;
118: }
119: }
|