1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
32 using ::com::sun::star::uno::Reference
;
33 using ::com::sun::star::uno::XInterface
;
34 using ::rtl::OUString
;
39 UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
44 /** returns a unique identifier for the given uno object. IF a uno object is
45 registered more than once, the returned identifier is always the same.
47 const OUString
& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference
< XInterface
>& rInterface
)
49 IdMap_t::const_iterator aIter
;
50 if( findReference( rInterface
, aIter
) )
52 return (*aIter
).first
;
56 OUString
aId( RTL_CONSTASCII_USTRINGPARAM( "id" ) );
57 aId
+= OUString::valueOf( mnNextId
++ );
58 return (*maEntries
.insert( IdMap_t::value_type( aId
, rInterface
) ).first
).first
;
62 /** registers the given uno object with the given identifier.
65 false, if the given identifier already exists and is not associated with the given interface
67 bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString
& rIdentifier
, const Reference
< XInterface
>& rInterface
)
69 IdMap_t::const_iterator aIter
;
70 if( findReference( rInterface
, aIter
) )
72 return rIdentifier
!= (*aIter
).first
;
74 else if( findIdentifier( rIdentifier
, aIter
) )
80 maEntries
.insert( IdMap_t::value_type( rIdentifier
, rInterface
) );
82 // see if this is a reference like something we would generate in the future
83 const sal_Unicode
*p
= rIdentifier
.getStr();
84 sal_Int32 nLength
= rIdentifier
.getLength();
86 // see if the identifier is 'id' followed by a pure integer value
87 if( nLength
< 2 || p
[0] != 'i' || p
[1] != 'd' )
95 if( (*p
< '0') || (*p
> '9') )
96 return true; // a custom id, that will never conflict with genereated id's
101 // the identifier is a pure integer value
102 // so we make sure we will never generate
103 // an integer value like this one
104 sal_Int32 nId
= rIdentifier
.copy(2).toInt32();
105 if( mnNextId
<= nId
)
113 the identifier for the given uno object. If this uno object is not already
114 registered, an empty string is returned
116 const OUString
& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference
< XInterface
>& rInterface
) const
118 IdMap_t::const_iterator aIter
;
119 if( findReference( rInterface
, aIter
) )
121 return (*aIter
).first
;
125 static const OUString aEmpty
;
131 the uno object that is registered with the given identifier. If no uno object
132 is registered with the given identifier, an empty reference is returned.
134 const Reference
< XInterface
>& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString
& rIdentifier
) const
136 IdMap_t::const_iterator aIter
;
137 if( findIdentifier( rIdentifier
, aIter
) )
139 return (*aIter
).second
;
143 static const Reference
< XInterface
> aEmpty
;
148 bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference
< XInterface
>& rInterface
, IdMap_t::const_iterator
& rIter
) const
150 rIter
= maEntries
.begin();
151 const IdMap_t::const_iterator
aEnd( maEntries
.end() );
152 while( rIter
!= aEnd
)
154 if( (*rIter
).second
== rInterface
)
163 bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString
& rIdentifier
, IdMap_t::const_iterator
& rIter
) const
165 rIter
= maEntries
.find( rIdentifier
);
166 return rIter
!= maEntries
.end();
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */