JDK-6221596 : RTF document - getLength() - last paragraph
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2005-01-26
  • Updated: 2015-10-16
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
and
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195] & XP

A DESCRIPTION OF THE PROBLEM :
1. When I read a RTF file from a JTextPane, an empty paragraph is added at the end of the document. If I load, then save, then reload, the resave, ... each time a new empty line appears il the document.

2. If I try to delete this last paragraphe (workaround), I cannot because it seams to be after the end of the document. The start position of this paragraph is equal to the Document.getLength(). So an exception is thrown (pos out of range). Why in AbstractDocument.getLength() I see a return .... -1 ?

3. When I save a RTF document throught the RTFEditorKit and I read it from MS Word 98 or MS Word X on a Machintosh, character encoding is not right. It seams that Word doesn't only look at the \ansi at the beginning of the RTF file but it looks also at the font encoding. So I have to add \fcharset0 after each \fnil to have the good characters on my MS Word

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. execute the following example
2. execute the following exemple
3. save a RTF file with accents from a JTextPane an read it from Word X or Word 98 on a Machintosh.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
last paragraph = xxx - 409
doc len = 409

ACTUAL -
last paragraph = 409 - 410
doc len = 409


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;

public class Test {
    
    public static void main(String[] args) {
        InputStream in = new StringBufferInputStream(
"{\\rtf1\\ansi" +
"{\\fonttbl\\f0\\fnil\\fcharset0 Monospaced;\\f1\\fnil\\fcharset0 Dialog;\\f2\\fnil\\fcharset0 Times;}"+
"\\paperw11906\\paperh16838\\margl200\\margr7746\\margt851\\margb567"+
"\\li0\\ri0\\fi0\\ql\\nowidctlpar\\f2\\fs24\\i0\\b0\\ul0\\cf0 Ja, Ihm, unserm ewigen K\\'f6nig, \\par"+
"Ihm, dem unsterblichen K\\'f6nig,\\par"+
"Ihm, unserm himmlichen K\\'f6nig,\\par"+
"dem Gott aller Weisheit,\\par"+
"dem Gott aller Weisheit. (2x)\\par"+
"$\\par"+
"Ja, bringt unserm K\\'f6nig \\par"+
"Anbetung und Ehre, \\par"+
"Ihm unserm K\\'f6nig \\par"+
"f\\'fcr immer.\\par"+
"Bringt unserm K\\'f6nig \\par"+
"Anbetung und Ehre \\par"+
"f\\'fcr immer und ewig! \\par"+
"Amen. Amen.\\par"+
"$\\par"+
"A toi, le Roi \\'e9ternel, \\par"+
"\\'e0 toi, le Roi immortel,\\par"+
"\\'e0 toi, le Roi invisible,\\par"+
"\\'e0 toi, Dieu, seul sage,\\par"+
"\\'e0 toi, Dieu, seul sage, (2x)\\par"+
"\\fs16 Joey Holder \\par"+
"\\'a9 1984 Far Lane Music Publishi\\fs24\\par"+
"\\li0\\ri0\\fi0\\f1\\ul0\\par ohhhhh"+
"}"
        );
        try {
            RTFEditorKit kit = new RTFEditorKit();
            Document doc = kit.createDefaultDocument();
            kit.read(in, doc, 0);
            Element e = doc.getDefaultRootElement();
            int c = e.getElementCount();
            e = e.getElement(c-1);
            System.out.println("last paragraph = " + e.getStartOffset() + " - " + e.getEndOffset());
            System.out.println("doc len = "+doc.getLength());
            // bug in AbstractDocument.getLength() where there is a strange "-1"
            // i cannot do doc.remove(e.getStartOffset(), 1); to workaround the empty paragraph
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
    
}

---------- END SOURCE ----------
###@###.### 2005-1-26 00:23:08 GMT