1 package gnu.mapping; 2 import java.io.*; 3 import gnu.text.*; 4 import gnu.lists.*; 5 6 9 10 public class OutPort extends PrintConsumer implements Printable 11 { 12 Object name; 13 private Writer base; 14 15 protected PrettyWriter bout; 20 21 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 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 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 name) 78 { 79 this(base, false, false); 80 this.name = name; 81 } 82 83 public OutPort (Writer base, boolean autoflush, Object 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 fname) 122 throws java.io.IOException 123 { 124 Object conv = Environment.user().get("port-char-encoding"); 125 java.io.OutputStream strm = URI_utils.getOutputStream(fname); 126 strm = new java.io.BufferedOutputStream (strm); 127 java.io.Writer wr; 128 if (conv == null || conv == Boolean.TRUE) 129 wr = new java.io.OutputStreamWriter (strm); 130 else 131 { 132 if (conv == Boolean.FALSE) 133 conv = "8859_1"; 134 wr = new java.io.OutputStreamWriter (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 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 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 name) throws java.io.IOException 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 184 185 protected static final boolean isWordChar(char ch) 186 { 187 return Character.isJavaIdentifierPart(ch) || ch == '-' || ch == '+'; 188 } 189 190 192 193 java.text.NumberFormat 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 v) 230 { 231 write(v == null ? "(null)" : v); 232 } 233 234 public void print(Object 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 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 276 public void beginAttribute(Object 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 289 public void endAttribute() 290 { 291 if (objectFormat != null) 292 objectFormat.endAttribute(this); 293 else 294 print(' '); 295 } 296 297 298 public void writeWordEnd () 299 { 300 bout.writeWordEnd(); 301 } 302 303 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 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 prefix, boolean perLine, 364 String suffix) 365 { 366 bout.startLogicalBlock(prefix, perLine, suffix); 367 } 368 369 public void startLogicalBlock (String prefix, String 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 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 396 public void writeBreakLinear() 397 { 398 writeBreak(PrettyWriter.NEWLINE_LINEAR); 399 } 400 401 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 |