KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > OutPort


1 package gnu.mapping;
2 import java.io.*;
3 import gnu.text.*;
4 import gnu.lists.*;
5
6 /**
7  * An extended PrintWriter.
8  */

9
10 public class OutPort extends PrintConsumer implements Printable
11 {
12   Object JavaDoc name;
13   private Writer base;
14
15   // To keep track of column-numbers, we use a helper class.
16
// Otherwise, it is too painful, as there is no documented
17
// interface that would allow PrintWriter to be cleanly extended ...
18
// The helper class also lets us make transparent use of WriterManager.
19
protected PrettyWriter bout;
20
21   /** An index into the WriterManager's internal table.
22    * The value zero means it is unregistered. */

23   protected int index;
24   
25   protected OutPort(Writer base, PrettyWriter out, boolean autoflush)
26   {
27     super(out, autoflush);
28     this.bout = out;
29     this.base = base;
30     if (closeOnExit())
31       index = WriterManager.instance.register(out);
32   }
33
34   protected OutPort (OutPort out, boolean autoflush)
35   {
36     this(out, out.bout, autoflush);
37   }
38
39   protected OutPort (Writer out, boolean autoflush)
40   {
41     this(out,
42          (out instanceof OutPort ? ((OutPort) out).bout
43           : new PrettyWriter(out, true)),
44          autoflush);
45   }
46
47   public OutPort(Writer base, boolean printPretty, boolean autoflush)
48   {
49     this(base, new PrettyWriter(base, printPretty), autoflush);
50   }
51
52   public OutPort(Writer base, boolean printPretty,
53          boolean autoflush, Object JavaDoc name)
54   {
55     this(base, new PrettyWriter(base, printPretty), autoflush);
56     this.name = name;
57   }
58
59   public OutPort (OutputStream out)
60   {
61     this (out, null);
62   }
63
64   public OutPort (OutputStream out, Object JavaDoc name)
65   {
66     this(new OutputStreamWriter(out), true, name);
67   }
68
69   public OutPort (Writer out)
70   {
71     this(out,
72          out instanceof OutPort ? ((OutPort) out).bout
73          : new PrettyWriter(out, false),
74          false);
75   }
76
77   public OutPort (Writer base, Object JavaDoc name)
78   {
79     this(base, false, false);
80     this.name = name;
81   }
82
83   public OutPort (Writer base, boolean autoflush, Object JavaDoc name)
84   {
85     this (base, false, autoflush);
86     this.name = name;
87   }
88
89   public boolean printReadable;
90
91   static OutPort outInitial = new OutPort (new LogWriter (new BufferedWriter(new OutputStreamWriter(System.out))), true, true, "<stdout>");
92
93   private static OutPort errInitial = new OutPort (new LogWriter(new OutputStreamWriter(System.err)), true, true, "<stderr>");
94
95   public static final ThreadLocation outLocation
96     = new ThreadLocation("out-default");
97   static { outLocation.setGlobal(outInitial); }
98   public static final ThreadLocation errLocation
99     = new ThreadLocation("err-default");
100   static { errLocation.setGlobal(errInitial); }
101   static public OutPort outDefault ()
102   {
103     return (OutPort) outLocation.get();
104   }
105
106   static public void setOutDefault (OutPort o)
107   {
108     outLocation.set(o);
109   }
110
111   static public OutPort errDefault ()
112   {
113     return (OutPort) errLocation.get();
114   }
115
116   static public void setErrDefault (OutPort e)
117   {
118     errLocation.set(e);
119   }
120
121   public static OutPort openFile(Object JavaDoc fname)
122     throws java.io.IOException JavaDoc
123   {
124       Object JavaDoc conv = Environment.user().get("port-char-encoding");
125       java.io.OutputStream JavaDoc strm = URI_utils.getOutputStream(fname);
126       strm = new java.io.BufferedOutputStream JavaDoc(strm);
127       java.io.Writer JavaDoc wr;
128       if (conv == null || conv == Boolean.TRUE)
129     wr = new java.io.OutputStreamWriter JavaDoc(strm);
130       else
131     {
132       if (conv == Boolean.FALSE)
133         conv = "8859_1";
134       wr = new java.io.OutputStreamWriter JavaDoc(strm, conv.toString());
135     }
136       return new OutPort(wr, fname);
137   }
138
139   public void echo (char[] buf, int off, int len) throws java.io.IOException JavaDoc
140   {
141     if (base instanceof LogWriter)
142       ((LogWriter)base).echo(buf, off, len);
143   }
144
145   static Writer logFile;
146
147   public static void closeLogFile () throws java.io.IOException JavaDoc
148   {
149     if (logFile != null)
150       {
151     logFile.close();
152     logFile = null;
153       }
154     if (outInitial.base instanceof LogWriter)
155       ((LogWriter)outInitial.base).setLogFile((Writer) null);
156     if (errInitial.base instanceof LogWriter)
157       ((LogWriter)errInitial.base).setLogFile((Writer) null);
158   }
159
160   public static void setLogFile (String JavaDoc name) throws java.io.IOException JavaDoc
161   {
162     if (logFile != null)
163       closeLogFile();
164     logFile = new PrintWriter(new BufferedWriter(new FileWriter(name)));
165     if (outInitial.base instanceof LogWriter)
166       ((LogWriter)outInitial.base).setLogFile(logFile);
167     if (errInitial.base instanceof LogWriter)
168       ((LogWriter)errInitial.base).setLogFile(logFile);
169   }
170
171   /*
172   public void closeLogFile () throws java.io.IOException
173   {
174     if (base instanceof LogWriter)
175       ((LogWriter)base).closeLogFile();
176   }
177
178   public void setLogFile (String name) throws java.io.IOException
179   {
180     if (base instanceof LogWriter)
181       ((LogWriter)base).setLogFile(name);
182   }
183   */

184
185   protected static final boolean isWordChar(char ch)
186   {
187     return Character.isJavaIdentifierPart(ch) || ch == '-' || ch == '+';
188   }
189
190   // java.text.FieldPosition fieldPosition;
191

192   /** If non-null, use this to print numbers. */
193   java.text.NumberFormat JavaDoc numberFormat;
194
195   public AbstractFormat objectFormat;
196
197   public void print(int v)
198   {
199     if (numberFormat == null)
200       super.print(v);
201     else
202       print(numberFormat.format((long) v));
203   }
204
205   public void print(long v)
206   {
207     if (numberFormat == null)
208       super.print(v);
209     else
210       print(numberFormat.format(v));
211   }
212
213   public void print(double v)
214   {
215     if (numberFormat == null)
216       super.print(v);
217     else
218       print(numberFormat.format(v));
219   }
220
221   public void print(float v)
222   {
223     if (numberFormat == null)
224       super.print(v);
225     else
226       print(numberFormat.format((double) v));
227   }
228
229   public void print(String JavaDoc v)
230   {
231     write(v == null ? "(null)" : v);
232   }
233
234   public void print(Object JavaDoc v)
235   {
236     if (objectFormat != null)
237       objectFormat.writeObject(v, this);
238     else if (v instanceof Consumable)
239       ((Consumable) v).consume(this);
240     else
241       super.print(v == null ? "null" : v);
242   }
243
244   public void print (Consumer out)
245   {
246     out.write("#<output-port");
247     if (name != null)
248       {
249     out.write(' ');
250     out.write(name.toString());
251       }
252     out.write('>');
253   }
254
255   public void beginGroup(Object JavaDoc type)
256   {
257     if (objectFormat != null)
258       objectFormat.beginGroup(type, this);
259     else
260       {
261     print('(');
262     print(type);
263       }
264   }
265
266   public void endGroup ()
267   {
268     if (objectFormat != null)
269       objectFormat.endGroup(this);
270     else
271       print(')');
272   }
273
274   /** Write a attribute for the current group.
275    * This is only allowed immediately after a beginGroup. */

276   public void beginAttribute(Object JavaDoc attrType)
277   {
278     if (objectFormat != null)
279       objectFormat.beginAttribute(attrType, this);
280     else
281       {
282         print(' ');
283         print(attrType);
284         print(": ");
285       }
286   }
287
288   /** No more attributes in this group. */
289   public void endAttribute()
290   {
291     if (objectFormat != null)
292       objectFormat.endAttribute(this);
293     else
294       print(' ');
295   }
296
297   /** Note the end of a "word". See {@link #writeWordStart}. */
298   public void writeWordEnd ()
299   {
300     bout.writeWordEnd();
301   }
302
303   /** Maybe write a word-separating space.
304    * Specifically, write a space if the previous output
305    * was {@link #writeWordEnd}. Otherwise, do nothing.
306    */

307   public void writeWordStart ()
308   {
309     bout.writeWordStart();
310   }
311
312   public void freshLine()
313   {
314     int col = bout.getColumnNumber();
315     if (col != 0)
316       println();
317   }
318
319   public int getColumnNumber ()
320   {
321     return bout.getColumnNumber();
322   }
323
324   public void setColumnNumber (int column)
325   {
326     bout.setColumnNumber(column);
327   }
328
329   public void clearBuffer ()
330   {
331     bout.clearBuffer();
332   }
333
334   public void close()
335   {
336     try
337       {
338         if (base instanceof OutPort && ((OutPort) base).bout == bout)
339           base.close();
340         else
341           out.close();
342       }
343     catch (IOException ex)
344       {
345         setError();
346       }
347     if (index > 0)
348       WriterManager.instance.unregister(index);
349   }
350
351   /** True if the port should be automatically closed on exit.
352    * (If so, it will be registered by WriterManager. */

353   protected boolean closeOnExit ()
354   {
355     return true;
356   }
357
358   public static void runCleanups ()
359   {
360     WriterManager.instance.run();
361   }
362
363   public void startLogicalBlock (String JavaDoc prefix, boolean perLine,
364                  String JavaDoc suffix)
365   {
366     bout.startLogicalBlock(prefix, perLine, suffix);
367   }
368
369   public void startLogicalBlock (String JavaDoc prefix, String JavaDoc suffix, int indent)
370   {
371     bout.startLogicalBlock(prefix, false, suffix);
372     bout.addIndentation(prefix == null ? indent : indent - prefix.length(),
373             false);
374   }
375
376   public void endLogicalBlock (String JavaDoc suffix)
377   {
378     bout.endLogicalBlock(suffix);
379   }
380
381   public void writeBreak(int kind)
382   {
383     bout.writeBreak(kind);
384   }
385
386   public void writeSpaceLinear()
387   {
388     write(' ');
389     writeBreak(PrettyWriter.NEWLINE_LINEAR);
390   }
391
392   /** Write a new-line iff the containing section cannot be printed
393    * on one line. Either all linear-style newlines in a logical
394    * block becomes spaces (if it all fits in a line), or none
395    * of them do. */

396   public void writeBreakLinear()
397   {
398     writeBreak(PrettyWriter.NEWLINE_LINEAR);
399   }
400
401   /** Write a new-line if needed, space otherwise. */
402   public void writeSpaceFill()
403   {
404     write(' ');
405     writeBreak(PrettyWriter.NEWLINE_FILL);
406   }
407
408   public void writeBreakFill()
409   {
410     writeBreak(PrettyWriter.NEWLINE_FILL);
411   }
412
413   public void setIndentation(int amount, boolean current)
414   {
415     bout.addIndentation(amount, current);
416   }
417 }
418
Popular Tags