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 import com.sun.star.uno.UnoRuntime;
25 
26 
27 /** This class opens a new or an existing office document.
28  */
29 public class DocumentLoader {
main(String args[])30     public static void main(String args[]) {
31         if ( args.length < 1 ) {
32             System.out.println(
33                 "usage: java -jar DocumentLoader.jar \"<URL|path>\"" );
34             System.out.println( "\ne.g.:" );
35             System.out.println(
36                 "java -jar DocumentLoader.jar \"private:factory/swriter\"" );
37             System.exit(1);
38         }
39 
40         com.sun.star.uno.XComponentContext xContext = null;
41 
42         try {
43             // get the remote office component context
44             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
45             System.out.println("Connected to a running office ...");
46 
47             // get the remote office service manager
48             com.sun.star.lang.XMultiComponentFactory xMCF =
49                 xContext.getServiceManager();
50 
51             Object oDesktop = xMCF.createInstanceWithContext(
52                 "com.sun.star.frame.Desktop", xContext);
53 
54             com.sun.star.frame.XComponentLoader xCompLoader =
55                 (com.sun.star.frame.XComponentLoader)
56                      UnoRuntime.queryInterface(
57                          com.sun.star.frame.XComponentLoader.class, oDesktop);
58 
59             String sUrl = args[0];
60             if ( sUrl.indexOf("private:") != 0) {
61                 java.io.File sourceFile = new java.io.File(args[0]);
62                 StringBuffer sbTmp = new StringBuffer("file:///");
63                 sbTmp.append(sourceFile.getCanonicalPath().replace('\\', '/'));
64                 sUrl = sbTmp.toString();
65             }
66 
67             // Load a Writer document, which will be automatically displayed
68             com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL(
69                 sUrl, "_blank", 0, new com.sun.star.beans.PropertyValue[0]);
70 
71             if ( xComp != null )
72                 System.exit(0);
73             else
74                 System.exit(1);
75         }
76         catch( Exception e ) {
77             e.printStackTrace(System.err);
78             System.exit(1);
79         }
80     }
81 }
82