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 
23 
24 package org.openoffice.examples.embedding;
25 
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XComponentContext;
28 import com.sun.star.lib.uno.helper.Factory;
29 import com.sun.star.lang.XSingleComponentFactory;
30 import com.sun.star.registry.XRegistryKey;
31 import com.sun.star.lib.uno.helper.WeakBase;
32 import com.sun.star.uno.Exception;
33 import com.sun.star.lang.IllegalArgumentException;
34 
35 import com.sun.star.embed.*;
36 
37 import org.openoffice.examples.embedding.OwnEmbeddedObject;
38 
39 public final class OwnEmbeddedObjectFactory extends WeakBase
40    implements com.sun.star.lang.XServiceInfo,
41               com.sun.star.embed.XEmbedObjectFactory
42 {
43     private final XComponentContext m_xContext;
44     private static final String m_implementationName = OwnEmbeddedObjectFactory.class.getName();
45     private static final String[] m_serviceNames = {
46         "org.openoffice.examples.embedding.Factory69474366FD6F480683748EDD1B6E771D" };
47     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 };
48 
49 
OwnEmbeddedObjectFactory( XComponentContext context )50     public OwnEmbeddedObjectFactory( XComponentContext context )
51     {
52         m_xContext = context;
53     };
54 
__getComponentFactory( String sImplementationName )55     public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
56         XSingleComponentFactory xFactory = null;
57 
58         if ( sImplementationName.equals( m_implementationName ) )
59             xFactory = Factory.createComponentFactory(OwnEmbeddedObjectFactory.class, m_serviceNames);
60         return xFactory;
61     }
62 
63     // This method not longer necessary since OOo 3.4 where the component registration
64     // was changed to passive component registration. For more details see
65     // https://wiki.openoffice.org/wiki/Passive_Component_Registration
66 
67 //     public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
68 //         return Factory.writeRegistryServiceInfo(m_implementationName,
69 //                                                 m_serviceNames,
70 //                                                 xRegistryKey);
71 //     }
72 
73     // com.sun.star.lang.XServiceInfo:
getImplementationName()74     public String getImplementationName() {
75          return m_implementationName;
76     }
77 
supportsService( String sService )78     public boolean supportsService( String sService ) {
79         int len = m_serviceNames.length;
80 
81         for( int i=0; i < len; i++) {
82             if (sService.equals(m_serviceNames[i]))
83                 return true;
84         }
85         return false;
86     }
87 
getSupportedServiceNames()88     public String[] getSupportedServiceNames() {
89         return m_serviceNames;
90     }
91 
92     // 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)93     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
94     {
95         if ( xStorage == null || sEntName == null || sEntName.length() == 0 )
96             throw new com.sun.star.lang.IllegalArgumentException();
97 
98         if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT )
99         {
100             if ( aClassID != null && aClassID.length != 0 )
101             {
102                 if ( aClassID.length != m_classID.length )
103                     throw new com.sun.star.lang.IllegalArgumentException();
104 
105                 for ( int i = 0; i < aClassID.length; i++ )
106                     if ( aClassID[i] != m_classID[i] )
107                         throw new com.sun.star.lang.IllegalArgumentException();
108             }
109             else if ( !xStorage.hasByName( sEntName ) )
110                 throw new com.sun.star.lang.IllegalArgumentException();
111         }
112         else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT )
113         {
114             if ( aClassID.length != m_classID.length )
115                 throw new com.sun.star.lang.IllegalArgumentException();
116 
117             for ( int i = 0; i < m_classID.length; i++ )
118                 if ( aClassID[i] != m_classID[i] )
119                     throw new com.sun.star.lang.IllegalArgumentException();
120         }
121 
122         OwnEmbeddedObject aObject = new OwnEmbeddedObject( m_xContext, m_classID );
123         aObject.setPersistentEntry( xStorage, sEntName, nEntryConnectionMode, aArgs, aObjectArgs );
124 
125         return aObject;
126     }
127 
128 }
129 
130