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 : #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
21 :
22 : using namespace ::com::sun::star;
23 : using ::com::sun::star::uno::Reference;
24 : using ::com::sun::star::uno::XInterface;
25 :
26 : namespace comphelper
27 : {
28 :
29 0 : UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
30 0 : : mnNextId( 1 )
31 : {
32 0 : }
33 :
34 0 : const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
35 : {
36 : // Be certain that the references we store in our table are to the
37 : // leading / primary XInterface - cf. findReference
38 0 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
39 :
40 0 : IdMap_t::const_iterator aIter;
41 0 : if( findReference( xRef, aIter ) )
42 : {
43 0 : return (*aIter).first;
44 : }
45 : else
46 : {
47 0 : OUString aId( "id" );
48 0 : aId += OUString::number( mnNextId++ );
49 0 : return (*maEntries.insert( IdMap_t::value_type( aId, xRef ) ).first).first;
50 0 : }
51 : }
52 :
53 0 : bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
54 : {
55 0 : IdMap_t::const_iterator aIter;
56 :
57 : // Be certain that the references we store in our table are to the
58 : // leading / primary XInterface - cf. findReference
59 0 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
60 :
61 0 : if( findReference( xRef, aIter ) )
62 : {
63 0 : return rIdentifier != (*aIter).first;
64 : }
65 0 : else if( findIdentifier( rIdentifier, aIter ) )
66 : {
67 0 : return false;
68 : }
69 : else
70 : {
71 0 : insertReference( rIdentifier, xRef );
72 : }
73 :
74 0 : return true;
75 : }
76 :
77 0 : void UnoInterfaceToUniqueIdentifierMapper::registerReferenceAlways( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
78 : {
79 : // Be certain that the references we store in our table are to the
80 : // leading / primary XInterface - cf. findReference
81 0 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
82 :
83 0 : insertReference( rIdentifier, xRef );
84 0 : }
85 :
86 0 : const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
87 : {
88 0 : IdMap_t::const_iterator aIter;
89 0 : if( findReference( rInterface, aIter ) )
90 : {
91 0 : return (*aIter).first;
92 : }
93 : else
94 : {
95 0 : static const OUString aEmpty;
96 0 : return aEmpty;
97 : }
98 : }
99 :
100 0 : const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
101 : {
102 0 : IdMap_t::const_iterator aIter;
103 0 : if( findIdentifier( rIdentifier, aIter ) )
104 : {
105 0 : return (*aIter).second;
106 : }
107 : else
108 : {
109 0 : static const Reference< XInterface > aEmpty;
110 0 : return aEmpty;
111 : }
112 : }
113 :
114 0 : bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
115 : {
116 0 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
117 :
118 0 : rIter = maEntries.begin();
119 :
120 0 : const IdMap_t::const_iterator aEnd( maEntries.end() );
121 0 : while( rIter != aEnd )
122 : {
123 : // The Reference == operator, does a repeated queryInterface on
124 : // this to ensure we got the right XInterface base-class. However,
125 : // we can be sure that this has been done already by the time we
126 : // get to here.
127 0 : if( (*rIter).second.get() == xRef.get() )
128 0 : return true;
129 :
130 0 : rIter++;
131 : }
132 :
133 0 : return false;
134 : }
135 :
136 0 : bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
137 : {
138 0 : rIter = maEntries.find( rIdentifier );
139 0 : return rIter != maEntries.end();
140 : }
141 :
142 0 : void UnoInterfaceToUniqueIdentifierMapper::insertReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
143 : {
144 0 : maEntries[rIdentifier] = rInterface;
145 :
146 : // see if this is a reference like something we would generate in the future
147 0 : const sal_Unicode *p = rIdentifier.getStr();
148 0 : sal_Int32 nLength = rIdentifier.getLength();
149 :
150 : // see if the identifier is 'id' followed by a pure integer value
151 0 : if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
152 0 : return;
153 :
154 0 : nLength -= 2;
155 0 : p += 2;
156 :
157 0 : while(nLength--)
158 : {
159 0 : if( (*p < '0') || (*p > '9') )
160 0 : return; // a custom id, that will never conflict with genereated id's
161 :
162 0 : p++;
163 : }
164 :
165 : // the identifier is a pure integer value
166 : // so we make sure we will never generate
167 : // an integer value like this one
168 0 : sal_Int32 nId = rIdentifier.copy(2).toInt32();
169 0 : if( mnNextId <= nId )
170 0 : mnNextId = nId + 1;
171 : }
172 :
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|