001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: import java.io.OutputStream;
005: import java.io.Writer;
006:
007: public class StdoutWrapper extends OutputStream {
008: protected String name;
009:
010: public StdoutWrapper() {
011: this .name = "stdout";
012: }
013:
014: protected PyObject getObject(PySystemState ss) {
015: return ss.stdout;
016: }
017:
018: protected void setObject(PySystemState ss, PyObject obj) {
019: ss.stdout = obj;
020: }
021:
022: protected PyObject myFile() {
023: PySystemState ss = Py.getSystemState();
024: PyObject obj = getObject(ss);
025: if (obj == null) {
026: throw Py.AttributeError("missing sys." + this .name);
027: }
028: if (obj instanceof PyJavaInstance) {
029: PyFile f = null;
030:
031: Object tmp = obj.__tojava__(OutputStream.class);
032: if ((tmp != Py.NoConversion) && (tmp != null)) {
033: OutputStream os = (OutputStream) tmp;
034: f = new PyFile(os, "<java OutputStream>");
035: } else {
036: tmp = obj.__tojava__(Writer.class);
037: if ((tmp != Py.NoConversion) && (tmp != null)) {
038: Writer w = (Writer) tmp;
039: f = new PyFile(w, "<java Writer>");
040: }
041: }
042: if (f != null) {
043: setObject(ss, f);
044: return f;
045: }
046: }
047: return obj;
048: }
049:
050: public void flush() {
051: PyObject obj = myFile();
052: if (obj instanceof PyFile) {
053: ((PyFile) obj).flush();
054: } else {
055: obj.invoke("flush");
056: }
057: }
058:
059: public void write(String s) {
060: PyObject obj = myFile();
061:
062: if (obj instanceof PyFile) {
063: ((PyFile) obj).write(s);
064: } else {
065: obj.invoke("write", new PyString(s));
066: }
067: }
068:
069: public void write(int i) {
070: write(new String(new char[] { (char) i }));
071: }
072:
073: public void write(byte[] data, int off, int len) {
074: write(new String(data, off, len));
075: }
076:
077: public void clearSoftspace() {
078: PyObject obj = myFile();
079:
080: if (obj instanceof PyFile) {
081: PyFile file = (PyFile) obj;
082: if (file.softspace) {
083: file.write("\n");
084: file.flush();
085: }
086: file.softspace = false;
087: } else {
088: PyObject ss = obj.__findattr__("softspace");
089: if (ss != null && ss.__nonzero__()) {
090: obj.invoke("write", Py.Newline);
091: }
092: obj.invoke("flush");
093: obj.__setattr__("softspace", Py.Zero);
094: }
095: }
096:
097: public void print(PyObject o, boolean space, boolean newline) {
098: PyString string = o.__str__();
099: PyObject obj = myFile();
100:
101: if (obj instanceof PyFile) {
102: PyFile file = (PyFile) obj;
103: String s = string.toString();
104: if (newline) {
105: s = s + "\n";
106: }
107: if (file.softspace) {
108: s = " " + s;
109: }
110: file.write(s);
111: file.flush();
112: if (space && s.endsWith("\n")) {
113: space = false;
114: }
115: file.softspace = space;
116: } else {
117: PyObject ss = obj.__findattr__("softspace");
118: if (ss != null && ss.__nonzero__()) {
119: obj.invoke("write", Py.Space);
120: }
121: obj.invoke("write", string);
122: if (newline) {
123: obj.invoke("write", Py.Newline);
124: }
125: // obj.invoke("flush");
126:
127: if (space && string.toString().endsWith("\n")) {
128: space = false;
129: }
130: obj.__setattr__("softspace", space ? Py.One : Py.Zero);
131: }
132: }
133:
134: public void print(String s) {
135: print(new PyString(s), false, false);
136: }
137:
138: public void println(String s) {
139: print(new PyString(s), false, true);
140: }
141:
142: public void print(PyObject o) {
143: print(o, false, false);
144: }
145:
146: public void printComma(PyObject o) {
147: print(o, true, false);
148: }
149:
150: public void println(PyObject o) {
151: print(o, false, true);
152: }
153:
154: public void println() {
155: PyObject obj = myFile();
156:
157: if (obj instanceof PyFile) {
158: PyFile file = (PyFile) obj;
159: file.write("\n");
160: file.flush();
161: file.softspace = false;
162: } else {
163: obj.invoke("write", Py.Newline);
164: obj.__setattr__("softspace", Py.Zero);
165: }
166: }
167: }
|