2017-02-19

9: To Build a First UNO Extension (LibreOffice Extension or Apache OpenOffice Extension), Part Three

<The previous article in this series | The table of contents of this series | The next article in this series>

Main body START

To Know How to Handle (Read or Write) LibreOffice or Apache OpenOffice Writer or Calc Documents via Extensions from Java or Macro Programs

To Adjust Some Settings to the Environment

-Hypothesizer

One or two parameters in a Ant build file or a Gradle script may have to be modified. In the file, 'commonBuild.properties', for Ant, and in the file, 'commonBuild01.gradle', for Gradle, the two parameters, 'LIBREOFFICE_DIRECTORY_NAME' and 'LIBREOFFICE_SDK_DIRECTORY_NAME', have to be modified if they aren't correct for our environment.

-Rebutter

Ah-ha.

-Hypothesizer

'LIBREOFFICE_DIRECTORY_NAME' is the name of the directory where LibreOffice has been installed; 'LIBREOFFICE_SDK_DIRECTORY_NAME' is the name of the directory where LibreOffice SDK has been installed. On Windows, they will have to be modified. When they are modified, the directory separator to be used is '/', not '\'. In the Windows environment of this series, as LibreOffice has been installed at 'D:\LibreOffice', 'LIBREOFFICE_DIRECTORY_NAME' is set with the value of 'D:/LibreOffice', and as LibreOffice SDK has been installed at 'D:\LibreOffice\sdk', 'LIBREOFFICE_SDK_DIRECTORY_NAME' isn't modified.

-Rebutter

I see.

-Hypothesizer

In the same versions of LibreOffice and LibreOffice SDK with this series', other parameters shouldn't have to modified, but in older versions, it's likely that other parameters have to be modified. In fact, in LibreOffice 4, the directories structure was different from LibreOffice 5's. We can check whether directories and files set in properties, 'LIBREOFFICE_*' in 'commonBuild.properties' or 'commonBuild01.gradle', exist.

-Rebutter

Ah-ha.

To Build and Register the First UNO Extension

-Hypothesizer

Now, we can call the 'ant' or 'gradle' command. Open a terminal; change the current directory to the 'unoUtilitiesToDisclose' directory; execute 'ant' or 'gradle', depending on which of Ant and Gradle we will use. For both Ant and Gradle, the message, 'BUILD SUCCESSFUL', should appear.

-Rebutter

What if the message doesn't appear?

-Hypothesizer

The environment must be not as what we intend. JDK has to be newer than 8, including; settings mentioned above have to be done correctly; directory names are supposed to have no spaces. . . .

-Rebutter

OK.

-Hypothesizer

Next, change the current directory to the 'hiUnoExtensionsUnoExtension'; execute 'ant' or 'gradle', depending on which of Ant and Gradle we will use. If the UNO extension is built successfully, the command will proceed to register the UNO extension into LibreOffice. If LibreOffice has been already up, the command tries to shut down LibreOffice. If wmctrl isn't installed on Linux, we have to shut down LibreOffice manually. If wmctrl is installed on Linux or on Windows, LibreOffice will be shut down automatically if not-saved files aren't open; if some not-saved files are open, we will be asked to save those files. If LibreOffice won't be shut down for whatever reason, we have to shut down LibreOffice manually. Then, LibreOffice will be started automatically. We can check whether the process was successful by looking at the messages 'unopkg done.' and 'BUILD SUCCESSFUL' for both Ant and Gradle on the terminal.

-Rebutter

What if the process doesn't succeed?

-Hypothesizer

Well, other than possibilities mentioned above, if the script to shut down LibreOffice doesn't work fine (as bash and some OS commands are used in Ant build files and Gradle build scripts, some incompatibilities may exist in them between Linux distributions), it isn't critical. We can register UNO extensions from the LibreOffice menu, 'Tools'-'Extension Manager...'. The UNO extension file should exist in the 'target' directory under the sample UNO extension project directory; the file with the file extension of 'oxt' is the one. Just note that LibreOffice will have to be restarted after the UNO extension is registered.

-Rebutter

All right.

To Test the First UNO Extension

-Hypothesizer

Let's test the registered sample UNO extension. In a LibreOffice macro, we will create a UNO service instance, and call a method of the UNO service instance.

-Rebutter

To review, the sample UNO extension has a UNO interface implementation, which is registered as a UNO service, right?

-Hypothesizer

Yes. Actually, the UNO interface implementation has a method named 'sayHi' with a string parameter, and the UNO service is named 'thebiasplanet.uno.hiunoextensionsunoextension.HiUnoExtensions', and has a UNO service constructor named create1 with a string parameter.

-Rebutter

Ah-ha.

-Hypothesizer

On the menu of LibreOffice (it doesn't matter what file (Writer document, Calc document, etc) is opened, or no file is opened), we click 'Tools'; click 'Macros'-'Organize Macros'-"LibreOffice Basic..."; click 'My Macros'-'Standard' in the left 'Macro From' section; in 'Macro Name' section, type 'testsUnoExtension' (the single quotations don't need to be typed in) or any valid name; click the 'New' button; type this code inside the Sub with the name specified previously (such a Sub must have been created):

 Dim l_hiUnoExtensions As Variant
 l_hiUnoExtensions = thebiasplanet.uno.hiunoextensionsunoextension.HiUnoExtensions.create1 ("Hi")
 Msgbox (l_hiUnoExtensions.sayHi ("bro"))
-Rebutter

Um, we use Variant, not Object?

-Hypothesizer

A reference document tells us to do so at the bottom of the page. In this case, using Object doesn't cause problems, but it seems that the UNO object isn't exactly a LibreOffice Basic Object.

-Rebutter

Hmm, I wonder what the LibreOffice Basic Object is, then.

-Hypothesizer

I don't know. It seems that there is no explicit data type fit for UNO objects. And strangely, the LibreOffice Basic Object is used in sample codes in reference documents (for example, here) while the reference document recommends not to use the LibreOffice Basic Object.

-Rebutter

Hmm . . .

-Hypothesizer

By the way, when we use the term, UNO object, it's completely convertible with the term, UNO interface implementation instance.

-Rebutter

I see.

-Hypothesizer

Anyway, let's run the macro. We set the cursor inside the Sub; push the F5 key; a message box with the message, 'Hi, bro!' should pop up.

-Rebutter

What does the UNO object do?

-Hypothesizer

It just takes the base message to say from the constructor, takes how the receiver of the message should be called from the method, 'sayHi', and return the constructed message. If you change the parameters, you will see the returned message changed.

-Rebutter

Ah-ha.

-Hypothesizer

. . . It isn't fun? Well, so called Hello World type sample would be usually like that. However, as it's now confirmed that Java methods can be called from LibreOffice, almost anything can be called from LibreOffice if one puts codes in the methods. The method being called here is actually the 'sayHi' method of 'HiUnoExtensionsImplementation.java'.

-Rebutter

I see.

Main body END

References

  • Apache OpenOffice Wiki. (2014/01/02). Apache OpenOffice Developer's Guide. Retrieved from https://wiki.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide

<The previous article in this series | The table of contents of this series | The next article in this series>