diff options
Diffstat (limited to 'src/org/openoffice/tools/types/TypeManager.java')
-rw-r--r-- | src/org/openoffice/tools/types/TypeManager.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/org/openoffice/tools/types/TypeManager.java b/src/org/openoffice/tools/types/TypeManager.java new file mode 100644 index 0000000..e0a8b34 --- /dev/null +++ b/src/org/openoffice/tools/types/TypeManager.java @@ -0,0 +1,115 @@ +package org.openoffice.tools.types; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Vector; + +import com.sun.star.container.NoSuchElementException; +import com.sun.star.container.XHierarchicalNameAccess; +import com.sun.star.container.XSet; +import com.sun.star.frame.XDesktop; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.reflection.XTypeDescription; +import com.sun.star.registry.XSimpleRegistry; +import com.sun.star.uno.RuntimeException; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XComponentContext; + +/* + * TODO: + * + Load the registries + * + use the introspection API to extract the useful infos from the type + */ +public class TypeManager { + + private String mInstallDir; + private XComponentContext mCtxt; + private XHierarchicalNameAccess mTypeDescMngr; + + public TypeManager( String pInstallDir ) { + mInstallDir = pInstallDir; + connect( ); + } + + public void dispose( ) { + disconnect(); + } + + public void initRegistries( String[] pUrls ) throws Exception { + // Get the type description manager + Object o = mCtxt.getValueByName( "/singletons/com.sun.star.reflection.theTypeDescriptionManager" ); + mTypeDescMngr = (XHierarchicalNameAccess)UnoRuntime.queryInterface( XHierarchicalNameAccess.class, o ); + + if ( mTypeDescMngr == null ) { + throw new RuntimeException( "Couldn't get the type description manager from OOo" ); + } + + // Get the service manager + XMultiComponentFactory xServiceMngr = mCtxt.getServiceManager(); + + // Open the registries + XSimpleRegistry[] regs = new XSimpleRegistry[ pUrls.length ]; + for ( int i = 0; i < pUrls.length; i++ ) { + String regUrl = pUrls[i]; + o = xServiceMngr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", mCtxt ); + XSimpleRegistry reg = (XSimpleRegistry)UnoRuntime.queryInterface( XSimpleRegistry.class, o ); + reg.open( regUrl, true, false); + regs[i] = reg; + } + + // Fill the type description manager + o = xServiceMngr.createInstanceWithArgumentsAndContext( + "com.sun.star.reflection.TypeDescriptionProvider", regs, mCtxt ); + XHierarchicalNameAccess tdProvider = (XHierarchicalNameAccess)UnoRuntime.queryInterface( + XHierarchicalNameAccess.class, o ); + if ( tdProvider == null ) { + throw new RuntimeException( "Can't initialize the TypeDescriptionProvider" ); + } + + XSet mngrSet = (XSet)UnoRuntime.queryInterface( XSet.class, mTypeDescMngr ); + mngrSet.insert( tdProvider ); + } + + /** + * Search the description of a given UNO type in the registries. + * + * @param pTypeName the name of the type to look for, separated by '.' + * + * @return the type description + * + * @throws NoSuchElementException if no element is corresponding to the asked type. + */ + public XTypeDescription searchType( String pTypeName ) throws NoSuchElementException { + Object o = mTypeDescMngr.getByHierarchicalName( pTypeName ); + return (XTypeDescription)UnoRuntime.queryInterface( XTypeDescription.class, o ); + } + + private void connect( ) { + try { + File installDir = new File( mInstallDir ); + File programDir = new File( installDir, "program" ); + URLClassLoader loader = new URLClassLoader( new URL[] { programDir.toURI().toURL() }); + mCtxt = Bootstrap.bootstrap( loader ); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void disconnect( ) { + try { + if ( mCtxt != null ) { + // Only the uno test suite which started the office can stop it + XMultiComponentFactory xMngr = mCtxt.getServiceManager(); + Object oDesktop = xMngr.createInstanceWithContext("com.sun.star.frame.Desktop", mCtxt); + XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, oDesktop); + + xDesktop.terminate(); + mCtxt = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} |