KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > StdoutWrapper


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.io.OutputStream JavaDoc;
5 import java.io.Writer JavaDoc;
6
7 public class StdoutWrapper extends OutputStream JavaDoc
8 {
9     protected String JavaDoc name;
10
11     public StdoutWrapper() {
12         name = "stdout";
13     }
14
15     protected PyObject getObject(PySystemState ss) {
16         return ss.stdout;
17     }
18     protected void setObject(PySystemState ss, PyObject obj) {
19         ss.stdout = obj;
20     }
21
22     protected PyObject myFile() {
23         PySystemState ss = Py.getSystemState();
24         PyObject obj = getObject(ss);
25         if (obj == null) {
26             throw Py.AttributeError("missing sys."+name);
27         }
28         if (obj instanceof PyJavaInstance) {
29             PyFile f = null;
30
31             Object JavaDoc tmp = obj.__tojava__(OutputStream JavaDoc.class);
32             if ((tmp != Py.NoConversion) && (tmp != null)) {
33                 OutputStream JavaDoc os = (OutputStream JavaDoc)tmp;
34                 f = new PyFile(os, "<java OutputStream>");
35             } else {
36                 tmp = obj.__tojava__(Writer JavaDoc.class);
37                 if ((tmp != Py.NoConversion) && (tmp != null)) {
38                     Writer JavaDoc w = (Writer JavaDoc)tmp;
39                     f = new PyFile(w, "<java Writer>");
40                 }
41             }
42             if (f != null) {
43                 setObject(ss, f);
44                 return f;
45             }
46         }
47         return obj;
48     }
49
50     public void flush() {
51         PyObject obj = myFile();
52         if (obj instanceof PyFile) {
53             ((PyFile)obj).flush();
54         } else {
55             obj.invoke("flush");
56         }
57     }
58
59     public void write(String JavaDoc s) {
60         PyObject obj = myFile();
61
62         if (obj instanceof PyFile) {
63             ((PyFile)obj).write(s);
64         } else {
65             obj.invoke("write", new PyString(s));
66         }
67     }
68
69
70     public void write(int i) {
71         write(new String JavaDoc(new char[] {(char)i}));
72     }
73
74     public void write(byte[] data, int off, int len) {
75         write(new String JavaDoc(data, off, len));
76     }
77
78
79     public void clearSoftspace() {
80         PyObject obj = myFile();
81
82         if (obj instanceof PyFile) {
83             PyFile file = (PyFile)obj;
84             if (file.softspace) {
85                 file.write("\n");
86                 file.flush();
87             }
88             file.softspace = false;
89         } else {
90             PyObject ss = obj.__findattr__("softspace");
91             if (ss != null && ss.__nonzero__()) {
92                 obj.invoke("write", Py.Newline);
93             }
94             obj.invoke("flush");
95             obj.__setattr__("softspace", Py.Zero);
96         }
97     }
98
99     public void print(PyObject o, boolean space, boolean newline) {
100         PyString string = o.__str__();
101         PyObject obj = myFile();
102
103         if (obj instanceof PyFile) {
104             PyFile file = (PyFile)obj;
105             String JavaDoc s = string.toString();
106             if (newline)
107                 s = s+"\n";
108             if (file.softspace)
109                 s = " "+s;
110             file.write(s);
111             file.flush();
112             if (space && s.endsWith("\n"))
113                 space = false;
114             file.softspace = space;
115         } else {
116             PyObject ss = obj.__findattr__("softspace");
117             if (ss != null && ss.__nonzero__()) {
118                 obj.invoke("write", Py.Space);
119             }
120             obj.invoke("write", string);
121             if (newline)
122                 obj.invoke("write", Py.Newline);
123 // obj.invoke("flush");
124

125             if (space && string.toString().endsWith("\n"))
126                 space = false;
127             obj.__setattr__("softspace", space ? Py.One : Py.Zero);
128         }
129     }
130
131
132     public void print(String JavaDoc s) {
133         print(new PyString(s), false, false);
134     }
135
136     public void println(String JavaDoc s) {
137         print(new PyString(s), false, true);
138     }
139
140     public void print(PyObject o) {
141         print(o, false, false);
142     }
143
144     public void printComma(PyObject o) {
145         print(o, true, false);
146     }
147
148     public void println(PyObject o) {
149         print(o, false, true);
150     }
151
152     public void println() {
153         PyObject obj = myFile();
154
155         if (obj instanceof PyFile) {
156             PyFile file = (PyFile)obj;
157             file.write("\n");
158             file.flush();
159             file.softspace = false;
160         }
161         else {
162             obj.invoke("write", Py.Newline);
163             obj.__setattr__("softspace", Py.Zero);
164         }
165     }
166 }
167
Popular Tags