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