1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 30*cdf0e10cSrcweir #include "precompiled_xmloff.hxx" 31*cdf0e10cSrcweir #include "unointerfacetouniqueidentifiermapper.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 34*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 35*cdf0e10cSrcweir using ::rtl::OUString; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir namespace comphelper 38*cdf0e10cSrcweir { 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper() 41*cdf0e10cSrcweir : mnNextId( 1 ) 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /** returns a unique identifier for the given uno object. IF a uno object is 46*cdf0e10cSrcweir registered more than once, the returned identifier is always the same. 47*cdf0e10cSrcweir */ 48*cdf0e10cSrcweir const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface ) 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir IdMap_t::const_iterator aIter; 51*cdf0e10cSrcweir if( findReference( rInterface, aIter ) ) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir return (*aIter).first; 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir else 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir OUString aId( RTL_CONSTASCII_USTRINGPARAM( "id" ) ); 58*cdf0e10cSrcweir aId += OUString::valueOf( mnNextId++ ); 59*cdf0e10cSrcweir return (*maEntries.insert( IdMap_t::value_type( aId, rInterface ) ).first).first; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir /** registers the given uno object with the given identifier. 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir @returns 66*cdf0e10cSrcweir false, if the given identifier already exists and is not associated with the given interface 67*cdf0e10cSrcweir */ 68*cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface ) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir IdMap_t::const_iterator aIter; 71*cdf0e10cSrcweir if( findReference( rInterface, aIter ) ) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir return rIdentifier != (*aIter).first; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir else if( findIdentifier( rIdentifier, aIter ) ) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir return false; 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir else 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir maEntries.insert( IdMap_t::value_type( rIdentifier, rInterface ) ); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir // see if this is a reference like something we would generate in the future 84*cdf0e10cSrcweir const sal_Unicode *p = rIdentifier.getStr(); 85*cdf0e10cSrcweir sal_Int32 nLength = rIdentifier.getLength(); 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir // see if the identifier is 'id' followed by a pure integer value 88*cdf0e10cSrcweir if( nLength < 2 || p[0] != 'i' || p[1] != 'd' ) 89*cdf0e10cSrcweir return true; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir nLength -= 2; 92*cdf0e10cSrcweir p += 2; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir while(nLength--) 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir if( (*p < '0') || (*p > '9') ) 97*cdf0e10cSrcweir return true; // a custom id, that will never conflict with genereated id's 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir p++; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir // the identifier is a pure integer value 103*cdf0e10cSrcweir // so we make sure we will never generate 104*cdf0e10cSrcweir // an integer value like this one 105*cdf0e10cSrcweir sal_Int32 nId = rIdentifier.copy(2).toInt32(); 106*cdf0e10cSrcweir if( mnNextId <= nId ) 107*cdf0e10cSrcweir mnNextId = nId + 1; 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir return true; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir /** @returns 114*cdf0e10cSrcweir the identifier for the given uno object. If this uno object is not already 115*cdf0e10cSrcweir registered, an empty string is returned 116*cdf0e10cSrcweir */ 117*cdf0e10cSrcweir const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir IdMap_t::const_iterator aIter; 120*cdf0e10cSrcweir if( findReference( rInterface, aIter ) ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir return (*aIter).first; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir else 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir static const OUString aEmpty; 127*cdf0e10cSrcweir return aEmpty; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir /** @returns 132*cdf0e10cSrcweir the uno object that is registered with the given identifier. If no uno object 133*cdf0e10cSrcweir is registered with the given identifier, an empty reference is returned. 134*cdf0e10cSrcweir */ 135*cdf0e10cSrcweir const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir IdMap_t::const_iterator aIter; 138*cdf0e10cSrcweir if( findIdentifier( rIdentifier, aIter ) ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir return (*aIter).second; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir else 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir static const Reference< XInterface > aEmpty; 145*cdf0e10cSrcweir return aEmpty; 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir rIter = maEntries.begin(); 152*cdf0e10cSrcweir const IdMap_t::const_iterator aEnd( maEntries.end() ); 153*cdf0e10cSrcweir while( rIter != aEnd ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir if( (*rIter).second == rInterface ) 156*cdf0e10cSrcweir return true; 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir rIter++; 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir return false; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir rIter = maEntries.find( rIdentifier ); 167*cdf0e10cSrcweir return rIter != maEntries.end(); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172