home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / text / rtf / RTFParser.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  4.7 KB  |  245 lines

  1. package javax.swing.text.rtf;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6.  
  7. abstract class RTFParser extends AbstractFilter {
  8.    public int level = 0;
  9.    private int state = 0;
  10.    private StringBuffer currentCharacters = new StringBuffer();
  11.    private String pendingKeyword = null;
  12.    private int pendingCharacter;
  13.    private long binaryBytesLeft;
  14.    ByteArrayOutputStream binaryBuf;
  15.    private boolean[] savedSpecials;
  16.    protected PrintStream warnings;
  17.    private final int S_text = 0;
  18.    private final int S_backslashed = 1;
  19.    private final int S_token = 2;
  20.    private final int S_parameter = 3;
  21.    private final int S_aftertick = 4;
  22.    private final int S_aftertickc = 5;
  23.    private final int S_inblob = 6;
  24.    static final boolean[] rtfSpecialsTable;
  25.  
  26.    static {
  27.       rtfSpecialsTable = (boolean[])AbstractFilter.noSpecialsTable.clone();
  28.       rtfSpecialsTable[10] = true;
  29.       rtfSpecialsTable[13] = true;
  30.       rtfSpecialsTable[123] = true;
  31.       rtfSpecialsTable[125] = true;
  32.       rtfSpecialsTable[92] = true;
  33.    }
  34.  
  35.    public RTFParser() {
  36.       this.warnings = System.out;
  37.       super.specialsTable = rtfSpecialsTable;
  38.    }
  39.  
  40.    public abstract void begingroup();
  41.  
  42.    public void close() throws IOException {
  43.       this.flush();
  44.       if (this.state != 0 || this.level > 0) {
  45.          this.warnings.println("Truncated RTF file.");
  46.  
  47.          while(this.level > 0) {
  48.             this.endgroup();
  49.             --this.level;
  50.          }
  51.       }
  52.  
  53.       super.close();
  54.    }
  55.  
  56.    public abstract void endgroup();
  57.  
  58.    public void flush() throws IOException {
  59.       super.flush();
  60.       if (this.state == 0 && this.currentCharacters.length() > 0) {
  61.          this.handleText(this.currentCharacters.toString());
  62.          this.currentCharacters = new StringBuffer();
  63.       }
  64.  
  65.    }
  66.  
  67.    public abstract void handleBinaryBlob(byte[] var1);
  68.  
  69.    public abstract boolean handleKeyword(String var1);
  70.  
  71.    public abstract boolean handleKeyword(String var1, int var2);
  72.  
  73.    public void handleText(char var1) {
  74.       this.handleText(String.valueOf(var1));
  75.    }
  76.  
  77.    public abstract void handleText(String var1);
  78.  
  79.    public void write(char var1) throws IOException {
  80.       switch (this.state) {
  81.          case 0:
  82.             if (var1 != '\n' && var1 != '\r') {
  83.                if (var1 == '{') {
  84.                   if (this.currentCharacters.length() > 0) {
  85.                      this.handleText(this.currentCharacters.toString());
  86.                      this.currentCharacters = new StringBuffer();
  87.                   }
  88.  
  89.                   ++this.level;
  90.                   this.begingroup();
  91.                } else if (var1 == '}') {
  92.                   if (this.currentCharacters.length() > 0) {
  93.                      this.handleText(this.currentCharacters.toString());
  94.                      this.currentCharacters = new StringBuffer();
  95.                   }
  96.  
  97.                   if (this.level == 0) {
  98.                      throw new IOException("Too many close-groups in RTF text");
  99.                   }
  100.  
  101.                   this.endgroup();
  102.                   --this.level;
  103.                } else if (var1 == '\\') {
  104.                   if (this.currentCharacters.length() > 0) {
  105.                      this.handleText(this.currentCharacters.toString());
  106.                      this.currentCharacters = new StringBuffer();
  107.                   }
  108.  
  109.                   this.state = 1;
  110.                } else {
  111.                   this.currentCharacters.append(var1);
  112.                }
  113.             }
  114.             break;
  115.          case 1:
  116.             if (var1 == '\'') {
  117.                this.state = 4;
  118.                break;
  119.             } else if (!Character.isLetter(var1)) {
  120.                char[] var8 = new char[]{var1};
  121.                if (!this.handleKeyword(new String(var8))) {
  122.                   this.warnings.println("Unknown keyword: " + var8 + " (" + (byte)var1 + ")");
  123.                }
  124.  
  125.                this.state = 0;
  126.                this.pendingKeyword = null;
  127.                break;
  128.             } else {
  129.                this.state = 2;
  130.             }
  131.          case 2:
  132.             if (Character.isLetter(var1)) {
  133.                this.currentCharacters.append(var1);
  134.             } else {
  135.                this.pendingKeyword = this.currentCharacters.toString();
  136.                this.currentCharacters = new StringBuffer();
  137.                if (!Character.isDigit(var1) && var1 != '-') {
  138.                   boolean var6 = this.handleKeyword(this.pendingKeyword);
  139.                   if (!var6) {
  140.                      this.warnings.println("Unknown keyword: " + this.pendingKeyword);
  141.                   }
  142.  
  143.                   this.pendingKeyword = null;
  144.                   this.state = 0;
  145.                   if (!Character.isWhitespace(var1)) {
  146.                      this.write(var1);
  147.                   }
  148.                } else {
  149.                   this.state = 3;
  150.                   this.currentCharacters.append(var1);
  151.                }
  152.             }
  153.             break;
  154.          case 3:
  155.             if (Character.isDigit(var1)) {
  156.                this.currentCharacters.append(var1);
  157.             } else if (this.pendingKeyword.equals("bin")) {
  158.                long var3 = Long.parseLong(this.currentCharacters.toString());
  159.                this.pendingKeyword = null;
  160.                this.state = 6;
  161.                this.binaryBytesLeft = var3;
  162.                if (this.binaryBytesLeft > 2147483647L) {
  163.                   this.binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE);
  164.                } else {
  165.                   this.binaryBuf = new ByteArrayOutputStream((int)this.binaryBytesLeft);
  166.                }
  167.  
  168.                this.savedSpecials = super.specialsTable;
  169.                super.specialsTable = AbstractFilter.allSpecialsTable;
  170.             } else {
  171.                int var7 = Integer.parseInt(this.currentCharacters.toString());
  172.                boolean var2 = this.handleKeyword(this.pendingKeyword, var7);
  173.                if (!var2) {
  174.                   this.warnings.println("Unknown keyword: " + this.pendingKeyword + " (param " + this.currentCharacters + ")");
  175.                }
  176.  
  177.                this.pendingKeyword = null;
  178.                this.currentCharacters = new StringBuffer();
  179.                this.state = 0;
  180.                if (!Character.isWhitespace(var1)) {
  181.                   this.write(var1);
  182.                }
  183.             }
  184.             break;
  185.          case 4:
  186.             if (Character.digit(var1, 16) == -1) {
  187.                this.state = 0;
  188.             } else {
  189.                this.pendingCharacter = Character.digit(var1, 16);
  190.                this.state = 5;
  191.             }
  192.             break;
  193.          case 5:
  194.             this.state = 0;
  195.             if (Character.digit(var1, 16) != -1) {
  196.                this.pendingCharacter = this.pendingCharacter * 16 + Character.digit(var1, 16);
  197.                var1 = super.translationTable[this.pendingCharacter];
  198.                if (var1 != 0) {
  199.                   this.handleText(var1);
  200.                }
  201.             }
  202.             break;
  203.          case 6:
  204.             this.binaryBuf.write(var1);
  205.             --this.binaryBytesLeft;
  206.             if (this.binaryBytesLeft == 0L) {
  207.                this.state = 0;
  208.                super.specialsTable = this.savedSpecials;
  209.                this.savedSpecials = null;
  210.                this.handleBinaryBlob(this.binaryBuf.toByteArray());
  211.                this.binaryBuf = null;
  212.             }
  213.       }
  214.  
  215.    }
  216.  
  217.    public void write(String var1) throws IOException {
  218.       if (this.state != 0) {
  219.          int var2 = 0;
  220.  
  221.          int var3;
  222.          for(var3 = var1.length(); var2 < var3 && this.state != 0; ++var2) {
  223.             this.write(var1.charAt(var2));
  224.          }
  225.  
  226.          if (var2 >= var3) {
  227.             return;
  228.          }
  229.  
  230.          var1 = var1.substring(var2);
  231.       }
  232.  
  233.       if (this.currentCharacters.length() > 0) {
  234.          this.currentCharacters.append(var1);
  235.       } else {
  236.          this.handleText(var1);
  237.       }
  238.  
  239.    }
  240.  
  241.    public void writeSpecial(int var1) throws IOException {
  242.       this.write((char)var1);
  243.    }
  244. }
  245.