001: /*
002: * @(#)OutImpl.java 1.11 2000/08/16
003: *
004: */
005:
006: package org.w3c.tidy;
007:
008: /**
009: *
010: * Output Stream Implementation
011: *
012: * (c) 1998-2000 (W3C) MIT, INRIA, Keio University
013: * See Tidy.java for the copyright notice.
014: * Derived from <a href="http://www.w3.org/People/Raggett/tidy">
015: * HTML Tidy Release 4 Aug 2000</a>
016: *
017: * @author Dave Raggett <dsr@w3.org>
018: * @author Andy Quick <ac.quick@sympatico.ca> (translation to Java)
019: * @version 1.0, 1999/05/22
020: * @version 1.0.1, 1999/05/29
021: * @version 1.1, 1999/06/18 Java Bean
022: * @version 1.2, 1999/07/10 Tidy Release 7 Jul 1999
023: * @version 1.3, 1999/07/30 Tidy Release 26 Jul 1999
024: * @version 1.4, 1999/09/04 DOM support
025: * @version 1.5, 1999/10/23 Tidy Release 27 Sep 1999
026: * @version 1.6, 1999/11/01 Tidy Release 22 Oct 1999
027: * @version 1.7, 1999/12/06 Tidy Release 30 Nov 1999
028: * @version 1.8, 2000/01/22 Tidy Release 13 Jan 2000
029: * @version 1.9, 2000/06/03 Tidy Release 30 Apr 2000
030: * @version 1.10, 2000/07/22 Tidy Release 8 Jul 2000
031: * @version 1.11, 2000/08/16 Tidy Release 4 Aug 2000
032: */
033:
034: import java.io.IOException;
035:
036: public class OutImpl extends Out {
037:
038: public OutImpl() {
039: this .out = null;
040: }
041:
042: public void outc(byte c) {
043: outc(((int) c) & 0xFF); // Convert to unsigned.
044: }
045:
046: /* For mac users, should we map Unicode back to MacRoman? */
047: public void outc(int c) {
048: int ch;
049:
050: try {
051: if (this .encoding == Configuration.UTF8) {
052: if (c < 128)
053: this .out.write(c);
054: else if (c <= 0x7FF) {
055: ch = (0xC0 | (c >> 6));
056: this .out.write(ch);
057: ch = (0x80 | (c & 0x3F));
058: this .out.write(ch);
059: } else if (c <= 0xFFFF) {
060: ch = (0xE0 | (c >> 12));
061: this .out.write(ch);
062: ch = (0x80 | ((c >> 6) & 0x3F));
063: this .out.write(ch);
064: ch = (0x80 | (c & 0x3F));
065: this .out.write(ch);
066: } else if (c <= 0x1FFFFF) {
067: ch = (0xF0 | (c >> 18));
068: this .out.write(ch);
069: ch = (0x80 | ((c >> 12) & 0x3F));
070: this .out.write(ch);
071: ch = (0x80 | ((c >> 6) & 0x3F));
072: this .out.write(ch);
073: ch = (0x80 | (c & 0x3F));
074: this .out.write(ch);
075: } else {
076: ch = (0xF8 | (c >> 24));
077: this .out.write(ch);
078: ch = (0x80 | ((c >> 18) & 0x3F));
079: this .out.write(ch);
080: ch = (0x80 | ((c >> 12) & 0x3F));
081: this .out.write(ch);
082: ch = (0x80 | ((c >> 6) & 0x3F));
083: this .out.write(ch);
084: ch = (0x80 | (c & 0x3F));
085: this .out.write(ch);
086: }
087: } else if (this .encoding == Configuration.ISO2022) {
088: if (c == 0x1b) /* ESC */
089: this .state = StreamIn.FSM_ESC;
090: else {
091: switch (this .state) {
092: case StreamIn.FSM_ESC:
093: if (c == '$')
094: this .state = StreamIn.FSM_ESCD;
095: else if (c == '(')
096: this .state = StreamIn.FSM_ESCP;
097: else
098: this .state = StreamIn.FSM_ASCII;
099: break;
100:
101: case StreamIn.FSM_ESCD:
102: if (c == '(')
103: this .state = StreamIn.FSM_ESCDP;
104: else
105: this .state = StreamIn.FSM_NONASCII;
106: break;
107:
108: case StreamIn.FSM_ESCDP:
109: this .state = StreamIn.FSM_NONASCII;
110: break;
111:
112: case StreamIn.FSM_ESCP:
113: this .state = StreamIn.FSM_ASCII;
114: break;
115:
116: case StreamIn.FSM_NONASCII:
117: c &= 0x7F;
118: break;
119: }
120: }
121:
122: this .out.write(c);
123: } else
124: this .out.write(c);
125: } catch (IOException e) {
126: System.err.println("OutImpl.outc: " + e.toString());
127: }
128: }
129:
130: public void newline() {
131: try {
132: this .out.write(nlBytes);
133: this .out.flush();
134: } catch (IOException e) {
135: System.err.println("OutImpl.newline: " + e.toString());
136: }
137: }
138:
139: private static final byte[] nlBytes = (System
140: .getProperty("line.separator")).getBytes();
141:
142: };
|