summaryrefslogtreecommitdiff
path: root/odk/examples/java/EmbedDocument/EmbeddedObject/OwnEmbeddedObjectFactory.java
blob: a146e04d3ca17c0df7cf36eb4362e5e14bbf74ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.openoffice.examples.embedding;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.uno.Exception;
import com.sun.star.lang.IllegalArgumentException;

import com.sun.star.embed.*;

import org.openoffice.examples.embedding.OwnEmbeddedObject;

public final class OwnEmbeddedObjectFactory extends WeakBase
   implements com.sun.star.lang.XServiceInfo,
              com.sun.star.embed.XEmbedObjectFactory
{
    private final XComponentContext m_xContext;
    private static final String m_implementationName = OwnEmbeddedObjectFactory.class.getName();
    private static final String[] m_serviceNames = {
        "org.openoffice.examples.embedding.Factory69474366FD6F480683748EDD1B6E771D" };
    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 };


    public OwnEmbeddedObjectFactory( XComponentContext context )
    {
        m_xContext = context;
    };

    public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
        XSingleComponentFactory xFactory = null;

        if ( sImplementationName.equals( m_implementationName ) )
            xFactory = Factory.createComponentFactory(OwnEmbeddedObjectFactory.class, m_serviceNames);
        return xFactory;
    }

    // This method not longer necessary since OOo 3.4 where the component registration
    // was changed to passive component registration. For more details see
    // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration

//     public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
//         return Factory.writeRegistryServiceInfo(m_implementationName,
//                                                 m_serviceNames,
//                                                 xRegistryKey);
//     }

    // com.sun.star.lang.XServiceInfo:
    public String getImplementationName() {
         return m_implementationName;
    }

    public boolean supportsService( String sService ) {
        int len = m_serviceNames.length;

        for( int i=0; i < len; i++) {
            if (sService.equals(m_serviceNames[i]))
                return true;
        }
        return false;
    }

    public String[] getSupportedServiceNames() {
        return m_serviceNames;
    }

    // com.sun.star.embed.XEmbedObjectFactory:
    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
    {
        if ( xStorage == null || sEntName == null || sEntName.length() == 0 )
            throw new com.sun.star.lang.IllegalArgumentException();

        if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.DEFAULT_INIT )
        {
            if ( aClassID != null && aClassID.length != 0 )
            {
                if ( aClassID.length != m_classID.length )
                    throw new com.sun.star.lang.IllegalArgumentException();

                for ( int i = 0; i < aClassID.length; i++ )
                    if ( aClassID[i] != m_classID[i] )
                        throw new com.sun.star.lang.IllegalArgumentException();
            }
            else if ( !xStorage.hasByName( sEntName ) )
                throw new com.sun.star.lang.IllegalArgumentException();
        }
        else if ( nEntryConnectionMode == com.sun.star.embed.EntryInitModes.TRUNCATE_INIT )
        {
            if ( aClassID.length != m_classID.length )
                throw new com.sun.star.lang.IllegalArgumentException();

            for ( int i = 0; i < m_classID.length; i++ )
                if ( aClassID[i] != m_classID[i] )
                    throw new com.sun.star.lang.IllegalArgumentException();
        }

        OwnEmbeddedObject aObject = new OwnEmbeddedObject( m_xContext, m_classID );
        aObject.setPersistentEntry( xStorage, sEntName, nEntryConnectionMode, aArgs, aObjectArgs );

        return aObject;
    }

}