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