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