import java.util.*; import com.sun.star.beans.*; import com.sun.star.bridge.*; import com.sun.star.comp.helper.*; import com.sun.star.container.*; import com.sun.star.frame.*; import com.sun.star.lang.*; import com.sun.star.sdbc.*; import com.sun.star.sdbcx.*; import com.sun.star.task.*; import com.sun.star.text.*; import com.sun.star.uno.*; import com.sun.star.util.XCloseable; import com.sun.star.sdb.XDocumentDataSource; 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.XPropertySet; import com.sun.star.beans.PropertyValue; import com.sun.star.bridge.XBridgeFactory; import com.sun.star.bridge.XBridge; import com.sun.star.connection.XConnector; import com.sun.star.connection.XConnection; public class OOoIssue65742 { public XComponentContext mxComponentContext = null ; public XComponentLoader mxComponentLoader = null ; public XMultiComponentFactory mxMCF = null ; public XMultiServiceFactory mxMSF = null ; private XBridge bridge = null ; public static String cleanFilename( String s ) { return s.replaceAll( "\\\\", "/" ); } public OOoIssue65742() { } static public void main(String[] args) throws java.lang.Exception { OOoIssue65742 o = new OOoIssue65742() ; o.setupConnection("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager") ; String dataSourceName = "" ; String dataSourceTable = "" ; String baseDir = "" ; String modeleFileNameSXWRealDataSource = "" ; o.merge( dataSourceName, dataSourceTable, baseDir + modeleFileNameSXWRealDataSource, baseDir ); } // Refer to DevelopersGuide.pdf, p30-33, "2.3.4 First Connection". public void setupConnection(String sConnection) throws java.lang.Exception { // 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. mxComponentContext = 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. mxMCF = mxComponentContext.getServiceManager(); // Creates an instance of the component UnoUrlResolver which // supports the services specified by the factory. Object objectUrlResolver = mxMCF.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver",mxComponentContext ); // Create a new url resolver XUnoUrlResolver xurlresolver = ( XUnoUrlResolver ) UnoRuntime.queryInterface( XUnoUrlResolver.class,objectUrlResolver ); // Resolves an object that is specified as follow: // uno:;; XInterface objectInitial = ( XInterface ) xurlresolver.resolve( sConnection ); // Create a service manager from the initial object mxMCF = ( XMultiComponentFactory ) UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial ); // Query for the XPropertySet interface. XPropertySet xProperySet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class,mxMCF ); // Get the default context from the office server. Object oDefaultContext = xProperySet.getPropertyValue( "DefaultContext" ); // Query for the interface XComponentContext. mxComponentContext = ( XComponentContext ) UnoRuntime.queryInterface( XComponentContext.class, oDefaultContext ); // 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. Object oDesktop = mxMCF.createInstanceWithContext( "com.sun.star.frame.Desktop", mxComponentContext ); mxComponentLoader = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); // Query for an XMultiServiceFactory instance from the global // service manager mxMSF = ( XMultiServiceFactory ) UnoRuntime.queryInterface(XMultiServiceFactory.class,mxComponentContext.getServiceManager() ); } // Ouverture d'un fichier. // Si l'on veut debugguer, on peut virer le parametre "Hidden", ce qui aurait pour effet // de ne plus cacher la fenetre du document sur lequel Openoffice travaille. public XComponent openFile( String file ) throws java.lang.Exception { XTextDocument oDoc = null; XComponent aDoc = null; String str = "file:///" + cleanFilename( file ); try { PropertyValue[] loadProps = new PropertyValue[1 ]; loadProps[ 0 ] = new PropertyValue(); loadProps[ 0 ].Name = "Hidden"; loadProps[ 0 ].Value = Boolean.TRUE; aDoc = mxComponentLoader.loadComponentFromURL( str, "_blank", 0, loadProps ); if( aDoc == null ) { throw new java.lang.Exception( "mmm.openFile : impossible d'ouvrir le fichier ; " + str ); } oDoc = ( XTextDocument ) UnoRuntime.queryInterface( XTextDocument.class, aDoc ); } catch( java.lang.Exception e ) { System.err.println( "Error openFile" + e + " - " + str); throw new java.lang.Exception( "mmm.openFile : " + e.getMessage() + " ; " + e.getClass().getName() + " - " + str ); } //return oDoc; return aDoc; } // Fusion soit : // - depuis un fichier sxw sur le disque // - depuis le fichier courant public void merge( String dataSourceName, String tableName, String fileIn, String prefix, String dirOut ) throws java.lang.Exception { try { // MERGE Object mmservice = mxMCF.createInstanceWithContext( "com.sun.star.text.MailMerge", mxComponentContext ); XPropertySet oObjProps = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, mmservice ); oObjProps.setPropertyValue( "DataSourceName", dataSourceName ); oObjProps.setPropertyValue( "Command", tableName ); oObjProps.setPropertyValue( "CommandType", new Integer( com.sun.star.sdb.CommandType.TABLE ) ); oObjProps.setPropertyValue( "OutputType", new Short( com.sun.star.text.MailMergeType.FILE ) ); // Le fichier de sortie s'appelle "fichierDentreeN.sxw" avec N=0 pour le 1er enregistrement. //oObjProps.setPropertyValue( "OutputURL", "file:///C:/OOoMM/" ); String outputURL = "file:///" + cleanFilename( dirOut ); oObjProps.setPropertyValue( "OutputURL", outputURL ); oObjProps.setPropertyValue( "FileNameFromColumn", new Boolean( false ) ); //oObjProps.setPropertyValue( "DocumentURL", "file:///C:/OOoMM/CopieMailingFromWord2000ApresMAJFields.sxw" ); //MERGE DEPUIS UN FICHIER SOURCE oObjProps.setPropertyValue( "DocumentURL", "file:///" + cleanFilename( fileIn ) ); XJob job = ( XJob ) UnoRuntime.queryInterface( XJob.class, mmservice ); job.execute( new NamedValue[0 ] ); } catch( java.lang.Exception e ) { System.err.println( "merge " + e.getMessage() + e.getClass().getName() ); throw new java.lang.Exception( "mmm.merge : " + e.getMessage() + " ; " + e.getClass().getName() ); } } public void merge( String dataSourceName, String tableName, String fileIn, String dirOut ) throws java.lang.Exception { merge( dataSourceName, tableName, fileIn, "", dirOut ); } // Ferme le document public void close( XComponent theDoc ) { try { XCloseable xcloseable = ( XCloseable ) UnoRuntime.queryInterface( XCloseable.class, theDoc ); xcloseable.close( true ); //theDoc.dispose(); } catch( java.lang.Exception ex ) { System.err.println( "close " + ex.getMessage() + ";" + ex.getClass().getName() ); } } }