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