xref: /AOO41X/main/odk/examples/java/DocumentHandling/DocumentConverter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import java.io.File;
38*cdf0e10cSrcweir import java.io.FileFilter;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir /** The class <CODE>DocumentConverter</CODE> allows you to convert all documents
42*cdf0e10cSrcweir  * in a given directory and in its subdirectories to a given type. A converted
43*cdf0e10cSrcweir  * document will be created in the same directory as the origin document.
44*cdf0e10cSrcweir  *
45*cdf0e10cSrcweir  */
46*cdf0e10cSrcweir public class DocumentConverter {
47*cdf0e10cSrcweir     /** Containing the loaded documents
48*cdf0e10cSrcweir      */
49*cdf0e10cSrcweir     static com.sun.star.frame.XComponentLoader xCompLoader = null;
50*cdf0e10cSrcweir     /** Containing the given type to convert to
51*cdf0e10cSrcweir      */
52*cdf0e10cSrcweir     static String sConvertType = "";
53*cdf0e10cSrcweir     /** Containing the given extension
54*cdf0e10cSrcweir      */
55*cdf0e10cSrcweir     static String sExtension = "";
56*cdf0e10cSrcweir     /** Containing the current file or directory
57*cdf0e10cSrcweir      */
58*cdf0e10cSrcweir     static String sIndent = "";
59*cdf0e10cSrcweir     /** Containing the directory where the converted files are saved
60*cdf0e10cSrcweir      */
61*cdf0e10cSrcweir     static String sOutputDir = "";
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /** Traversing the given directory recursively and converting their files to
64*cdf0e10cSrcweir      * the favoured type if possible
65*cdf0e10cSrcweir      * @param fileDirectory Containing the directory
66*cdf0e10cSrcweir      */
67*cdf0e10cSrcweir     static void traverse( File fileDirectory ) {
68*cdf0e10cSrcweir         // Testing, if the file is a directory, and if so, it throws an exception
69*cdf0e10cSrcweir         if ( !fileDirectory.isDirectory() ) {
70*cdf0e10cSrcweir             throw new IllegalArgumentException(
71*cdf0e10cSrcweir                 "not a directory: " + fileDirectory.getName()
72*cdf0e10cSrcweir                 );
73*cdf0e10cSrcweir         }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         // Prepare Url for the output directory
76*cdf0e10cSrcweir         File outdir = new File(DocumentConverter.sOutputDir);
77*cdf0e10cSrcweir         String sOutUrl = "file:///" + outdir.getAbsolutePath().replace( '\\', '/' );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         System.out.println("\nThe converted documents will stored in \""
80*cdf0e10cSrcweir                            + outdir.getPath() + "!");
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         System.out.println(sIndent + "[" + fileDirectory.getName() + "]");
83*cdf0e10cSrcweir         sIndent += "  ";
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         // Getting all files and directories in the current directory
86*cdf0e10cSrcweir         File[] entries = fileDirectory.listFiles();
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         // Iterating for each file and directory
90*cdf0e10cSrcweir         for ( int i = 0; i < entries.length; ++i ) {
91*cdf0e10cSrcweir             // Testing, if the entry in the list is a directory
92*cdf0e10cSrcweir             if ( entries[ i ].isDirectory() ) {
93*cdf0e10cSrcweir                 // Recursive call for the new directory
94*cdf0e10cSrcweir                 traverse( entries[ i ] );
95*cdf0e10cSrcweir             } else {
96*cdf0e10cSrcweir                 // Converting the document to the favoured type
97*cdf0e10cSrcweir                 try {
98*cdf0e10cSrcweir                     // Composing the URL by replacing all backslashs
99*cdf0e10cSrcweir                     String sUrl = "file:///"
100*cdf0e10cSrcweir                         + entries[ i ].getAbsolutePath().replace( '\\', '/' );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir                     // Loading the wanted document
103*cdf0e10cSrcweir                     com.sun.star.beans.PropertyValue propertyValues[] =
104*cdf0e10cSrcweir                         new com.sun.star.beans.PropertyValue[1];
105*cdf0e10cSrcweir                     propertyValues[0] = new com.sun.star.beans.PropertyValue();
106*cdf0e10cSrcweir                     propertyValues[0].Name = "Hidden";
107*cdf0e10cSrcweir                     propertyValues[0].Value = new Boolean(true);
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir                     Object oDocToStore =
110*cdf0e10cSrcweir                         DocumentConverter.xCompLoader.loadComponentFromURL(
111*cdf0e10cSrcweir                             sUrl, "_blank", 0, propertyValues);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir                     // Getting an object that will offer a simple way to store
114*cdf0e10cSrcweir                     // a document to a URL.
115*cdf0e10cSrcweir                     com.sun.star.frame.XStorable xStorable =
116*cdf0e10cSrcweir                         (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
117*cdf0e10cSrcweir                             com.sun.star.frame.XStorable.class, oDocToStore );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir                     // Preparing properties for converting the document
120*cdf0e10cSrcweir                     propertyValues = new com.sun.star.beans.PropertyValue[2];
121*cdf0e10cSrcweir                     // Setting the flag for overwriting
122*cdf0e10cSrcweir                     propertyValues[0] = new com.sun.star.beans.PropertyValue();
123*cdf0e10cSrcweir                     propertyValues[0].Name = "Overwrite";
124*cdf0e10cSrcweir                     propertyValues[0].Value = new Boolean(true);
125*cdf0e10cSrcweir                     // Setting the filter name
126*cdf0e10cSrcweir                     propertyValues[1] = new com.sun.star.beans.PropertyValue();
127*cdf0e10cSrcweir                     propertyValues[1].Name = "FilterName";
128*cdf0e10cSrcweir                     propertyValues[1].Value = DocumentConverter.sConvertType;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir                     // Appending the favoured extension to the origin document name
131*cdf0e10cSrcweir                     int index1 = sUrl.lastIndexOf('/');
132*cdf0e10cSrcweir                     int index2 = sUrl.lastIndexOf('.');
133*cdf0e10cSrcweir                     String sStoreUrl = sOutUrl + sUrl.substring(index1, index2 + 1)
134*cdf0e10cSrcweir                         + DocumentConverter.sExtension;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir                     // Storing and converting the document
137*cdf0e10cSrcweir                     xStorable.storeAsURL(sStoreUrl, propertyValues);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir                     // Closing the converted document. Use XCloseable.clsoe if the
140*cdf0e10cSrcweir                     // interface is supported, otherwise use XComponent.dispose
141*cdf0e10cSrcweir                     com.sun.star.util.XCloseable xCloseable =
142*cdf0e10cSrcweir                         (com.sun.star.util.XCloseable)UnoRuntime.queryInterface(
143*cdf0e10cSrcweir                             com.sun.star.util.XCloseable.class, xStorable);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir                     if ( xCloseable != null ) {
146*cdf0e10cSrcweir                         xCloseable.close(false);
147*cdf0e10cSrcweir                     } else {
148*cdf0e10cSrcweir                         com.sun.star.lang.XComponent xComp =
149*cdf0e10cSrcweir                             (com.sun.star.lang.XComponent)UnoRuntime.queryInterface(
150*cdf0e10cSrcweir                                 com.sun.star.lang.XComponent.class, xStorable);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir                         xComp.dispose();
153*cdf0e10cSrcweir                     }
154*cdf0e10cSrcweir                 }
155*cdf0e10cSrcweir                 catch( Exception e ) {
156*cdf0e10cSrcweir                     e.printStackTrace(System.err);
157*cdf0e10cSrcweir                 }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir                 System.out.println(sIndent + entries[ i ].getName());
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         sIndent = sIndent.substring(2);
164*cdf0e10cSrcweir     }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     /** Bootstrap UNO, getting the remote component context, getting a new instance
167*cdf0e10cSrcweir      * of the desktop (used interface XComponentLoader) and calling the
168*cdf0e10cSrcweir      * static method traverse
169*cdf0e10cSrcweir      * @param args The array of the type String contains the directory, in which
170*cdf0e10cSrcweir      *             all files should be converted, the favoured converting type
171*cdf0e10cSrcweir      *             and the wanted extension
172*cdf0e10cSrcweir      */
173*cdf0e10cSrcweir     public static void main( String args[] ) {
174*cdf0e10cSrcweir         if ( args.length < 3 ) {
175*cdf0e10cSrcweir             System.out.println("usage: java -jar DocumentConverter.jar " +
176*cdf0e10cSrcweir                 "\"<directory to convert>\" \"<type to convert to>\" " +
177*cdf0e10cSrcweir                 "\"<extension>\" \"<output_directory>\"");
178*cdf0e10cSrcweir             System.out.println("\ne.g.:");
179*cdf0e10cSrcweir             System.out.println("usage: java -jar DocumentConverter.jar " +
180*cdf0e10cSrcweir                 "\"c:/myoffice\" \"swriter: MS Word 97\" \"doc\"");
181*cdf0e10cSrcweir             System.exit(1);
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         com.sun.star.uno.XComponentContext xContext = null;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         try {
187*cdf0e10cSrcweir             // get the remote office component context
188*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
189*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir             // get the remote office service manager
192*cdf0e10cSrcweir             com.sun.star.lang.XMultiComponentFactory xMCF =
193*cdf0e10cSrcweir                 xContext.getServiceManager();
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             Object oDesktop = xMCF.createInstanceWithContext(
196*cdf0e10cSrcweir                 "com.sun.star.frame.Desktop", xContext);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir             xCompLoader = (com.sun.star.frame.XComponentLoader)
199*cdf0e10cSrcweir                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
200*cdf0e10cSrcweir                                           oDesktop);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir             // Getting the given starting directory
203*cdf0e10cSrcweir             File file = new File(args[0]);
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir             // Getting the given type to convert to
206*cdf0e10cSrcweir             sConvertType = args[1];
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir             // Getting the given extension that should be appended to the
209*cdf0e10cSrcweir             // origin document
210*cdf0e10cSrcweir             sExtension = args[2];
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             // Getting the given type to convert to
213*cdf0e10cSrcweir             sOutputDir = args[3];
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir             // Starting the conversion of documents in the given directory
216*cdf0e10cSrcweir             // and subdirectories
217*cdf0e10cSrcweir             traverse(file);
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir             System.exit(0);
220*cdf0e10cSrcweir         } catch( Exception e ) {
221*cdf0e10cSrcweir             e.printStackTrace(System.err);
222*cdf0e10cSrcweir             System.exit(1);
223*cdf0e10cSrcweir         }
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir }
226