1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /************************************************************************* 3 * 4 * The Contents of this file are made available subject to the terms of 5 * the BSD license. 6 * 7 * Copyright 2000, 2010 Oracle and/or its affiliates. 8 * All rights reserved. 9 * 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. 21 * 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. 33 * 34 *************************************************************************/ 35 36 import com.sun.star.uno.UnoRuntime; 37 38 import java.io.File; 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 */ traverse( File fileDirectory )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 backslashes 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 = 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 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 = 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.close if the 140 // interface is supported, otherwise use XComponent.dispose 141 com.sun.star.util.XCloseable xCloseable = 142 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 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 */ main( String args[] )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 = UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, 199 oDesktop); 200 201 // Getting the given starting directory 202 File file = new File(args[0]); 203 204 // Getting the given type to convert to 205 sConvertType = args[1]; 206 207 // Getting the given extension that should be appended to the 208 // origin document 209 sExtension = args[2]; 210 211 // Getting the given type to convert to 212 sOutputDir = args[3]; 213 214 // Starting the conversion of documents in the given directory 215 // and subdirectories 216 traverse(file); 217 218 System.exit(0); 219 } catch( Exception e ) { 220 e.printStackTrace(System.err); 221 System.exit(1); 222 } 223 } 224 } 225 226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 227