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

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