2020-11-29

55: Read and Write Any LibreOffice Document from and to a Stream

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

Or any OpenOffice, Microsoft Office, or similar document. In order to store the document in a database, a remote server, or whatever.

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice

The table of contents of this article


Starting Context


  • The reader has a basic knowledge on LibreOffice or Apache OpenOffice.

Target Context


  • The reader will know a way to read and write any LibreOffice, Apache OpenOffice, Microsoft Office, or similar document from and to a stream.

Orientation


How to make any LibreOffice or Apache OpenOffice instance a UNO server has been addressed in a previous article.

How to create a minimum WebDAV server has been addressed in an article of another series.

How to programmatically open and store any LibreOffice or Apache OpenOffice document has been addressed in a previous article (for Java, for C++, for C#, for Python, and for Basic).


Main Body

Stage Direction
Here are Hypothesizer 7, Objector 55A, and Objector 55B in front of a computer.


1: What If You Want to a Store a LibreOffice Document in a Remote Server?


Objector 55B
I know how to programmatically save any LibreOffice document to a local file.

Hypothesizer 7
Congratulations, madam.

Objector 55B
But I want to save the document in a remote server.

Hypothesizer 7
The easiest way is to start the LibreOffice server in the remote server, if that is allowed.

Objector 55B
Sorry, I can't move my program to the remote server.

Hypothesizer 7
You do not have to: your program will connect to the remote LibreOffice server via TCP/IP socket.

Objector 55B
Huh? My program connects to the local LibreOffice instance, right?

Hypothesizer 7
Not right, madam. Your program can connect to the remote LibreOffice server if it employs an appropriate way (for Java, for C++, for C#, or for Python).

Objector 55B
. . . Well, but I'm not allowed to start the LibreOffice server in the remote server.

Hypothesizer 7
In that case, another way is to start a WebDAV server in the remote server.

Objector 55B
"WebDAV server"? What WebDAV server, exactly?

Hypothesizer 7
Whatever you like. Apache Tomcat can become one, and Microsoft Windows starts one by default, for example.

Objector 55B
Well, how will the WebDAV server be used?

Hypothesizer 7
You specify a URL in order to store the document, in your program, right?

Objector 55B
I specify a URL like 'file:///D:/home/objector47b/myData/tests/Test.odt'.

Hypothesizer 7
You should have noticed that it is not a file path, but a URL.

Objector 55B
Of course.

Hypothesizer 7
The suggests that the function argument is more versatile than accepting only file URLs.

Objector 55B
So, can I pass HTTP URLs into it?

Hypothesizer 7
You can pass WebDAV URLs (which are HTML URLs) into it.


2: What If You Want to a Store a LibreOffice Document in a Database?


Objector 55A
I want to save LibreOffice documents in a database.

Hypothesizer 7
Very well, sir.

Objector 55A
What URLs should I specify for the argument?

Hypothesizer 7
Well, as LibreOffice does not support such URLs (as far as I know), a way is, again, to use a WebDAV server.

Objector 55A
Huh? What does WebDAV have, to do with it? Any WebDAV server saves data in a file system, right?

Hypothesizer 7
Not necessarily. WebDAV is about the protocol between the server and the client; the data can be kept in any forms.

Objector 55A
So, is there a WebDAV product that saves data in a database?

Hypothesizer 7
I do not particularly know such an existing product, but you can create one yourself, rather easily.

Objector 55A
Huh? Is there a library or something for that?

Hypothesizer 7
You do not need any particular library except the standard library in typical programming languages; I have introduced a Java WebDAV server in an article of another series.

Objector 55A
. . . Does that save data in a database?

Hypothesizer 7
It does not do so as it is, but you can tweak it as you like.

Objector 55A
How?

Hypothesizer 7
When you save a document, the WebDAV server receives the datum as the request body stream of a 'PUT' request; when you open a document, the WebDAV server sends the datum as the response body stream of a 'GET' request. Your job is to get or set the datum from or to the stream.

Objector 55A
So, can I just tweak 'PUT' and 'GET' handlers?

Hypothesizer 7
You will have to tweak also some other handlers, because the server has to check the existence of the datum and get and set some attributes of the datum, by accessing the database.

Objector 55A
. . . What will the URL be like?

Hypothesizer 7
That is what you design. For example, like 'http://%the server name%:%the port number%/%the datum name%' to be simple or like 'http://%the server name%:%the port number%/%the database name%/%the table name%/%the datum name%' to be more versatile.


3: The Gist of the Technique Is to Read and Write any LibreOffice or Apache OpenOffice Document from and to a Stream, So . . .


Hypothesizer 7
The gist of the technique is to read and write any LibreOffice or Apache OpenOffice stream document from and to a stream.

The storage of the datum can be anything: a filesystem, a database, the memory, or whatever you can think of.


4: In Fact, the Document Does Not Have to Be Handled Programmatically, and the Document Can Be a Microsoft Office Document or Whatever That Can Be Accessed Via WebDAV


Hypothesizer 7
We have assumed that we are handling the document programmatically, but the merit of the technique is that that is not any necessity.

Objector 55A
If I save a document via the LibreOffice menu, it will go into the database.

Hypothesizer 7
In fact, you do not need to use LibreOffice or Apache OpenOffice, but Microsoft Office or whatever that supports WebDAV (just note that as Microsoft Office requires 'LOCK', the WebDAV server introduced in an article of another series has to be extended to support 'LOCK').


References


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