1 package org.openoffice.examples.embedding;
2 
3 import com.sun.star.uno.UnoRuntime;
4 import com.sun.star.uno.XComponentContext;
5 import com.sun.star.lib.uno.helper.Factory;
6 import com.sun.star.lang.XSingleComponentFactory;
7 import com.sun.star.registry.XRegistryKey;
8 import com.sun.star.lib.uno.helper.WeakBase;
9 import com.sun.star.uno.Exception;
10 import com.sun.star.lang.IllegalArgumentException;
11 
12 import com.sun.star.embed.*;
13 
14 import org.openoffice.examples.embedding.OwnEmbeddedObject;
15 
16 public final class OwnEmbeddedObjectFactory extends WeakBase
17    implements com.sun.star.lang.XServiceInfo,
18               com.sun.star.embed.XEmbedObjectFactory
19 {
20     private final XComponentContext m_xContext;
21     private static final String m_implementationName = OwnEmbeddedObjectFactory.class.getName();
22     private static final String[] m_serviceNames = {
23         "org.openoffice.examples.embedding.Factory69474366FD6F480683748EDD1B6E771D" };
24     private static final byte[] m_classID = { 0x69, 0x47, 0x43, 0x66, (byte)0xFD, 0x6F, 0x48, 0x06, (byte)0x83, 0x74, (byte)0x8E, (byte)0xDD, 0x1B, 0x6E, 0x77, 0x1D };
25 
26 
27     public OwnEmbeddedObjectFactory( XComponentContext context )
28     {
29         m_xContext = context;
30     };
31 
32     public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
33         XSingleComponentFactory xFactory = null;
34 
35         if ( sImplementationName.equals( m_implementationName ) )
36             xFactory = Factory.createComponentFactory(OwnEmbeddedObjectFactory.class, m_serviceNames);
37         return xFactory;
38     }
39 
40     // This method not longer necessary since OOo 3.4 where the component registration
41     // was changed to passive component registration. For more details see
42     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
43 
44 //     public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
45 //         return Factory.writeRegistryServiceInfo(m_implementationName,
46 //                                                 m_serviceNames,
47 //                                                 xRegistryKey);
48 //     }
49 
50     // com.sun.star.lang.XServiceInfo:
51     public String getImplementationName() {
52          return m_implementationName;
53     }
54 
55     public boolean supportsService( String sService ) {
56         int len = m_serviceNames.length;
57 
58         for( int i=0; i < len; i++) {
59             if (sService.equals(m_serviceNames[i]))
60                 return true;
61         }
62         return false;
63     }
64 
65     public String[] getSupportedServiceNames() {
66         return m_serviceNames;
67     }
68 
69     // com.sun.star.embed.XEmbedObjectFactory:
70     public Object createInstanceUserInit(byte[] aClassID, String sClassName, com.sun.star.embed.XStorage xStorage, String sEntName, int nEntryConnectionMode, com.sun.star.beans.PropertyValue[] aArgs, com.sun.star.beans.PropertyValue[] aObjectArgs) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.io.IOException, com.sun.star.uno.Exception
71     {
72         if ( xStorage == null || sEntName == null || sEntName.length() == 0 )
73             throw new com.sun.star.lang.IllegalArgumentException();
74 
75         if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT )
76         {
77             if ( aClassID != null && aClassID.length != 0 )
78             {
79                 if ( aClassID.length != m_classID.length )
80                     throw new com.sun.star.lang.IllegalArgumentException();
81 
82                 for ( int i = 0; i < aClassID.length; i++ )
83                     if ( aClassID[i] != m_classID[i] )
84                         throw new com.sun.star.lang.IllegalArgumentException();
85             }
86             else if ( !xStorage.hasByName( sEntName ) )
87                 throw new com.sun.star.lang.IllegalArgumentException();
88         }
89         else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT )
90         {
91             if ( aClassID.length != m_classID.length )
92                 throw new com.sun.star.lang.IllegalArgumentException();
93 
94             for ( int i = 0; i < m_classID.length; i++ )
95                 if ( aClassID[i] != m_classID[i] )
96                     throw new com.sun.star.lang.IllegalArgumentException();
97         }
98 
99         OwnEmbeddedObject aObject = new OwnEmbeddedObject( m_xContext, m_classID );
100         aObject.setPersistentEntry( xStorage, sEntName, nEntryConnectionMode, aArgs, aObjectArgs );
101 
102         return aObject;
103     }
104 
105 }
106