Hi,
To load a RTF-document, I tried to use an JEditorPane and
invoke the read(Reader,null) method of its superclass JTextPane.

But there is allways an IOException: īRFT is an 8-Bit formatī.

I tried to build the Reader with: 

InputStreamReader(new FileInputStream(fileName),encoding).
I tried every BytetoChar-Adapter I could find in the classes.zip, but it
donīt work (see example attached).

Does the read method require a special second parameter? 
Canīt find any Documention about this parameter.
Thank you for any help,

Bernhard
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class RTFTest extends JFrame{
  public static int WIDTH = 500;
  public static int HEIGHT = 500;

  JEditorPane rtfPane;
  public static String docName="test.rtf";
  String isoType;
  String[] 
formats={"Default","ISO8859_1","SingleByte","DoubleByte","DBCS_ASCII","DBCS_EBCDIC","EUC","ASCII","Big5","Cp037","Cp1006","Cp1025","Cp1026","Cp1046","SJIS","Cp1097","Cp1098","Cp1112","Cp1122","Cp1123","Cp1124","Cp1250","Cp1251","Cp1252","Cp1253","Cp1254","Cp1255","Cp1256","Cp1257","Cp1258","Cp1381","Cp1383","Cp273","Cp277","Cp278","Cp280","Cp284","Cp285","Cp297","Cp33722","Cp420","Cp424","Cp437","Cp500","Cp737","Cp775","Cp838","Cp850","Cp852","Cp855","Cp856","Cp857","Cp860","Cp861","Cp862","Cp863","Cp864","Cp865","Cp866","Cp868","Cp869","Cp870","Cp871","Cp874","Cp875","Cp918","Cp921","Cp922","Cp930","Cp935","Cp937","Cp939","Cp942","Cp942C","Cp943","Cp943C","Cp948","Cp949","Cp949C","Cp950","Cp970","JIS0201","JIS0208","JIS0212","EUC_CN","EUC_JP","EUC_KR","EUC_TW","GBK","ISO2022","ISO2022CN","ISO2022JP","ISO2022KR","ISO8859_2","ISO8859_3","ISO8859_4","ISO8859_5","ISO8859_6","ISO8859_7","ISO8859_8","ISO8859_9","JISAutoDetect","Johab","KOI8_R","MS874","MS936","MS950","MacArabic","MacCentralEurope","MacCroatian","MacCyrillic","MacDingbat","MacGreek","MacHebrew","MacIceland","MacRoman","MacRomania","MacSymbol","MacThai","MacTurkish","MacUkraine","TIS620","UTF8","Unicode","UnicodeBig","UnicodeLittle"};

  public RTFTest(String title){
    super(title);
      rtfPane = new JEditorPane();
      rtfPane.setContentType("application/rtf");
      //rtfPane.read(openFile(docName),null);
      rtfPane.setEditable(false);
      int len = formats.length;
      boolean res = false;
      for (int i=0;i<len;i++){
        // try to load the file with every ByteToChar-Adapter found in classes.zip
        res = loadDocument(formats[i]);
        System.out.println("round "+i+": format: "+formats[i]+" result: "+res);
        if (res)
          // stop trying, if loading process end successfully
          System.exit(0);
      }
  }

  public boolean loadDocument(String encoding){
    try {
        // which values can be given as the second parameter of the read-method ?
      rtfPane.read(openFile(docName,encoding),null);
      return true;
    } catch (IOException e) {
      System.out.println("IOException: " + e.getMessage());
    }
    return false;
  }

  public Reader openFile(String fileName, String encoding){
    Reader aReader=null;
    try {
        // Open the file with special encoding
      InputStreamReader isr = new InputStreamReader(new 
FileInputStream(fileName),encoding);
      aReader = new BufferedReader(isr);
    }
    catch ( FileNotFoundException fne) {
      System.err.println("File not found: "+fileName+": "+fne);
    } 
    catch (IOException e) {
      System.err.println("Canīt open the File: "+fileName+" | : "+e);
    } 
    return aReader;
  }//openFile

  public static void main(String args[])
  {
    if (args.length > 0) {
      docName = args[0];
    }

    RTFTest testFrame = new RTFTest("Test für RTF-Dokumente");
    testFrame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
 
    testFrame.setSize(WIDTH,HEIGHT);
    testFrame.setVisible(true);
  }// main
}



Reply via email to