1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*63bba73cSAndrew Rist * distributed with this work for additional information
6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at
10*63bba73cSAndrew Rist *
11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist *
13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the
17*63bba73cSAndrew Rist * specific language governing permissions and limitations
18*63bba73cSAndrew Rist * under the License.
19*63bba73cSAndrew Rist *
20*63bba73cSAndrew Rist *************************************************************/
21*63bba73cSAndrew Rist
22*63bba73cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
27cdf0e10cSrcweir #include "unointerfacetouniqueidentifiermapper.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
30cdf0e10cSrcweir using ::com::sun::star::uno::XInterface;
31cdf0e10cSrcweir using ::rtl::OUString;
32cdf0e10cSrcweir
33cdf0e10cSrcweir namespace comphelper
34cdf0e10cSrcweir {
35cdf0e10cSrcweir
UnoInterfaceToUniqueIdentifierMapper()36cdf0e10cSrcweir UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
37cdf0e10cSrcweir : mnNextId( 1 )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir }
40cdf0e10cSrcweir
41cdf0e10cSrcweir /** returns a unique identifier for the given uno object. IF a uno object is
42cdf0e10cSrcweir registered more than once, the returned identifier is always the same.
43cdf0e10cSrcweir */
registerReference(const Reference<XInterface> & rInterface)44cdf0e10cSrcweir const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir IdMap_t::const_iterator aIter;
47cdf0e10cSrcweir if( findReference( rInterface, aIter ) )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir return (*aIter).first;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir else
52cdf0e10cSrcweir {
53cdf0e10cSrcweir OUString aId( RTL_CONSTASCII_USTRINGPARAM( "id" ) );
54cdf0e10cSrcweir aId += OUString::valueOf( mnNextId++ );
55cdf0e10cSrcweir return (*maEntries.insert( IdMap_t::value_type( aId, rInterface ) ).first).first;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
59cdf0e10cSrcweir /** registers the given uno object with the given identifier.
60cdf0e10cSrcweir
61cdf0e10cSrcweir @returns
62cdf0e10cSrcweir false, if the given identifier already exists and is not associated with the given interface
63cdf0e10cSrcweir */
registerReference(const OUString & rIdentifier,const Reference<XInterface> & rInterface)64cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir IdMap_t::const_iterator aIter;
67cdf0e10cSrcweir if( findReference( rInterface, aIter ) )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir return rIdentifier != (*aIter).first;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir else if( findIdentifier( rIdentifier, aIter ) )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir return false;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir else
76cdf0e10cSrcweir {
77cdf0e10cSrcweir maEntries.insert( IdMap_t::value_type( rIdentifier, rInterface ) );
78cdf0e10cSrcweir
79cdf0e10cSrcweir // see if this is a reference like something we would generate in the future
80cdf0e10cSrcweir const sal_Unicode *p = rIdentifier.getStr();
81cdf0e10cSrcweir sal_Int32 nLength = rIdentifier.getLength();
82cdf0e10cSrcweir
83cdf0e10cSrcweir // see if the identifier is 'id' followed by a pure integer value
84cdf0e10cSrcweir if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
85cdf0e10cSrcweir return true;
86cdf0e10cSrcweir
87cdf0e10cSrcweir nLength -= 2;
88cdf0e10cSrcweir p += 2;
89cdf0e10cSrcweir
90cdf0e10cSrcweir while(nLength--)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir if( (*p < '0') || (*p > '9') )
93cdf0e10cSrcweir return true; // a custom id, that will never conflict with genereated id's
94cdf0e10cSrcweir
95cdf0e10cSrcweir p++;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
98cdf0e10cSrcweir // the identifier is a pure integer value
99cdf0e10cSrcweir // so we make sure we will never generate
100cdf0e10cSrcweir // an integer value like this one
101cdf0e10cSrcweir sal_Int32 nId = rIdentifier.copy(2).toInt32();
102cdf0e10cSrcweir if( mnNextId <= nId )
103cdf0e10cSrcweir mnNextId = nId + 1;
104cdf0e10cSrcweir
105cdf0e10cSrcweir return true;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
109cdf0e10cSrcweir /** @returns
110cdf0e10cSrcweir the identifier for the given uno object. If this uno object is not already
111cdf0e10cSrcweir registered, an empty string is returned
112cdf0e10cSrcweir */
getIdentifier(const Reference<XInterface> & rInterface) const113cdf0e10cSrcweir const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir IdMap_t::const_iterator aIter;
116cdf0e10cSrcweir if( findReference( rInterface, aIter ) )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir return (*aIter).first;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir else
121cdf0e10cSrcweir {
122cdf0e10cSrcweir static const OUString aEmpty;
123cdf0e10cSrcweir return aEmpty;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir /** @returns
128cdf0e10cSrcweir the uno object that is registered with the given identifier. If no uno object
129cdf0e10cSrcweir is registered with the given identifier, an empty reference is returned.
130cdf0e10cSrcweir */
getReference(const OUString & rIdentifier) const131cdf0e10cSrcweir const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
132cdf0e10cSrcweir {
133cdf0e10cSrcweir IdMap_t::const_iterator aIter;
134cdf0e10cSrcweir if( findIdentifier( rIdentifier, aIter ) )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir return (*aIter).second;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir else
139cdf0e10cSrcweir {
140cdf0e10cSrcweir static const Reference< XInterface > aEmpty;
141cdf0e10cSrcweir return aEmpty;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
findReference(const Reference<XInterface> & rInterface,IdMap_t::const_iterator & rIter) const145cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
146cdf0e10cSrcweir {
147cdf0e10cSrcweir rIter = maEntries.begin();
148cdf0e10cSrcweir const IdMap_t::const_iterator aEnd( maEntries.end() );
149cdf0e10cSrcweir while( rIter != aEnd )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir if( (*rIter).second == rInterface )
152cdf0e10cSrcweir return true;
153cdf0e10cSrcweir
154cdf0e10cSrcweir rIter++;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
157cdf0e10cSrcweir return false;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
findIdentifier(const OUString & rIdentifier,IdMap_t::const_iterator & rIter) const160cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
161cdf0e10cSrcweir {
162cdf0e10cSrcweir rIter = maEntries.find( rIdentifier );
163cdf0e10cSrcweir return rIter != maEntries.end();
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir }
167cdf0e10cSrcweir
168