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 /** The purpose of this class is to open a specified text document and save this
28  * file to a specified URL. The type of the saved file is
29  * "swriter: StarOffice XML (Writer)".
30  */
31 public class DocumentSaver {
32     /** The main method of the application.
33      * @param args The program needs two arguments:
34      * - full file name to open,
35      * - full file name to save.
36      */
main(String args[])37     public static void main(String args[]) {
38         if ( args.length < 2 ) {
39             System.out.println("usage: java -jar DocumentSaver.jar" +
40                                "\"<URL|path to load>\" \"<URL|path to save>\"");
41             System.out.println("\ne.g.:");
42             System.out.println("java -jar DocumentSaver " +
43                                "\"file:///f:/TestPrint.doc\"" +
44                                "\"file:///f:/TestPrint.odt\"");
45             System.exit(1);
46         }
47 
48         com.sun.star.uno.XComponentContext xContext = null;
49 
50         try {
51             // get the remote office component context
52             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
53             System.out.println("Connected to a running office ...");
54 
55             // get the remote office service manager
56             com.sun.star.lang.XMultiComponentFactory xMCF =
57                 xContext.getServiceManager();
58 
59             Object oDesktop = xMCF.createInstanceWithContext(
60                 "com.sun.star.frame.Desktop", xContext);
61 
62             com.sun.star.frame.XComponentLoader xCompLoader =
63                 (com.sun.star.frame.XComponentLoader)
64                      UnoRuntime.queryInterface(
65                          com.sun.star.frame.XComponentLoader.class, oDesktop);
66 
67             java.io.File sourceFile = new java.io.File(args[0]);
68             StringBuffer sLoadUrl = new StringBuffer("file:///");
69             sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
70 
71             sourceFile = new java.io.File(args[1]);
72             StringBuffer sSaveUrl = new StringBuffer("file:///");
73             sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
74 
75             com.sun.star.beans.PropertyValue[] propertyValue =
76                 new com.sun.star.beans.PropertyValue[1];
77             propertyValue[0] = new com.sun.star.beans.PropertyValue();
78             propertyValue[0].Name = "Hidden";
79             propertyValue[0].Value = new Boolean(true);
80 
81             Object oDocToStore = xCompLoader.loadComponentFromURL(
82                 sLoadUrl.toString(), "_blank", 0, propertyValue );
83             com.sun.star.frame.XStorable xStorable =
84                 (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
85                     com.sun.star.frame.XStorable.class, oDocToStore );
86 
87             propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
88             propertyValue[0] = new com.sun.star.beans.PropertyValue();
89             propertyValue[0].Name = "Overwrite";
90             propertyValue[0].Value = new Boolean(true);
91             propertyValue[1] = new com.sun.star.beans.PropertyValue();
92             propertyValue[1].Name = "FilterName";
93             propertyValue[1].Value = "StarOffice XML (Writer)";
94             xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
95 
96             System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
97                                sSaveUrl + "\"\n");
98 
99             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
100                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
101                                           oDocToStore );
102 
103             if (xCloseable != null ) {
104                 xCloseable.close(false);
105             } else
106             {
107                 com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
108                     UnoRuntime.queryInterface(
109                         com.sun.star.lang.XComponent.class, oDocToStore );
110                 xComp.dispose();
111             }
112             System.out.println("document closed!");
113             System.exit(0);
114         }
115         catch( Exception e ) {
116             e.printStackTrace(System.err);
117             System.exit(1);
118         }
119     }
120 }
121