INTEGRATION: CWS sdksample (1.4.40); FILE MERGED

2004/11/11 10:33:55 jsc 1.4.40.4: #i29308# adapted
2004/08/06 09:28:03 jsc 1.4.40.3: #i29308# use System.err for error output
2004/08/05 15:28:41 jsc 1.4.40.2: #i29308# use System.err for error output
2004/06/04 14:51:34 jsc 1.4.40.1: #i29308# use of new bootstrap feature
This commit is contained in:
Rüdiger Timm 2005-01-31 16:08:23 +00:00
parent 89559e1726
commit 31c3a803fa

View file

@ -2,9 +2,9 @@
*
* $RCSfile: DocumentConverter.java,v $
*
* $Revision: 1.4 $
* $Revision: 1.5 $
*
* last change: $Author: hr $ $Date: 2004-02-02 20:09:48 $
* last change: $Author: rt $ $Date: 2005-01-31 17:08:23 $
*
* The Contents of this file are made available subject to the terms of
* the BSD license.
@ -38,219 +38,194 @@
*
*************************************************************************/
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import java.io.File;
import java.io.FileFilter;
/** The class <CODE>DocumentConverter</CODE> allows you to convert all documents in
* a given directory and in its subdirectories to a given type. A converted
/** The class <CODE>DocumentConverter</CODE> allows you to convert all documents
* in a given directory and in its subdirectories to a given type. A converted
* document will be created in the same directory as the origin document.
*
*/
public class DocumentConverter {
/** Containing the loaded documents
*/
static XComponentLoader xcomponentloader = null;
/** Containing the given type to convert to
*/
static String stringConvertType = "";
/** Containing the given extension
*/
static String stringExtension = "";
/** Containing the current file or directory
*/
static String indent = "";
/** Containing the loaded documents
*/
static com.sun.star.frame.XComponentLoader xCompLoader = null;
/** Containing the given type to convert to
*/
static String sConvertType = "";
/** Containing the given extension
*/
static String sExtension = "";
/** Containing the current file or directory
*/
static String sIndent = "";
/** Containing the directory where the converted files are saved
*/
static String sOutputDir = "";
/** Traversing the given directory recursively and converting their files to the
* favoured type if possible
* @param fileDirectory Containing the directory
*/
static void traverse( File fileDirectory ) {
// Testing, if the file is a directory, and if so, it throws an exception
if ( !fileDirectory.isDirectory() ) {
throw new IllegalArgumentException(
"not a directory: " + fileDirectory.getName()
);
/** Traversing the given directory recursively and converting their files to
* the favoured type if possible
* @param fileDirectory Containing the directory
*/
static void traverse( File fileDirectory ) {
// Testing, if the file is a directory, and if so, it throws an exception
if ( !fileDirectory.isDirectory() ) {
throw new IllegalArgumentException(
"not a directory: " + fileDirectory.getName()
);
}
// Prepare Url for the output directory
File outdir = new File(DocumentConverter.sOutputDir);
String sOutUrl = "file:///" + outdir.getAbsolutePath().replace( '\\', '/' );
System.out.println("\nThe converted documents will stored in \""
+ outdir.getPath() + "!");
System.out.println(sIndent + "[" + fileDirectory.getName() + "]");
sIndent += " ";
// Getting all files and directories in the current directory
File[] entries = fileDirectory.listFiles();
// Iterating for each file and directory
for ( int i = 0; i < entries.length; ++i ) {
// Testing, if the entry in the list is a directory
if ( entries[ i ].isDirectory() ) {
// Recursive call for the new directory
traverse( entries[ i ] );
} else {
// Converting the document to the favoured type
try {
// Composing the URL by replacing all backslashs
String sUrl = "file:///"
+ entries[ i ].getAbsolutePath().replace( '\\', '/' );
// Loading the wanted document
com.sun.star.beans.PropertyValue propertyValues[] =
new com.sun.star.beans.PropertyValue[1];
propertyValues[0] = new com.sun.star.beans.PropertyValue();
propertyValues[0].Name = "Hidden";
propertyValues[0].Value = new Boolean(true);
Object oDocToStore =
DocumentConverter.xCompLoader.loadComponentFromURL(
sUrl, "_blank", 0, propertyValues);
// Getting an object that will offer a simple way to store
// a document to a URL.
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );
// Preparing properties for converting the document
propertyValues = new com.sun.star.beans.PropertyValue[2];
// Setting the flag for overwriting
propertyValues[0] = new com.sun.star.beans.PropertyValue();
propertyValues[0].Name = "Overwrite";
propertyValues[0].Value = new Boolean(true);
// Setting the filter name
propertyValues[1] = new com.sun.star.beans.PropertyValue();
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = DocumentConverter.sConvertType;
// Appending the favoured extension to the origin document name
int index1 = sUrl.lastIndexOf('/');
int index2 = sUrl.lastIndexOf('.');
String sStoreUrl = sOutUrl + sUrl.substring(index1, index2 + 1)
+ DocumentConverter.sExtension;
// Storing and converting the document
xStorable.storeAsURL(sStoreUrl, propertyValues);
// Closing the converted document. Use XCloseable.clsoe if the
// interface is supported, otherwise use XComponent.dispose
com.sun.star.util.XCloseable xCloseable =
(com.sun.star.util.XCloseable)UnoRuntime.queryInterface(
com.sun.star.util.XCloseable.class, xStorable);
if ( xCloseable != null ) {
xCloseable.close(false);
} else {
com.sun.star.lang.XComponent xComp =
(com.sun.star.lang.XComponent)UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, xStorable);
xComp.dispose();
}
}
catch( Exception e ) {
e.printStackTrace(System.err);
}
System.out.println(sIndent + entries[ i ].getName());
}
}
sIndent = sIndent.substring(2);
}
System.out.println(indent + "[" + fileDirectory.getName() + "]");
indent += " ";
/** Bootstrap UNO, getting the remote component context, getting a new instance
* of the desktop (used interface XComponentLoader) and calling the
* static method traverse
* @param args The array of the type String contains the directory, in which
* all files should be converted, the favoured converting type
* and the wanted extension
*/
public static void main( String args[] ) {
if ( args.length < 3 ) {
System.out.println("usage: java -jar DocumentConverter.jar " +
"\"<directory to convert>\" \"<type to convert to>\" " +
"\"<extension>\" \"<output_directory>\"");
System.out.println("\ne.g.:");
System.out.println("usage: java -jar DocumentConverter.jar " +
"\"c:/myoffice\" \"swriter: MS Word 97\" \"doc\"");
System.exit(1);
}
// Getting all files and directories in the current directory
File[] entries = fileDirectory.listFiles(
new FileFilter() {
public boolean accept( File pathname ) {
return true;
}
}
);
com.sun.star.uno.XComponentContext xContext = null;
// Iterating for each file and directory
for ( int i = 0; i < entries.length; ++i ) {
// Testing, if the entry in the list is a directory
if ( entries[ i ].isDirectory() ) {
// Recursive call for the new directory
traverse( entries[ i ] );
} else {
// Converting the document to the favoured type
try {
// Composing the URL by replacing all backslashs
String stringUrl = "file:///"
+ entries[ i ].getAbsolutePath().replace( '\\', '/' );
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");
// Loading the wanted document
Object objectDocumentToStore =
DocumentConverter.xcomponentloader.loadComponentFromURL(
stringUrl, "_blank", 0, new PropertyValue[0] );
// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();
// Getting an object that will offer a simple way to store a document to a URL.
XStorable xstorable =
( XStorable ) UnoRuntime.queryInterface( XStorable.class,
objectDocumentToStore );
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
// Preparing properties for converting the document
PropertyValue propertyvalue[] = new PropertyValue[ 2 ];
// Setting the flag for overwriting
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "Overwrite";
propertyvalue[ 0 ].Value = new Boolean(true);
// Setting the filter name
propertyvalue[ 1 ] = new PropertyValue();
propertyvalue[ 1 ].Name = "FilterName";
propertyvalue[ 1 ].Value = DocumentConverter.stringConvertType;
xCompLoader = (com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
oDesktop);
// Appending the favoured extension to the origin document name
int index = stringUrl.lastIndexOf('.');
if (index >= 0)
stringUrl = stringUrl.substring(0, index + 1);
stringUrl = stringUrl + DocumentConverter.stringExtension;
// Getting the given starting directory
File file = new File(args[0]);
// Storing and converting the document
xstorable.storeAsURL( stringUrl, propertyvalue );
// Getting the given type to convert to
sConvertType = args[1];
// Getting the method dispose() for closing the document
XComponent xcomponent =
( XComponent ) UnoRuntime.queryInterface( XComponent.class,
xstorable );
// Getting the given extension that should be appended to the
// origin document
sExtension = args[2];
// Closing the converted document
xcomponent.dispose();
// Getting the given type to convert to
sOutputDir = args[3];
// Starting the conversion of documents in the given directory
// and subdirectories
traverse(file);
System.exit(0);
} catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
catch( Exception exception ) {
exception.printStackTrace();
}
System.out.println(indent + entries[ i ].getName());
}
}
indent = indent.substring(2);
}
/** Connecting to the office with the component UnoUrlResolver and calling the
* static method traverse
* @param args The array of the type String contains the directory, in which all files should be
* converted, the favoured converting type and the wanted extension
*/
public static void main( String args[] ) {
try {
String sConnectionString = "uno:socket,host=localhost,port=2083;urp;StarOffice.ServiceManager";
if ( args.length < 3 ) {
System.out.println(
"usage: java -classpath <see make> " +
"DocumentConverter \"<directory to convert>\"" +
" \"<type to convert to>\" \"<extension>\" [\"<connection>\"]" );
System.out.println( "\ne.g.:" );
System.out.println(
"java -classpath <see make> " +
"DocumentConverter" + " \"c:/myoffice\" \"swriter: MS Word 97\" \"doc\"" );
System.exit(1);
}
// It is possible to use a different connection string, passed as argument
if ( args.length == 4 ) {
sConnectionString = args[3];
}
/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xComponentContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xMultiComponentFactory =
xComponentContext.getServiceManager();
/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver = xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xComponentContext );
// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );
// Resolves an object that is specified as follow:
// uno:<connection description>;<protocol description>;<initial object name>
Object objectInitial = xurlresolver.resolve( sConnectionString );
// Create a service manager from the initial object
xMultiComponentFactory = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial );
// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xMultiComponentFactory );
// Get the default context from the office server.
Object objectDefaultContext =
xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext" );
// Query for the interface XComponentContext.
xComponentContext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, objectDefaultContext );
/* A desktop environment contains tasks with one or more
frames in which components can be loaded. Desktop is the
environment for components which can instanciate within
frames. */
xcomponentloader = ( XComponentLoader )
UnoRuntime.queryInterface( XComponentLoader.class,
xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xComponentContext ) );
// Getting the given starting directory
File file = new File(args[ 0 ]);
// Getting the given type to convert to
stringConvertType = args[ 1 ];
// Getting the given extension that should be appended to the origin document
stringExtension = args[ 2 ];
// Starting the conversion of documents in the given directory and subdirectories
traverse( file );
System.exit(0);
}
catch( Exception exception ) {
System.err.println( exception );
}
}
}