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