1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // __________ Imports __________
25 
26 import com.sun.star.uno.UnoRuntime;
27 
28 public class Helper
29 {
30     // __________ static helper methods __________
31 
32     /** Connect to an office, if no office is running a new instance is started.
33      * A new connection is established and the service manger from the running
34      * offic eis returned.
35      */
connect()36     static public com.sun.star.uno.XComponentContext connect()
37 		throws Exception
38     {
39         // get the remote office component context
40         com.sun.star.uno.XComponentContext xOfficeContext =
41             com.sun.star.comp.helper.Bootstrap.bootstrap();
42 
43         // if connection fails an exception is thrown
44         System.out.println("Connected to a running office ...");
45 
46 		return xOfficeContext;
47     }
48 
49     /** creates and instantiates new document
50 	*/
createDocument( com.sun.star.uno.XComponentContext xOfficeContext, String sURL, String sTargetFrame, int nSearchFlags, com.sun.star.beans.PropertyValue[] aArgs )51 	static public com.sun.star.lang.XComponent createDocument(
52         com.sun.star.uno.XComponentContext xOfficeContext,
53 		String sURL, String sTargetFrame, int nSearchFlags,
54         com.sun.star.beans.PropertyValue[] aArgs )
55 			throws Exception
56 	{
57 		com.sun.star.lang.XComponent xComponent = null;
58 		com.sun.star.frame.XComponentLoader aLoader =
59             (com.sun.star.frame.XComponentLoader)UnoRuntime.queryInterface(
60                 com.sun.star.frame.XComponentLoader.class,
61 				xOfficeContext.getServiceManager().createInstanceWithContext(
62                     "com.sun.star.frame.Desktop", xOfficeContext));
63 
64 		xComponent = (com.sun.star.lang.XComponent)UnoRuntime.queryInterface(
65             com.sun.star.lang.XComponent.class, aLoader.loadComponentFromURL(
66 				sURL, sTargetFrame, nSearchFlags, aArgs ) );
67 
68 		if ( xComponent == null )
69 			throw new Exception( "could not create document: " + sURL );
70 		return xComponent;
71 	}
72 }
73