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