Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4735839

RTFEditorKit wrongly parses RTF while reading /writing to stream/document

XMLWordPrintable

    • Cause Known
    • x86
    • windows_nt



      Name: gm110360 Date: 08/22/2002


      FULL PRODUCT VERSION :
      java version "1.4.1-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-
      Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Windows NT Version 4.0 service pack 6

      A DESCRIPTION OF THE PROBLEM :
      I have a JTextPane which I am using for my Text Editor
      application.
      I have set the StyledDocument as the DefaultStyledDocument.
      I am trying to read a RTF file generated via MS Word.
      The RTF has Formatting/alignment/Font/Color at each
      character level.

      When I parse this RTF file and render the StyledDocument ,
      the background color of text and alignment are lost.

      The reverse is also true i.e. if I try to write the
      StyledDocument content into an output stream , the
      background color of text and alignment is lost.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.Create an RTF file using MS Word
      2.Create a JTextPane and attach a DefaultStyledDocument to
      it
      3.Create a RTFEditorKit and input/output streams
      4.Read this RTF file using RTFEditorKit and read it into the
      DefaultStyledDocument -- compare it with MS word file
      5.Use the read/write API's of RTFEditorKit
      6.Write this DefaultStyledDocument into a new RTF file.
      7. Compare the file with the original RTF file.

      RESULTS::
      The background color of text is lost .
      The alignment is LEFT -- ignores alignment

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      ------------------------------------------------------------
      Actual RTF file generated by MS word::
      ------------------------------------------------------------
      {\rtf1\ansi\ansicpg1252\uc1
      \deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharse
      t0\fprq2{\*\panose 02020603050405020304}Times New
      Roman;}{\f2\fmodern\fcharset0\fprq1{\*\panose
      02070309020205020404}Courier New;}
      {\f23\froman\fcharset128\fprq1{\*\panose
      00000000000000000000}MS Mincho{\*\falt MS
      ??};}{\f28\fmodern\fcharset0\fprq1{\*\panose
      020b0609040504020204}Lucida
      Console;}{\f29\fswiss\fcharset0\fprq2{\*\panose
      020b0a04020102020204}Arial Black;}
      {\f30\froman\fcharset128\fprq1{\*\panose
      00000000000000000000}@MS
      Mincho;}{\f31\froman\fcharset238\fprq2 Times New Roman
      CE;}{\f32\froman\fcharset204\fprq2 Times New Roman
      Cyr;}{\f34\froman\fcharset161\fprq2 Times New Roman Greek;}
      {\f35\froman\fcharset162\fprq2 Times New Roman
      Tur;}{\f38\froman\fcharset186\fprq2 Times New Roman
      Baltic;}{\f47\fmodern\fcharset238\fprq1 Courier New
      CE;}{\f48\fmodern\fcharset204\fprq1 Courier New
      Cyr;}{\f50\fmodern\fcharset161\fprq1 Courier New Greek;}
      {\f51\fmodern\fcharset162\fprq1 Courier New
      Tur;}{\f54\fmodern\fcharset186\fprq1 Courier New
      Baltic;}{\f255\fmodern\fcharset238\fprq1 Lucida Console
      CE;}{\f256\fmodern\fcharset204\fprq1 Lucida Console Cyr;}
      {\f258\fmodern\fcharset161\fprq1 Lucida Console
      Greek;}{\f259\fmodern\fcharset162\fprq1 Lucida Console
      Tur;}{\f263\fswiss\fcharset238\fprq2 Arial Black
      CE;}{\f264\fswiss\fcharset204\fprq2 Arial Black Cyr;}
      {\f266\fswiss\fcharset161\fprq2 Arial Black
      Greek;}{\f267\fswiss\fcharset162\fprq2 Arial Black
      Tur;}{\f270\fswiss\fcharset186\fprq2 Arial Black
      Baltic;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;
      \red0\green255\blue255;\red0\green255\blue0;
      \red255\green0\blue255;\red255\green0\blue0;\red255\green255
      \blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\g
      reen128\blue128;\red0\green128\blue0;\red128\green0\blue128;
      \red128\green0\blue0;\red128\green128\blue0;\red128\green128
      \blue128;
      \red192\green192\blue192;}{\stylesheet{\ql
      \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\l
      in0\itap0
      \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033
      \snext0 Normal;}{\*\cs10 \additive Default Paragraph Font;}{
      \s15\ql
      \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\l
      in0\itap0
      \f2\fs20\lang1033\langfe1033\cgrid\langnp1033\langfenp1033
      \sbasedon0 \snext15 Plain Text;}}{\info{\title This is
      Oracle Title}{\author usinha}{\operator usinha}
      {\creatim\yr2002\mo7\dy29\hr14\min43}{\revtim\yr2002\mo7\dy2
      9\hr16\min11}{\version8}{\edmins4}{\nofpages1}{\nofwords0}{nofchars0}{\*\company
      oracle}{\nofcharsws0}{\vern8247}}\margl1319\margr1319
      \widowctrl\ftnbj\aenddoc\noxlattoyen\expshrtn\noultrlspc\dnt
      blnsbdb\nospaceforul\hyphcaps0\formshade\horzdoc\dgmargin\dg
      hspace180\dgvspace180\dghorigin1701\dgvorigin1984\dghshow1\d
      gvshow1
      \jexpand\viewkind4\viewscale100\pgbrdrhead\pgbrdrfoot\splytw
      nine\ftnlytwnine\htmautsp\nolnhtadjtbl\useltbaln\alntblind\l
      ytcalctblwd\lyttblrtgr\lnbrkrule \fet0\sectd
      \linex0\endnhere\sectlinegrid360\sectdefaultcl {\*\pnseclvl1
      \pnucrm\pnstart1\pnindent720\pnhang{\pntxta
      .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang{\pntxta
      .}}{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang{\pntxta
      .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta
      )}}{\*\pnseclvl5
      \pndec\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta
      )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb
      (}{\pntxta
      )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb
      (}{\pntxta
      )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang
      {\pntxtb (}{\pntxta
      )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb
      (}{\pntxta )}}\pard\plain \s15\qr
      \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\l
      in0\itap0
      \f2\fs20\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {
      \fs52\cf4\highlight7\loch\af28\hich\af28\dbch\af23
      \hich\af28\dbch\af23\loch\f28 This
      is}{\cf9\highlight7\dbch\af23 \hich\af2\dbch\af23\loch\f2
      }{\b\i\fs36\ul\cf9\highlight7\loch\af29\hich\af29\dbch\af23
      \hich\af29\dbch\af23\loch\f29 Oracle Title}{
      \dbch\af23
      \par }}

      -----------------------------------------------------------
      The RTF file created by the RTFEditorKit ::
      -----------------------------------------------------------
      {\rtf1\ansi
      {\fonttbl\f0\fnil Monospaced;\f1\fnil Dialog;\f2\fnil Lucida
      Console;\f3\fnil Courier New;\f4\fnil Arial Black;}
      {\colortbl\red0\green0\blue0;\red0\green255\blue0;\red0\gree
      n0\blue128;}
      {\stylesheet{\s2\f1\fs24\i0\b0\cf0
      default;}{\s1\li0\ri0\fi0\ql\sbasedon2\snext1 Plain Text;}}
      \margl1319\margr1319\widowctrl
      \s1\li0\ri0\fi0\f2\fs52\i0\b0\ul0\cf1 This is\f3\fs20\cf2
      \f4\fs36\i\b\ul Oracle Title\f3\fs20\i0\b0\ul0\cf0\par
      \pard\li0\ri0\fi0\f1\fs24\ul0\par
      }
      ------------------------------------------------------------


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /**
      * This is a Demo application created
      */
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.text.rtf.*;

      public class RTFTest extends JApplet implements ActionListener
      {

        public static void main(String args[])
        {
          JFrame frame = new JFrame("TitleTest Application");
          frame.setSize(300, 200);
          frame.setLocation(50, 50);
          RTFTest test = new RTFTest();
          test.init();
          frame.getContentPane().add("Center", test);

          frame.show();
          frame.addWindowListener(new java.awt.event.WindowAdapter()
          {
            public void windowClosing(java.awt.event.WindowEvent event)
            {
              System.exit(0); // close the application
            }
          });
        }

        /**
         * constructor
         */
        public RTFTest()
        {
        }

        public void init()
        {
          super.init();
          
          getContentPane().setLayout(new BorderLayout());

          // Construct main part of UI
          JPanel common = new JPanel(new BorderLayout());

          JTextPane jtp = new JTextPane();
         
          std = new DefaultStyledDocument();

          RTFEditorKit kit = new RTFEditorKit();
          try
          {
             // Read from the RTF file stream and render the StyledDocument
              FileInputStream inputStream = new FileInputStream("D:\\Edit
                                                               Title\\javaTitle.rtf");
              kit.read(inputStream, std, 0);
              jtp.setStyledDocument(std);
              inputStream.close();
          }
          catch (Exception streamException)
          {
              System.out.println("Failed to Read from file: " +
                                                     streamException.toString());
              streamException.printStackTrace();
          }

          common.add("Center", jtp);

          getContentPane().add("Center", common);

          // Construct buttons of UI
          _buttonPanel = new JPanel();
          _buttonPanel.setLayout(new GridLayout(0, 1));
          common.add("East", _buttonPanel);

          _buttonToRTF = new JButton("Save RTF");
          _buttonPanel.add(_buttonToRTF);

          _buttonToRTF.addActionListener(this);

        }

        public void actionPerformed(ActionEvent e)
        {

          if (e.getSource() == _buttonToRTF)
          {
            StyledDocument doc = std;
            int length = doc.getLength();
            RTFEditorKit kit = new RTFEditorKit();

            try
            {
              // Write the Document's content into the RTF file
              FileOutputStream outStream = new FileOutputStream("D:\\Edit
                                                             Title\\javaTitle1.rtf");
              kit.write(outStream,std,0,length);
              outStream.close();

              ////////////////Print out the Document's content i.e. RTF ///////////
              PipedOutputStream pos = new PipedOutputStream();

              PipedInputStream pis = new PipedInputStream(pos);

              InputStreamReader isr = new InputStreamReader(pis, "UTF8");

              kit.write(pos, doc, 0, length);
              pos.flush();
              pos.close();
              
              StringBuffer buffer = new StringBuffer();
              Reader in = new BufferedReader(isr);
              int ch;
              while ((ch = in.read()) > -1)
              {
                 buffer.append((char)ch);
              }
              in.close();
              System.out.println("RTF O/P::"+buffer.toString());
              ///////////////////////////////////////////////////////////////
            }
            catch (Exception streamException)
            {
              System.out.println("Failed to write to file: " +
                                                        streamException.toString());
              streamException.printStackTrace();
            }
          }
        }


        private JPanel _buttonPanel = null;
        private JButton _buttonToRTF = null;
        private StyledDocument std = null;

      }

      ---------- END SOURCE ----------
      (Review ID: 160027)
      ======================================================================

            peterz Peter Zhelezniakov
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: