1 /*************************************************************************
3 * $RCSfile: DocumentConverter.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:08:23 $
9 * The Contents of this file are made available subject to the terms of
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
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 import java
.io
.FileFilter
;
47 /** The class <CODE>DocumentConverter</CODE> allows you to convert all documents
48 * in a given directory and in its subdirectories to a given type. A converted
49 * document will be created in the same directory as the origin document.
52 public class DocumentConverter
{
53 /** Containing the loaded documents
55 static com
.sun
.star
.frame
.XComponentLoader xCompLoader
= null;
56 /** Containing the given type to convert to
58 static String sConvertType
= "";
59 /** Containing the given extension
61 static String sExtension
= "";
62 /** Containing the current file or directory
64 static String sIndent
= "";
65 /** Containing the directory where the converted files are saved
67 static String sOutputDir
= "";
69 /** Traversing the given directory recursively and converting their files to
70 * the favoured type if possible
71 * @param fileDirectory Containing the directory
73 static void traverse( File fileDirectory
) {
74 // Testing, if the file is a directory, and if so, it throws an exception
75 if ( !fileDirectory
.isDirectory() ) {
76 throw new IllegalArgumentException(
77 "not a directory: " + fileDirectory
.getName()
81 // Prepare Url for the output directory
82 File outdir
= new File(DocumentConverter
.sOutputDir
);
83 String sOutUrl
= "file:///" + outdir
.getAbsolutePath().replace( '\\', '/' );
85 System
.out
.println("\nThe converted documents will stored in \""
86 + outdir
.getPath() + "!");
88 System
.out
.println(sIndent
+ "[" + fileDirectory
.getName() + "]");
91 // Getting all files and directories in the current directory
92 File
[] entries
= fileDirectory
.listFiles();
95 // Iterating for each file and directory
96 for ( int i
= 0; i
< entries
.length
; ++i
) {
97 // Testing, if the entry in the list is a directory
98 if ( entries
[ i
].isDirectory() ) {
99 // Recursive call for the new directory
100 traverse( entries
[ i
] );
102 // Converting the document to the favoured type
104 // Composing the URL by replacing all backslashs
105 String sUrl
= "file:///"
106 + entries
[ i
].getAbsolutePath().replace( '\\', '/' );
108 // Loading the wanted document
109 com
.sun
.star
.beans
.PropertyValue propertyValues
[] =
110 new com
.sun
.star
.beans
.PropertyValue
[1];
111 propertyValues
[0] = new com
.sun
.star
.beans
.PropertyValue();
112 propertyValues
[0].Name
= "Hidden";
113 propertyValues
[0].Value
= new Boolean(true);
116 DocumentConverter
.xCompLoader
.loadComponentFromURL(
117 sUrl
, "_blank", 0, propertyValues
);
119 // Getting an object that will offer a simple way to store
120 // a document to a URL.
121 com
.sun
.star
.frame
.XStorable xStorable
=
122 (com
.sun
.star
.frame
.XStorable
)UnoRuntime
.queryInterface(
123 com
.sun
.star
.frame
.XStorable
.class, oDocToStore
);
125 // Preparing properties for converting the document
126 propertyValues
= new com
.sun
.star
.beans
.PropertyValue
[2];
127 // Setting the flag for overwriting
128 propertyValues
[0] = new com
.sun
.star
.beans
.PropertyValue();
129 propertyValues
[0].Name
= "Overwrite";
130 propertyValues
[0].Value
= new Boolean(true);
131 // Setting the filter name
132 propertyValues
[1] = new com
.sun
.star
.beans
.PropertyValue();
133 propertyValues
[1].Name
= "FilterName";
134 propertyValues
[1].Value
= DocumentConverter
.sConvertType
;
136 // Appending the favoured extension to the origin document name
137 int index1
= sUrl
.lastIndexOf('/');
138 int index2
= sUrl
.lastIndexOf('.');
139 String sStoreUrl
= sOutUrl
+ sUrl
.substring(index1
, index2
+ 1)
140 + DocumentConverter
.sExtension
;
142 // Storing and converting the document
143 xStorable
.storeAsURL(sStoreUrl
, propertyValues
);
145 // Closing the converted document. Use XCloseable.clsoe if the
146 // interface is supported, otherwise use XComponent.dispose
147 com
.sun
.star
.util
.XCloseable xCloseable
=
148 (com
.sun
.star
.util
.XCloseable
)UnoRuntime
.queryInterface(
149 com
.sun
.star
.util
.XCloseable
.class, xStorable
);
151 if ( xCloseable
!= null ) {
152 xCloseable
.close(false);
154 com
.sun
.star
.lang
.XComponent xComp
=
155 (com
.sun
.star
.lang
.XComponent
)UnoRuntime
.queryInterface(
156 com
.sun
.star
.lang
.XComponent
.class, xStorable
);
161 catch( Exception e
) {
162 e
.printStackTrace(System
.err
);
165 System
.out
.println(sIndent
+ entries
[ i
].getName());
169 sIndent
= sIndent
.substring(2);
172 /** Bootstrap UNO, getting the remote component context, getting a new instance
173 * of the desktop (used interface XComponentLoader) and calling the
174 * static method traverse
175 * @param args The array of the type String contains the directory, in which
176 * all files should be converted, the favoured converting type
177 * and the wanted extension
179 public static void main( String args
[] ) {
180 if ( args
.length
< 3 ) {
181 System
.out
.println("usage: java -jar DocumentConverter.jar " +
182 "\"<directory to convert>\" \"<type to convert to>\" " +
183 "\"<extension>\" \"<output_directory>\"");
184 System
.out
.println("\ne.g.:");
185 System
.out
.println("usage: java -jar DocumentConverter.jar " +
186 "\"c:/myoffice\" \"swriter: MS Word 97\" \"doc\"");
190 com
.sun
.star
.uno
.XComponentContext xContext
= null;
193 // get the remote office component context
194 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
195 System
.out
.println("Connected to a running office ...");
197 // get the remote office service manager
198 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
199 xContext
.getServiceManager();
201 Object oDesktop
= xMCF
.createInstanceWithContext(
202 "com.sun.star.frame.Desktop", xContext
);
204 xCompLoader
= (com
.sun
.star
.frame
.XComponentLoader
)
205 UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XComponentLoader
.class,
208 // Getting the given starting directory
209 File file
= new File(args
[0]);
211 // Getting the given type to convert to
212 sConvertType
= args
[1];
214 // Getting the given extension that should be appended to the
216 sExtension
= args
[2];
218 // Getting the given type to convert to
219 sOutputDir
= args
[3];
221 // Starting the conversion of documents in the given directory
222 // and subdirectories
226 } catch( Exception e
) {
227 e
.printStackTrace(System
.err
);