home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / text / rtf / RTFReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  10.8 KB  |  371 lines

  1. package com.sun.java.swing.text.rtf;
  2.  
  3. import com.sun.java.swing.text.MutableAttributeSet;
  4. import com.sun.java.swing.text.SimpleAttributeSet;
  5. import com.sun.java.swing.text.Style;
  6. import com.sun.java.swing.text.StyledDocument;
  7. import java.awt.Color;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.StreamTokenizer;
  12. import java.net.URL;
  13. import java.util.Dictionary;
  14. import java.util.Enumeration;
  15. import java.util.Hashtable;
  16.  
  17. class RTFReader extends RTFParser {
  18.    StyledDocument target;
  19.    Dictionary parserState;
  20.    Destination rtfDestination;
  21.    MutableAttributeSet documentAttributes;
  22.    Dictionary fontTable;
  23.    Color[] colorTable;
  24.    Style[] characterStyles;
  25.    Style[] paragraphStyles;
  26.    Style[] sectionStyles;
  27.    int rtfversion;
  28.    boolean ignoreGroupIfUnknownKeyword;
  29.    int skippingCharacters;
  30.    private static Dictionary straightforwardAttributes = RTFAttributes.attributesByKeyword();
  31.    private MockAttributeSet mockery;
  32.    static Dictionary textKeywords = null;
  33.    static final String TabAlignmentKey = "tab_alignment";
  34.    static final String TabLeaderKey = "tab_leader";
  35.    static Dictionary characterSets;
  36.    public static boolean useNeXTForAnsi;
  37.  
  38.    public RTFReader(StyledDocument var1) {
  39.       this.target = var1;
  40.       this.parserState = new Hashtable();
  41.       this.fontTable = new Hashtable();
  42.       this.rtfversion = -1;
  43.       this.mockery = new MockAttributeSet();
  44.       this.documentAttributes = new SimpleAttributeSet();
  45.    }
  46.  
  47.    public void handleBinaryBlob(byte[] var1) {
  48.       if (this.skippingCharacters > 0) {
  49.          --this.skippingCharacters;
  50.       }
  51.    }
  52.  
  53.    public void handleText(String var1) {
  54.       if (this.skippingCharacters > 0) {
  55.          if (this.skippingCharacters >= var1.length()) {
  56.             this.skippingCharacters -= var1.length();
  57.             return;
  58.          }
  59.  
  60.          var1 = var1.substring(this.skippingCharacters);
  61.          this.skippingCharacters = 0;
  62.       }
  63.  
  64.       if (this.rtfDestination != null) {
  65.          this.rtfDestination.handleText(var1);
  66.       } else {
  67.          super.warnings.println("Text with no destination. oops.");
  68.       }
  69.    }
  70.  
  71.    Color defaultColor() {
  72.       return Color.black;
  73.    }
  74.  
  75.    public void begingroup() {
  76.       if (this.skippingCharacters > 0) {
  77.          this.skippingCharacters = 0;
  78.       }
  79.  
  80.       Object var1 = this.parserState.get("_savedState");
  81.       if (var1 != null) {
  82.          this.parserState.remove("_savedState");
  83.       }
  84.  
  85.       Dictionary var2 = (Dictionary)((Hashtable)this.parserState).clone();
  86.       if (var1 != null) {
  87.          var2.put("_savedState", var1);
  88.       }
  89.  
  90.       this.parserState.put("_savedState", var2);
  91.       if (this.rtfDestination != null) {
  92.          this.rtfDestination.begingroup();
  93.       }
  94.  
  95.    }
  96.  
  97.    public void endgroup() {
  98.       if (this.skippingCharacters > 0) {
  99.          this.skippingCharacters = 0;
  100.       }
  101.  
  102.       Dictionary var1 = (Dictionary)this.parserState.get("_savedState");
  103.       Destination var2 = (Destination)var1.get("dst");
  104.       if (var2 != this.rtfDestination) {
  105.          this.rtfDestination.close();
  106.          this.rtfDestination = var2;
  107.       }
  108.  
  109.       Dictionary var3 = this.parserState;
  110.       this.parserState = var1;
  111.       if (this.rtfDestination != null) {
  112.          this.rtfDestination.endgroup(var3);
  113.       }
  114.  
  115.    }
  116.  
  117.    protected void setRTFDestination(Destination var1) {
  118.       Dictionary var2 = (Dictionary)this.parserState.get("_savedState");
  119.       if (var2 != null && this.rtfDestination != var2.get("dst")) {
  120.          super.warnings.println("Warning, RTF destination overridden, invalid RTF.");
  121.          this.rtfDestination.close();
  122.       }
  123.  
  124.       this.rtfDestination = var1;
  125.       this.parserState.put("dst", this.rtfDestination);
  126.    }
  127.  
  128.    public void close() throws IOException {
  129.       Enumeration var1 = this.documentAttributes.getAttributeNames();
  130.  
  131.       while(var1.hasMoreElements()) {
  132.          Object var2 = var1.nextElement();
  133.          this.target.putProperty(var2, this.documentAttributes.getAttribute((String)var2));
  134.       }
  135.  
  136.       super.warnings.println("RTF filter done.");
  137.       super.close();
  138.    }
  139.  
  140.    public boolean handleKeyword(String var1) {
  141.       boolean var3 = this.ignoreGroupIfUnknownKeyword;
  142.       if (this.skippingCharacters > 0) {
  143.          --this.skippingCharacters;
  144.          return true;
  145.       } else {
  146.          this.ignoreGroupIfUnknownKeyword = false;
  147.          Object var2;
  148.          if ((var2 = textKeywords.get(var1)) != null) {
  149.             this.handleText((String)var2);
  150.             return true;
  151.          } else if (var1.equals("fonttbl")) {
  152.             this.setRTFDestination(new FonttblDestination(this));
  153.             return true;
  154.          } else if (var1.equals("colortbl")) {
  155.             this.setRTFDestination(new ColortblDestination(this));
  156.             return true;
  157.          } else if (var1.equals("stylesheet")) {
  158.             this.setRTFDestination(new StylesheetDestination(this));
  159.             return true;
  160.          } else if (var1.equals("info")) {
  161.             this.setRTFDestination(new InfoDestination(this));
  162.             return false;
  163.          } else if (var1.equals("mac")) {
  164.             this.setCharacterSet("mac");
  165.             return true;
  166.          } else if (var1.equals("ansi")) {
  167.             if (useNeXTForAnsi) {
  168.                this.setCharacterSet("NeXT");
  169.             } else {
  170.                this.setCharacterSet("ansi");
  171.             }
  172.  
  173.             return true;
  174.          } else if (var1.equals("next")) {
  175.             this.setCharacterSet("NeXT");
  176.             return true;
  177.          } else if (var1.equals("pc")) {
  178.             this.setCharacterSet("cpg437");
  179.             return true;
  180.          } else if (var1.equals("pca")) {
  181.             this.setCharacterSet("cpg850");
  182.             return true;
  183.          } else if (var1.equals("*")) {
  184.             this.ignoreGroupIfUnknownKeyword = true;
  185.             return true;
  186.          } else if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1)) {
  187.             return true;
  188.          } else {
  189.             if (var1.equals("aftncn") || var1.equals("aftnsep") || var1.equals("aftnsepc") || var1.equals("annotation") || var1.equals("atnauthor") || var1.equals("atnicn") || var1.equals("atnid") || var1.equals("atnref") || var1.equals("atntime") || var1.equals("atrfend") || var1.equals("atrfstart") || var1.equals("bkmkend") || var1.equals("bkmkstart") || var1.equals("datafield") || var1.equals("do") || var1.equals("dptxbxtext") || var1.equals("falt") || var1.equals("field") || var1.equals("file") || var1.equals("filetbl") || var1.equals("fname") || var1.equals("fontemb") || var1.equals("fontfile") || var1.equals("footer") || var1.equals("footerf") || var1.equals("footerl") || var1.equals("footerr") || var1.equals("footnote") || var1.equals("ftncn") || var1.equals("ftnsep") || var1.equals("ftnsepc") || var1.equals("header") || var1.equals("headerf") || var1.equals("headerl") || var1.equals("headerr") || var1.equals("keycode") || var1.equals("nextfile") || var1.equals("object") || var1.equals("pict") || var1.equals("pn") || var1.equals("pnseclvl") || var1.equals("pntxtb") || var1.equals("pntxta") || var1.equals("revtbl") || var1.equals("rxe") || var1.equals("tc") || var1.equals("template") || var1.equals("txe") || var1.equals("xe")) {
  190.                var3 = true;
  191.             }
  192.  
  193.             if (var3) {
  194.                this.setRTFDestination(new DiscardingDestination(this));
  195.             }
  196.  
  197.             return false;
  198.          }
  199.       }
  200.    }
  201.  
  202.    public boolean handleKeyword(String var1, int var2) {
  203.       boolean var3 = this.ignoreGroupIfUnknownKeyword;
  204.       if (this.skippingCharacters > 0) {
  205.          --this.skippingCharacters;
  206.          return true;
  207.       } else {
  208.          this.ignoreGroupIfUnknownKeyword = false;
  209.          if (var1.equals("uc")) {
  210.             this.parserState.put("UnicodeSkip", new Integer(var2));
  211.             return true;
  212.          } else if (var1.equals("u")) {
  213.             if (var2 < 0) {
  214.                var2 += 65536;
  215.             }
  216.  
  217.             ((RTFParser)this).handleText((char)var2);
  218.             Number var4 = (Number)this.parserState.get("UnicodeSkip");
  219.             if (var4 != null) {
  220.                this.skippingCharacters = var4.intValue();
  221.             } else {
  222.                this.skippingCharacters = 1;
  223.             }
  224.  
  225.             return true;
  226.          } else if (var1.equals("rtf")) {
  227.             this.rtfversion = var2;
  228.             this.setRTFDestination(new DocumentDestination(this));
  229.             return true;
  230.          } else {
  231.             if (var1.startsWith("NeXT") || var1.equals("private")) {
  232.                var3 = true;
  233.             }
  234.  
  235.             if (this.rtfDestination != null && this.rtfDestination.handleKeyword(var1, var2)) {
  236.                return true;
  237.             } else {
  238.                if (var3) {
  239.                   this.setRTFDestination(new DiscardingDestination(this));
  240.                }
  241.  
  242.                return false;
  243.             }
  244.          }
  245.       }
  246.    }
  247.  
  248.    private void setTargetAttribute(String var1, Object var2) {
  249.    }
  250.  
  251.    public void setCharacterSet(String var1) {
  252.       Object var2;
  253.       try {
  254.          var2 = getCharacterSet(var1);
  255.       } catch (Exception var5) {
  256.          super.warnings.println("Exception loading RTF character set \"" + var1 + "\": " + var5);
  257.          var2 = null;
  258.       }
  259.  
  260.       if (var2 != null) {
  261.          super.translationTable = (char[])var2;
  262.       } else {
  263.          super.warnings.println("Unknown RTF character set \"" + var1 + "\"");
  264.          if (!var1.equals("ansi")) {
  265.             try {
  266.                super.translationTable = (char[])getCharacterSet("ansi");
  267.             } catch (IOException var4) {
  268.                throw new InternalError("RTFReader: Unable to find character set resources (" + var4 + ")");
  269.             }
  270.          }
  271.       }
  272.  
  273.       this.setTargetAttribute("rtfCharacterSet", var1);
  274.    }
  275.  
  276.    public static synchronized void defineCharacterSet(String var0, char[] var1) {
  277.       if (var1.length < 256) {
  278.          throw new IllegalArgumentException("Translation table must have 256 entries.");
  279.       } else {
  280.          characterSets.put(var0, var1);
  281.       }
  282.    }
  283.  
  284.    public static synchronized Object getCharacterSet(String var0) throws IOException {
  285.       char[] var1 = (char[])characterSets.get(var0);
  286.       if (var1 == null) {
  287.          System.err.println("reading charset " + var0);
  288.          Object var2 = null;
  289.  
  290.          Class var3;
  291.          try {
  292.             var3 = Class.forName("com.sun.java.swing.text.rtf.RTFReader");
  293.          } catch (ClassNotFoundException var5) {
  294.             throw new InternalError("Help, I do not exist (" + var5 + ")");
  295.          }
  296.  
  297.          InputStream var6 = var3.getResourceAsStream("charsets/" + var0 + ".txt");
  298.          var1 = readCharset(var6);
  299.          defineCharacterSet(var0, var1);
  300.       }
  301.  
  302.       return var1;
  303.    }
  304.  
  305.    static char[] readCharset(InputStream var0) throws IOException {
  306.       char[] var1 = new char[256];
  307.       StreamTokenizer var3 = new StreamTokenizer(new InputStreamReader(var0));
  308.       var3.eolIsSignificant(false);
  309.       var3.commentChar(35);
  310.       var3.slashSlashComments(true);
  311.       var3.slashStarComments(true);
  312.  
  313.       for(int var2 = 0; var2 < 256; ++var2) {
  314.          int var4;
  315.          try {
  316.             var4 = var3.nextToken();
  317.          } catch (Exception var6) {
  318.             throw new IOException("Unable to read from character set file (" + var6 + ")");
  319.          }
  320.  
  321.          if (var4 != -2) {
  322.             throw new IOException("Unexpected token in character set file");
  323.          }
  324.  
  325.          var1[var2] = (char)((int)var3.nval);
  326.       }
  327.  
  328.       return var1;
  329.    }
  330.  
  331.    static char[] readCharset(URL var0) throws IOException {
  332.       return readCharset(var0.openStream());
  333.    }
  334.  
  335.    // $FF: synthetic method
  336.    static Dictionary access$0() {
  337.       return straightforwardAttributes;
  338.    }
  339.  
  340.    // $FF: synthetic method
  341.    static MockAttributeSet access$1(RTFReader var0) {
  342.       return var0.mockery;
  343.    }
  344.  
  345.    static {
  346.       textKeywords = new Hashtable();
  347.       textKeywords.put("\\", "\\");
  348.       textKeywords.put("{", "{");
  349.       textKeywords.put("}", "}");
  350.       textKeywords.put(" ", "┬á");
  351.       textKeywords.put("~", "┬á");
  352.       textKeywords.put("_", "ΓÇæ");
  353.       textKeywords.put("bullet", "ΓÇó");
  354.       textKeywords.put("emdash", "ΓÇö");
  355.       textKeywords.put("emspace", "ΓÇâ");
  356.       textKeywords.put("endash", "ΓÇô");
  357.       textKeywords.put("enspace", "ΓÇé");
  358.       textKeywords.put("ldblquote", "ΓÇ£");
  359.       textKeywords.put("lquote", "ΓÇÿ");
  360.       textKeywords.put("ltrmark", "\u200e");
  361.       textKeywords.put("rdblquote", "ΓÇ¥");
  362.       textKeywords.put("rquote", "ΓÇÖ");
  363.       textKeywords.put("rtlmark", "\u200f");
  364.       textKeywords.put("tab", "\t");
  365.       textKeywords.put("zwj", "\u200d");
  366.       textKeywords.put("zwnj", "\u200c");
  367.       textKeywords.put("-", "ΓǺ");
  368.       characterSets = new Hashtable();
  369.    }
  370. }
  371.