1*34dd1e25SAndrew Rist /************************************************************** 2*34dd1e25SAndrew Rist * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*34dd1e25SAndrew Rist * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*34dd1e25SAndrew Rist * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19*34dd1e25SAndrew Rist * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.File; 27cdf0e10cSrcweir import java.io.FileFilter; 28cdf0e10cSrcweir 29cdf0e10cSrcweir 30cdf0e10cSrcweir /** The class <CODE>DocumentConverter</CODE> allows you to convert all documents 31cdf0e10cSrcweir * in a given directory and in its subdirectories to a given type. A converted 32cdf0e10cSrcweir * document will be created in the same directory as the origin document. 33cdf0e10cSrcweir * 34cdf0e10cSrcweir */ 35cdf0e10cSrcweir public class DocumentConverter { 36cdf0e10cSrcweir /** Containing the loaded documents 37cdf0e10cSrcweir */ 38cdf0e10cSrcweir static com.sun.star.frame.XComponentLoader xCompLoader = null; 39cdf0e10cSrcweir /** Containing the given type to convert to 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir static String sConvertType = ""; 42cdf0e10cSrcweir /** Containing the given extension 43cdf0e10cSrcweir */ 44cdf0e10cSrcweir static String sExtension = ""; 45cdf0e10cSrcweir /** Containing the current file or directory 46cdf0e10cSrcweir */ 47cdf0e10cSrcweir static String sIndent = ""; 48cdf0e10cSrcweir /** Containing the directory where the converted files are saved 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir static String sOutputDir = ""; 51cdf0e10cSrcweir 52cdf0e10cSrcweir /** Traversing the given directory recursively and converting their files to 53cdf0e10cSrcweir * the favoured type if possible 54cdf0e10cSrcweir * @param fileDirectory Containing the directory 55cdf0e10cSrcweir */ traverse( File fileDirectory )56cdf0e10cSrcweir static void traverse( File fileDirectory ) { 57cdf0e10cSrcweir // Testing, if the file is a directory, and if so, it throws an exception 58cdf0e10cSrcweir if ( !fileDirectory.isDirectory() ) { 59cdf0e10cSrcweir throw new IllegalArgumentException( 60cdf0e10cSrcweir "not a directory: " + fileDirectory.getName() 61cdf0e10cSrcweir ); 62cdf0e10cSrcweir } 63cdf0e10cSrcweir 64cdf0e10cSrcweir // Prepare Url for the output directory 65cdf0e10cSrcweir File outdir = new File(DocumentConverter.sOutputDir); 66cdf0e10cSrcweir String sOutUrl = "file:///" + outdir.getAbsolutePath().replace( '\\', '/' ); 67cdf0e10cSrcweir 68cdf0e10cSrcweir System.out.println("\nThe converted documents will stored in \"" 69cdf0e10cSrcweir + outdir.getPath() + "!"); 70cdf0e10cSrcweir 71cdf0e10cSrcweir System.out.println(sIndent + "[" + fileDirectory.getName() + "]"); 72cdf0e10cSrcweir sIndent += " "; 73cdf0e10cSrcweir 74cdf0e10cSrcweir // Getting all files and directories in the current directory 75cdf0e10cSrcweir File[] entries = fileDirectory.listFiles(); 76cdf0e10cSrcweir 77cdf0e10cSrcweir 78cdf0e10cSrcweir // Iterating for each file and directory 79cdf0e10cSrcweir for ( int i = 0; i < entries.length; ++i ) { 80cdf0e10cSrcweir // Testing, if the entry in the list is a directory 81cdf0e10cSrcweir if ( entries[ i ].isDirectory() ) { 82cdf0e10cSrcweir // Recursive call for the new directory 83cdf0e10cSrcweir traverse( entries[ i ] ); 84cdf0e10cSrcweir } else { 85cdf0e10cSrcweir // Converting the document to the favoured type 86cdf0e10cSrcweir try { 87cdf0e10cSrcweir // Composing the URL by replacing all backslashs 88cdf0e10cSrcweir String sUrl = "file:///" 89cdf0e10cSrcweir + entries[ i ].getAbsolutePath().replace( '\\', '/' ); 90cdf0e10cSrcweir 91cdf0e10cSrcweir // Loading the wanted document 92cdf0e10cSrcweir com.sun.star.beans.PropertyValue propertyValues[] = 93cdf0e10cSrcweir new com.sun.star.beans.PropertyValue[1]; 94cdf0e10cSrcweir propertyValues[0] = new com.sun.star.beans.PropertyValue(); 95cdf0e10cSrcweir propertyValues[0].Name = "Hidden"; 96cdf0e10cSrcweir propertyValues[0].Value = new Boolean(true); 97cdf0e10cSrcweir 98cdf0e10cSrcweir Object oDocToStore = 99cdf0e10cSrcweir DocumentConverter.xCompLoader.loadComponentFromURL( 100cdf0e10cSrcweir sUrl, "_blank", 0, propertyValues); 101cdf0e10cSrcweir 102cdf0e10cSrcweir // Getting an object that will offer a simple way to store 103cdf0e10cSrcweir // a document to a URL. 104cdf0e10cSrcweir com.sun.star.frame.XStorable xStorable = 105cdf0e10cSrcweir (com.sun.star.frame.XStorable)UnoRuntime.queryInterface( 106cdf0e10cSrcweir com.sun.star.frame.XStorable.class, oDocToStore ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir // Preparing properties for converting the document 109cdf0e10cSrcweir propertyValues = new com.sun.star.beans.PropertyValue[2]; 110cdf0e10cSrcweir // Setting the flag for overwriting 111cdf0e10cSrcweir propertyValues[0] = new com.sun.star.beans.PropertyValue(); 112cdf0e10cSrcweir propertyValues[0].Name = "Overwrite"; 113cdf0e10cSrcweir propertyValues[0].Value = new Boolean(true); 114cdf0e10cSrcweir // Setting the filter name 115cdf0e10cSrcweir propertyValues[1] = new com.sun.star.beans.PropertyValue(); 116cdf0e10cSrcweir propertyValues[1].Name = "FilterName"; 117cdf0e10cSrcweir propertyValues[1].Value = DocumentConverter.sConvertType; 118cdf0e10cSrcweir 119cdf0e10cSrcweir // Appending the favoured extension to the origin document name 120cdf0e10cSrcweir int index1 = sUrl.lastIndexOf('/'); 121cdf0e10cSrcweir int index2 = sUrl.lastIndexOf('.'); 122cdf0e10cSrcweir String sStoreUrl = sOutUrl + sUrl.substring(index1, index2 + 1) 123cdf0e10cSrcweir + DocumentConverter.sExtension; 124cdf0e10cSrcweir 125cdf0e10cSrcweir // Storing and converting the document 126cdf0e10cSrcweir xStorable.storeAsURL(sStoreUrl, propertyValues); 127cdf0e10cSrcweir 128cdf0e10cSrcweir // Closing the converted document. Use XCloseable.clsoe if the 129cdf0e10cSrcweir // interface is supported, otherwise use XComponent.dispose 130cdf0e10cSrcweir com.sun.star.util.XCloseable xCloseable = 131cdf0e10cSrcweir (com.sun.star.util.XCloseable)UnoRuntime.queryInterface( 132cdf0e10cSrcweir com.sun.star.util.XCloseable.class, xStorable); 133cdf0e10cSrcweir 134cdf0e10cSrcweir if ( xCloseable != null ) { 135cdf0e10cSrcweir xCloseable.close(false); 136cdf0e10cSrcweir } else { 137cdf0e10cSrcweir com.sun.star.lang.XComponent xComp = 138cdf0e10cSrcweir (com.sun.star.lang.XComponent)UnoRuntime.queryInterface( 139cdf0e10cSrcweir com.sun.star.lang.XComponent.class, xStorable); 140cdf0e10cSrcweir 141cdf0e10cSrcweir xComp.dispose(); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir } 144cdf0e10cSrcweir catch( Exception e ) { 145cdf0e10cSrcweir e.printStackTrace(System.err); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir System.out.println(sIndent + entries[ i ].getName()); 149cdf0e10cSrcweir } 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir sIndent = sIndent.substring(2); 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir /** Bootstrap UNO, getting the remote component context, getting a new instance 156cdf0e10cSrcweir * of the desktop (used interface XComponentLoader) and calling the 157cdf0e10cSrcweir * static method traverse 158cdf0e10cSrcweir * @param args The array of the type String contains the directory, in which 159cdf0e10cSrcweir * all files should be converted, the favoured converting type 160cdf0e10cSrcweir * and the wanted extension 161cdf0e10cSrcweir */ main( String args[] )162cdf0e10cSrcweir public static void main( String args[] ) { 163cdf0e10cSrcweir if ( args.length < 3 ) { 164cdf0e10cSrcweir System.out.println("usage: java -jar DocumentConverter.jar " + 165cdf0e10cSrcweir "\"<directory to convert>\" \"<type to convert to>\" " + 166cdf0e10cSrcweir "\"<extension>\" \"<output_directory>\""); 167cdf0e10cSrcweir System.out.println("\ne.g.:"); 168cdf0e10cSrcweir System.out.println("usage: java -jar DocumentConverter.jar " + 169cdf0e10cSrcweir "\"c:/myoffice\" \"swriter: MS Word 97\" \"doc\""); 170cdf0e10cSrcweir System.exit(1); 171cdf0e10cSrcweir } 172cdf0e10cSrcweir 173cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext = null; 174cdf0e10cSrcweir 175cdf0e10cSrcweir try { 176cdf0e10cSrcweir // get the remote office component context 177cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 178cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 179cdf0e10cSrcweir 180cdf0e10cSrcweir // get the remote office service manager 181cdf0e10cSrcweir com.sun.star.lang.XMultiComponentFactory xMCF = 182cdf0e10cSrcweir xContext.getServiceManager(); 183cdf0e10cSrcweir 184cdf0e10cSrcweir Object oDesktop = xMCF.createInstanceWithContext( 185cdf0e10cSrcweir "com.sun.star.frame.Desktop", xContext); 186cdf0e10cSrcweir 187cdf0e10cSrcweir xCompLoader = (com.sun.star.frame.XComponentLoader) 188cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, 189cdf0e10cSrcweir oDesktop); 190cdf0e10cSrcweir 191cdf0e10cSrcweir // Getting the given starting directory 192cdf0e10cSrcweir File file = new File(args[0]); 193cdf0e10cSrcweir 194cdf0e10cSrcweir // Getting the given type to convert to 195cdf0e10cSrcweir sConvertType = args[1]; 196cdf0e10cSrcweir 197cdf0e10cSrcweir // Getting the given extension that should be appended to the 198cdf0e10cSrcweir // origin document 199cdf0e10cSrcweir sExtension = args[2]; 200cdf0e10cSrcweir 201cdf0e10cSrcweir // Getting the given type to convert to 202cdf0e10cSrcweir sOutputDir = args[3]; 203cdf0e10cSrcweir 204cdf0e10cSrcweir // Starting the conversion of documents in the given directory 205cdf0e10cSrcweir // and subdirectories 206cdf0e10cSrcweir traverse(file); 207cdf0e10cSrcweir 208cdf0e10cSrcweir System.exit(0); 209cdf0e10cSrcweir } catch( Exception e ) { 210cdf0e10cSrcweir e.printStackTrace(System.err); 211cdf0e10cSrcweir System.exit(1); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir } 214cdf0e10cSrcweir } 215