Update ooo320-m1
[ooovba.git] / xmloff / source / core / unointerfacetouniqueidentifiermapper.cxx
blobb372fc70b7ec4943b5726aff8957c13a784d1c64
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unointerfacetouniqueidentifiermapper.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_xmloff.hxx"
34 #include "unointerfacetouniqueidentifiermapper.hxx"
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XInterface;
38 using ::rtl::OUString;
40 namespace comphelper
43 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
44 : mnNextId( 1 )
48 /** returns a unique identifier for the given uno object. IF a uno object is
49 registered more than once, the returned identifier is always the same.
51 const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
53 IdMap_t::const_iterator aIter;
54 if( findReference( rInterface, aIter ) )
56 return (*aIter).first;
58 else
60 OUString aId( RTL_CONSTASCII_USTRINGPARAM( "id" ) );
61 aId += OUString::valueOf( mnNextId++ );
62 return (*maEntries.insert( IdMap_t::value_type( aId, rInterface ) ).first).first;
66 /** registers the given uno object with the given identifier.
68 @returns
69 false, if the given identifier already exists and is not associated with the given interface
71 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
73 IdMap_t::const_iterator aIter;
74 if( findReference( rInterface, aIter ) )
76 return rIdentifier != (*aIter).first;
78 else if( findIdentifier( rIdentifier, aIter ) )
80 return false;
82 else
84 maEntries.insert( IdMap_t::value_type( rIdentifier, rInterface ) );
86 // see if this is a reference like something we would generate in the future
87 const sal_Unicode *p = rIdentifier.getStr();
88 sal_Int32 nLength = rIdentifier.getLength();
90 // see if the identifier is 'id' followed by a pure integer value
91 if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
92 return true;
94 nLength -= 2;
95 p += 2;
97 while(nLength--)
99 if( (*p < '0') || (*p > '9') )
100 return true; // a custom id, that will never conflict with genereated id's
102 p++;
105 // the identifier is a pure integer value
106 // so we make sure we will never generate
107 // an integer value like this one
108 sal_Int32 nId = rIdentifier.copy(2).toInt32();
109 if( mnNextId <= nId )
110 mnNextId = nId + 1;
112 return true;
116 /** @returns
117 the identifier for the given uno object. If this uno object is not already
118 registered, an empty string is returned
120 const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
122 IdMap_t::const_iterator aIter;
123 if( findReference( rInterface, aIter ) )
125 return (*aIter).first;
127 else
129 static const OUString aEmpty;
130 return aEmpty;
134 /** @returns
135 the uno object that is registered with the given identifier. If no uno object
136 is registered with the given identifier, an empty reference is returned.
138 const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
140 IdMap_t::const_iterator aIter;
141 if( findIdentifier( rIdentifier, aIter ) )
143 return (*aIter).second;
145 else
147 static const Reference< XInterface > aEmpty;
148 return aEmpty;
152 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
154 rIter = maEntries.begin();
155 const IdMap_t::const_iterator aEnd( maEntries.end() );
156 while( rIter != aEnd )
158 if( (*rIter).second == rInterface )
159 return true;
161 rIter++;
164 return false;
167 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
169 rIter = maEntries.find( rIdentifier );
170 return rIter != maEntries.end();