How to switch encoding of LibreOffice strings in Java UNO API?

32 Views Asked by At

I want to write a Java app using LibreOffice UNO API. When I trying to insert some Unicode text (cyrillic) I got mojibake. It seems that LibreOffice transcodes UTF-8 to single-byte encoding (in my case - CP1251).

Code example:

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.io.IOException;
import com.sun.star.text.*;
import com.sun.star.frame.*;
import com.sun.star.uno.*;
import com.sun.star.lang.*;
import com.sun.star.comp.helper.Bootstrap;

import static com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK;

public class Application {
    private XTextDocument xDoc;

    public static void main(String[] args) throws java.lang.Exception {
        XComponentContext xContext = Bootstrap.bootstrap();
                XMultiComponentFactory xMCF = xContext.getServiceManager();
        Object oDesktop = xMCF.createInstanceWithContext(
                        "com.sun.star.frame.Desktop", xContext);
                
                XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, oDesktop);
        XComponentLoader xCompLoader = UnoRuntime
                .queryInterface(XComponentLoader.class, xDesktop);

        PropertyValue[] props = new PropertyValue[0];
                XComponent xComp = xCompLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, props);
        XTextDocument xDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp);
                XText xText = xDoc.getText();
                XTextCursor xCursor = xText.createTextCursorByRange(xText.getStart());
                xText.insertString(xCursor, "Съешь же ещё этих мягких французских булочек да выпей чаю.", false);
    }
}

Result:

Result of executing of this code

I don't know what is going on here but I suppose that there is some configuration option which specifies an encoding of target document but I don't know where it is...

1

There are 1 best solutions below

0
Данила Кондратенко On

I have solved that problem by replacing JDK 17 by JDK 21. I have translated this program to C++ and received correct string so problem is in Java.

JDK 21 implements JEP 400 implemented at Java 18.