1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.examples.embedding;
23 
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.lib.uno.helper.Factory;
27 import com.sun.star.lang.XSingleComponentFactory;
28 import com.sun.star.registry.XRegistryKey;
29 import com.sun.star.lib.uno.helper.WeakBase;
30 import com.sun.star.uno.Exception;
31 import com.sun.star.lang.IllegalArgumentException;
32 
33 import com.sun.star.embed.*;
34 
35 import org.openoffice.examples.embedding.OwnEmbeddedObject;
36 
37 public final class OwnEmbeddedObjectFactory extends WeakBase
38    implements com.sun.star.lang.XServiceInfo,
39               com.sun.star.embed.XEmbedObjectFactory
40 {
41     private final XComponentContext m_xContext;
42     private static final String m_implementationName = OwnEmbeddedObjectFactory.class.getName();
43     private static final String[] m_serviceNames = {
44         "org.openoffice.examples.embedding.Factory69474366FD6F480683748EDD1B6E771D" };
45     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 };
46 
47 
OwnEmbeddedObjectFactory( XComponentContext context )48     public OwnEmbeddedObjectFactory( XComponentContext context )
49     {
50         m_xContext = context;
51     };
52 
__getComponentFactory( String sImplementationName )53     public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
54         XSingleComponentFactory xFactory = null;
55 
56         if ( sImplementationName.equals( m_implementationName ) )
57             xFactory = Factory.createComponentFactory(OwnEmbeddedObjectFactory.class, m_serviceNames);
58         return xFactory;
59     }
60 
61     // This method not longer necessary since OOo 3.4 where the component registration
62     // was changed to passive component registration. For more details see
63     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
64 
65 //     public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
66 //         return Factory.writeRegistryServiceInfo(m_implementationName,
67 //                                                 m_serviceNames,
68 //                                                 xRegistryKey);
69 //     }
70 
71     // com.sun.star.lang.XServiceInfo:
getImplementationName()72     public String getImplementationName() {
73          return m_implementationName;
74     }
75 
supportsService( String sService )76     public boolean supportsService( String sService ) {
77         int len = m_serviceNames.length;
78 
79         for( int i=0; i < len; i++) {
80             if (sService.equals(m_serviceNames[i]))
81                 return true;
82         }
83         return false;
84     }
85 
getSupportedServiceNames()86     public String[] getSupportedServiceNames() {
87         return m_serviceNames;
88     }
89 
90     // com.sun.star.embed.XEmbedObjectFactory:
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)91     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
92     {
93         if ( xStorage == null || sEntName == null || sEntName.length() == 0 )
94             throw new com.sun.star.lang.IllegalArgumentException();
95 
96         if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT )
97         {
98             if ( aClassID != null && aClassID.length != 0 )
99             {
100                 if ( aClassID.length != m_classID.length )
101                     throw new com.sun.star.lang.IllegalArgumentException();
102 
103                 for ( int i = 0; i < aClassID.length; i++ )
104                     if ( aClassID[i] != m_classID[i] )
105                         throw new com.sun.star.lang.IllegalArgumentException();
106             }
107             else if ( !xStorage.hasByName( sEntName ) )
108                 throw new com.sun.star.lang.IllegalArgumentException();
109         }
110         else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT )
111         {
112             if ( aClassID.length != m_classID.length )
113                 throw new com.sun.star.lang.IllegalArgumentException();
114 
115             for ( int i = 0; i < m_classID.length; i++ )
116                 if ( aClassID[i] != m_classID[i] )
117                     throw new com.sun.star.lang.IllegalArgumentException();
118         }
119 
120         OwnEmbeddedObject aObject = new OwnEmbeddedObject( m_xContext, m_classID );
121         aObject.setPersistentEntry( xStorage, sEntName, nEntryConnectionMode, aArgs, aObjectArgs );
122 
123         return aObject;
124     }
125 
126 }
127