Difference between revisions of "UNO automation with a binary (executable)"

From Apache OpenOffice Wiki
Jump to: navigation, search
(See also)
(Getting the Service Manager)
Line 116: Line 116:
 
         printf( "XComonentloader succesfully instanciated\n" );
 
         printf( "XComonentloader succesfully instanciated\n" );
 
     }
 
     }
<code>
+
</code>
 
Before to see the problems with this code, we give again a schematic representation :
 
Before to see the problems with this code, we give again a schematic representation :
  

Revision as of 16:54, 15 May 2006

We want now discuss a fully UNO/OpenOffice.org program : we mean a program which automates some tasks on OpenOffice.org documents, file loading, file modifying, file saving... all in all “Create a binary program to replace a OOoBasic program”. The border between the examples of previous chapter and of this chapter is vague. I can only say  : if you imagine to automate any problem and create a program for, this paragraph and the next chapters are for you.

Introduction : starting from a SDK example

Danny Brewer wrote in the Openoffice forum : http://www.oooforum.org/forum/viewtopic.php?t=5252 “To do almost anything useful with OOo via. the API, in almost all cases, you must either create a new document, or open an existing document. Then you would manipulate the contents of the document, extract information from it, print it, convert it to a different format, work with data forms on the document, or perform various other tasks with office documents.

Therefore, one of the first things to learn is how to open or create documents.

ServiceManager and Desktop objects

In order to work with OOo via. the API, you must first acquire two essential objects.

  1. the ServiceManager
  2. the Desktop

Once you have the ServiceManager, you call its createInstance() method to get the Desktop object. Once you have the Desktop object, you can use it to create or open new documents.

Getting the Service Manager

In every different programming language, there are different mechanisms for acquiring the Service Manager.” One way of getting the service manager is given in one SDK example : <OpenOffice.org1.1_SDK>/examples/DevelopersGuide/ProfUNO/CppBinding We first check the example (under Linux) :

cd <OpenOffice.org1.1_SDK>
./setsdkenv_unix
cd examples/DevelopersGuide/ProfUNO/CppBinding
make
make Office_connect.run

which only writes out "Connected successfully to the office". Under Windows it would be :

cd <OpenOffice.org1.1_SDK>
setsdkenv_windows
cd examples/DevelopersGuide/ProfUNO/CppBinding
make
make Office_connect.run

We start first our discussion from this example. Later on, we will provide an other starting code. All listings we will give below are to be added at this example. Where ? It's shown here (in red) : [cpp] //C++ *** extract from office_connect.cxx int main( ) { // create the initial component context Reference< XComponentContext > rComponentContext = defaultBootstrap_InitialComponentContext(); // retrieve the servicemanager from the context Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager(); // instantiate a sample service with the servicemanager. Reference< XInterface > rInstance = rServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ), rComponentContext ); // Query for the XUnoUrlResolver interface Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY ); if( ! rResolver.is() ) { printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" ); return 1; } try { // resolve the uno-url rInstance = rResolver->resolve( OUString::createFromAscii( "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" ) ); if( ! rInstance.is() ) { printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" ); return 1; } // query for the simpler XMultiServiceFactory interface, sufficient for scripting Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY); if( ! rInstance.is() )

       {
           printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" );
           return 1;
       }
       printf( "Connected sucessfully to the office\n" );

// ***** add your code here } catch( Exception &e ) { OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); printf( "Error: %s\n", o.pData->buffer ); return 1; } return 0; } I cannot explain all the lines of this source file. I can just say : here is one way among others to get the Service Manager. A second way is given in <OpenOffice.org1.1_SDK>/examples/cpp/DocumentLoader's example. That piece of code is in general called bootstrapping. To memorize this piece of code is not easy and probably useless. However, we give a drawing representation of this piece of code to show the main step of the bootstrap process.

Ch4Fig1bootstrap.png

Bootstrap processes are more deeply discussed later.

If you are interested in, have a look simultaneously to the code and to the drawing to see the schematic conventions I have used to carry out the Figure above. For instance both XInterface are connected because we use the same variable in fact. The gray filled rectangles are for parameters... and so on. We want now the desktop and load a OooCalc component. The first step is to obtain desktop. You then add this code :

[cpp] Reference< XInterface > xDesktop = rOfficeServiceManager->createInstance(

  	OUString::createFromAscii( "com.sun.star.frame.Desktop" ));

Your desktop object is xDesktop. Second step : query for the XComponentLoader interface

[cpp] // query a XcomponentLoader Interface Reference< XComponentLoader > rComponentLoader (xDesktop, UNO_QUERY); if( rComponentLoader.is() )

   	{
       	printf( "XComonentloader succesfully instanciated\n" );
   	}

Before to see the problems with this code, we give again a schematic representation :

See also

Personal tools