1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /** The purpose of this class is to open a specified text document and save this
28cdf0e10cSrcweir  * file to a specified URL. The type of the saved file is
29cdf0e10cSrcweir  * "swriter: StarOffice XML (Writer)".
30cdf0e10cSrcweir  */
31cdf0e10cSrcweir public class DocumentSaver {
32cdf0e10cSrcweir     /** The main method of the application.
33cdf0e10cSrcweir      * @param args The program needs two arguments:
34cdf0e10cSrcweir      * - full file name to open,
35cdf0e10cSrcweir      * - full file name to save.
36cdf0e10cSrcweir      */
main(String args[])37cdf0e10cSrcweir     public static void main(String args[]) {
38cdf0e10cSrcweir         if ( args.length < 2 ) {
39cdf0e10cSrcweir             System.out.println("usage: java -jar DocumentSaver.jar" +
40cdf0e10cSrcweir                                "\"<URL|path to load>\" \"<URL|path to save>\"");
41cdf0e10cSrcweir             System.out.println("\ne.g.:");
42cdf0e10cSrcweir             System.out.println("java -jar DocumentSaver " +
43cdf0e10cSrcweir                                "\"file:///f:/TestPrint.doc\"" +
44cdf0e10cSrcweir                                "\"file:///f:/TestPrint.odt\"");
45cdf0e10cSrcweir             System.exit(1);
46cdf0e10cSrcweir         }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         com.sun.star.uno.XComponentContext xContext = null;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         try {
51cdf0e10cSrcweir             // get the remote office component context
52cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
53cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
54cdf0e10cSrcweir 
55cdf0e10cSrcweir             // get the remote office service manager
56cdf0e10cSrcweir             com.sun.star.lang.XMultiComponentFactory xMCF =
57cdf0e10cSrcweir                 xContext.getServiceManager();
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             Object oDesktop = xMCF.createInstanceWithContext(
60cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xContext);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir             com.sun.star.frame.XComponentLoader xCompLoader =
63cdf0e10cSrcweir                 (com.sun.star.frame.XComponentLoader)
64cdf0e10cSrcweir                      UnoRuntime.queryInterface(
65cdf0e10cSrcweir                          com.sun.star.frame.XComponentLoader.class, oDesktop);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             java.io.File sourceFile = new java.io.File(args[0]);
68cdf0e10cSrcweir             StringBuffer sLoadUrl = new StringBuffer("file:///");
69cdf0e10cSrcweir             sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             sourceFile = new java.io.File(args[1]);
72cdf0e10cSrcweir             StringBuffer sSaveUrl = new StringBuffer("file:///");
73cdf0e10cSrcweir             sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             com.sun.star.beans.PropertyValue[] propertyValue =
76cdf0e10cSrcweir                 new com.sun.star.beans.PropertyValue[1];
77cdf0e10cSrcweir             propertyValue[0] = new com.sun.star.beans.PropertyValue();
78cdf0e10cSrcweir             propertyValue[0].Name = "Hidden";
79cdf0e10cSrcweir             propertyValue[0].Value = new Boolean(true);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir             Object oDocToStore = xCompLoader.loadComponentFromURL(
82cdf0e10cSrcweir                 sLoadUrl.toString(), "_blank", 0, propertyValue );
83cdf0e10cSrcweir             com.sun.star.frame.XStorable xStorable =
84cdf0e10cSrcweir                 (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
85cdf0e10cSrcweir                     com.sun.star.frame.XStorable.class, oDocToStore );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
88cdf0e10cSrcweir             propertyValue[0] = new com.sun.star.beans.PropertyValue();
89cdf0e10cSrcweir             propertyValue[0].Name = "Overwrite";
90cdf0e10cSrcweir             propertyValue[0].Value = new Boolean(true);
91cdf0e10cSrcweir             propertyValue[1] = new com.sun.star.beans.PropertyValue();
92cdf0e10cSrcweir             propertyValue[1].Name = "FilterName";
93cdf0e10cSrcweir             propertyValue[1].Value = "StarOffice XML (Writer)";
94cdf0e10cSrcweir             xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir             System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
97cdf0e10cSrcweir                                sSaveUrl + "\"\n");
98cdf0e10cSrcweir 
99cdf0e10cSrcweir             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
100cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
101cdf0e10cSrcweir                                           oDocToStore );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             if (xCloseable != null ) {
104cdf0e10cSrcweir                 xCloseable.close(false);
105cdf0e10cSrcweir             } else
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
108cdf0e10cSrcweir                     UnoRuntime.queryInterface(
109cdf0e10cSrcweir                         com.sun.star.lang.XComponent.class, oDocToStore );
110cdf0e10cSrcweir                 xComp.dispose();
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir             System.out.println("document closed!");
113cdf0e10cSrcweir             System.exit(0);
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir         catch( Exception e ) {
116cdf0e10cSrcweir             e.printStackTrace(System.err);
117cdf0e10cSrcweir             System.exit(1);
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir }
121