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 13228 : UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
30 13228 : : mnNextId( 1 )
31 : {
32 13228 : }
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 36 : bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
54 : {
55 36 : 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 36 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
60 :
61 36 : if( findReference( xRef, aIter ) )
62 : {
63 0 : return rIdentifier != (*aIter).first;
64 : }
65 36 : else if( findIdentifier( rIdentifier, aIter ) )
66 : {
67 2 : return false;
68 : }
69 : else
70 : {
71 34 : insertReference( rIdentifier, xRef );
72 : }
73 :
74 34 : return true;
75 : }
76 :
77 2 : 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 2 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
82 :
83 2 : insertReference( rIdentifier, xRef );
84 2 : }
85 :
86 778 : const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
87 : {
88 778 : IdMap_t::const_iterator aIter;
89 778 : if( findReference( rInterface, aIter ) )
90 : {
91 0 : return (*aIter).first;
92 : }
93 : else
94 : {
95 778 : static const OUString aEmpty;
96 778 : return aEmpty;
97 : }
98 : }
99 :
100 10 : const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
101 : {
102 10 : IdMap_t::const_iterator aIter;
103 10 : if( findIdentifier( rIdentifier, aIter ) )
104 : {
105 10 : return (*aIter).second;
106 : }
107 : else
108 : {
109 0 : static const Reference< XInterface > aEmpty;
110 0 : return aEmpty;
111 : }
112 : }
113 :
114 814 : bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
115 : {
116 814 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
117 :
118 814 : rIter = maEntries.begin();
119 :
120 814 : const IdMap_t::const_iterator aEnd( maEntries.end() );
121 1814 : 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 186 : if( (*rIter).second.get() == xRef.get() )
128 0 : return true;
129 :
130 186 : rIter++;
131 : }
132 :
133 814 : return false;
134 : }
135 :
136 46 : bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
137 : {
138 46 : rIter = maEntries.find( rIdentifier );
139 46 : return rIter != maEntries.end();
140 : }
141 :
142 36 : void UnoInterfaceToUniqueIdentifierMapper::insertReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
143 : {
144 36 : maEntries[rIdentifier] = rInterface;
145 :
146 : // see if this is a reference like something we would generate in the future
147 36 : const sal_Unicode *p = rIdentifier.getStr();
148 36 : sal_Int32 nLength = rIdentifier.getLength();
149 :
150 : // see if the identifier is 'id' followed by a pure integer value
151 36 : if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
152 0 : return;
153 :
154 36 : nLength -= 2;
155 36 : p += 2;
156 :
157 368 : while(nLength--)
158 : {
159 296 : if( (*p < '0') || (*p > '9') )
160 0 : return; // a custom id, that will never conflict with genereated id's
161 :
162 296 : 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 36 : sal_Int32 nId = rIdentifier.copy(2).toInt32();
169 36 : if( mnNextId <= nId )
170 12 : mnNextId = nId + 1;
171 : }
172 :
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|