Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
22 : :
23 : : using ::com::sun::star::uno::Reference;
24 : : using ::com::sun::star::uno::XInterface;
25 : : using ::rtl::OUString;
26 : :
27 : : namespace comphelper
28 : : {
29 : :
30 : 5741 : UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
31 : 5741 : : mnNextId( 1 )
32 : : {
33 : 5741 : }
34 : :
35 : : /** returns a unique identifier for the given uno object. IF a uno object is
36 : : registered more than once, the returned identifier is always the same.
37 : : */
38 : 0 : const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
39 : : {
40 : 0 : IdMap_t::const_iterator aIter;
41 [ # # ][ # # ]: 0 : if( findReference( rInterface, aIter ) )
42 : : {
43 : 0 : return (*aIter).first;
44 : : }
45 : : else
46 : : {
47 : 0 : OUString aId( "id" );
48 : 0 : aId += OUString::valueOf( mnNextId++ );
49 [ # # ][ # # ]: 0 : return (*maEntries.insert( IdMap_t::value_type( aId, rInterface ) ).first).first;
[ # # ]
50 : : }
51 : : }
52 : :
53 : : /** registers the given uno object with the given identifier.
54 : :
55 : : @returns
56 : : false, if the given identifier already exists and is not associated with the given interface
57 : : */
58 : 30 : bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
59 : : {
60 : 30 : IdMap_t::const_iterator aIter;
61 [ - + ][ + - ]: 30 : if( findReference( rInterface, aIter ) )
62 : : {
63 : 0 : return rIdentifier != (*aIter).first;
64 : : }
65 [ + - ][ - + ]: 30 : else if( findIdentifier( rIdentifier, aIter ) )
66 : : {
67 : 0 : return false;
68 : : }
69 : : else
70 : : {
71 [ + - ][ + - ]: 30 : maEntries.insert( IdMap_t::value_type( rIdentifier, rInterface ) );
[ + - ]
72 : :
73 : : // see if this is a reference like something we would generate in the future
74 : 30 : const sal_Unicode *p = rIdentifier.getStr();
75 : 30 : sal_Int32 nLength = rIdentifier.getLength();
76 : :
77 : : // see if the identifier is 'id' followed by a pure integer value
78 [ + - ][ - + ]: 30 : if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
[ + - ]
79 : 0 : return true;
80 : :
81 : 30 : nLength -= 2;
82 : 30 : p += 2;
83 : :
84 [ + + ]: 318 : while(nLength--)
85 : : {
86 [ + - ][ - + ]: 288 : if( (*p < '0') || (*p > '9') )
87 : 0 : return true; // a custom id, that will never conflict with genereated id's
88 : :
89 : 288 : p++;
90 : : }
91 : :
92 : : // the identifier is a pure integer value
93 : : // so we make sure we will never generate
94 : : // an integer value like this one
95 : 30 : sal_Int32 nId = rIdentifier.copy(2).toInt32();
96 [ + + ]: 30 : if( mnNextId <= nId )
97 : 8 : mnNextId = nId + 1;
98 : :
99 : 30 : return true;
100 : : }
101 : : }
102 : :
103 : : /** @returns
104 : : the identifier for the given uno object. If this uno object is not already
105 : : registered, an empty string is returned
106 : : */
107 : 344 : const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
108 : : {
109 : 344 : IdMap_t::const_iterator aIter;
110 [ - + ][ + - ]: 344 : if( findReference( rInterface, aIter ) )
111 : : {
112 : 0 : return (*aIter).first;
113 : : }
114 : : else
115 : : {
116 [ + + ][ + - ]: 344 : static const OUString aEmpty;
117 : 344 : return aEmpty;
118 : : }
119 : : }
120 : :
121 : : /** @returns
122 : : the uno object that is registered with the given identifier. If no uno object
123 : : is registered with the given identifier, an empty reference is returned.
124 : : */
125 : 0 : const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
126 : : {
127 : 0 : IdMap_t::const_iterator aIter;
128 [ # # ][ # # ]: 0 : if( findIdentifier( rIdentifier, aIter ) )
129 : : {
130 : 0 : return (*aIter).second;
131 : : }
132 : : else
133 : : {
134 [ # # ][ # # ]: 0 : static const Reference< XInterface > aEmpty;
135 : 0 : return aEmpty;
136 : : }
137 : : }
138 : :
139 : 374 : bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
140 : : {
141 : 374 : rIter = maEntries.begin();
142 : 374 : const IdMap_t::const_iterator aEnd( maEntries.end() );
143 [ + + ]: 556 : while( rIter != aEnd )
144 : : {
145 [ + - ][ - + ]: 182 : if( (*rIter).second == rInterface )
146 : 0 : return true;
147 : :
148 : 182 : rIter++;
149 : : }
150 : :
151 : 374 : return false;
152 : : }
153 : :
154 : 30 : bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
155 : : {
156 : 30 : rIter = maEntries.find( rIdentifier );
157 : 30 : return rIter != maEntries.end();
158 : : }
159 : :
160 : : }
161 : :
162 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|