xref: /AOO41X/main/odk/examples/java/DocumentHandling/DocumentSaver.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.uno.UnoRuntime;
36 
37 
38 /** The purpose of this class is to open a specified text document and save this
39  * file to a specified URL. The type of the saved file is
40  * "swriter: StarOffice XML (Writer)".
41  */
42 public class DocumentSaver {
43     /** The main method of the application.
44      * @param args The program needs two arguments:
45      * - full file name to open,
46      * - full file name to save.
47      */
48     public static void main(String args[]) {
49         if ( args.length < 2 ) {
50             System.out.println("usage: java -jar DocumentSaver.jar" +
51                                "\"<URL|path to load>\" \"<URL|path to save>\"");
52             System.out.println("\ne.g.:");
53             System.out.println("java -jar DocumentSaver " +
54                                "\"file:///f:/TestPrint.doc\"" +
55                                "\"file:///f:/TestPrint.odt\"");
56             System.exit(1);
57         }
58 
59         com.sun.star.uno.XComponentContext xContext = null;
60 
61         try {
62             // get the remote office component context
63             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
64             System.out.println("Connected to a running office ...");
65 
66             // get the remote office service manager
67             com.sun.star.lang.XMultiComponentFactory xMCF =
68                 xContext.getServiceManager();
69 
70             Object oDesktop = xMCF.createInstanceWithContext(
71                 "com.sun.star.frame.Desktop", xContext);
72 
73             com.sun.star.frame.XComponentLoader xCompLoader =
74                 (com.sun.star.frame.XComponentLoader)
75                      UnoRuntime.queryInterface(
76                          com.sun.star.frame.XComponentLoader.class, oDesktop);
77 
78             java.io.File sourceFile = new java.io.File(args[0]);
79             StringBuffer sLoadUrl = new StringBuffer("file:///");
80             sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
81 
82             sourceFile = new java.io.File(args[1]);
83             StringBuffer sSaveUrl = new StringBuffer("file:///");
84             sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
85 
86             com.sun.star.beans.PropertyValue[] propertyValue =
87                 new com.sun.star.beans.PropertyValue[1];
88             propertyValue[0] = new com.sun.star.beans.PropertyValue();
89             propertyValue[0].Name = "Hidden";
90             propertyValue[0].Value = new Boolean(true);
91 
92             Object oDocToStore = xCompLoader.loadComponentFromURL(
93                 sLoadUrl.toString(), "_blank", 0, propertyValue );
94             com.sun.star.frame.XStorable xStorable =
95                 (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
96                     com.sun.star.frame.XStorable.class, oDocToStore );
97 
98             propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
99             propertyValue[0] = new com.sun.star.beans.PropertyValue();
100             propertyValue[0].Name = "Overwrite";
101             propertyValue[0].Value = new Boolean(true);
102             propertyValue[1] = new com.sun.star.beans.PropertyValue();
103             propertyValue[1].Name = "FilterName";
104             propertyValue[1].Value = "StarOffice XML (Writer)";
105             xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
106 
107             System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
108                                sSaveUrl + "\"\n");
109 
110             com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
111                 UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
112                                           oDocToStore );
113 
114             if (xCloseable != null ) {
115                 xCloseable.close(false);
116             } else
117             {
118                 com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
119                     UnoRuntime.queryInterface(
120                         com.sun.star.lang.XComponent.class, oDocToStore );
121                 xComp.dispose();
122             }
123             System.out.println("document closed!");
124             System.exit(0);
125         }
126         catch( Exception e ) {
127             e.printStackTrace(System.err);
128             System.exit(1);
129         }
130     }
131 }
132